【问题标题】:Enable and disable link button on gridview在gridview上启用和禁用链接按钮
【发布时间】:2013-06-20 07:04:32
【问题描述】:

我想根据条件在 gridview 的某些行上启用或禁用链接按钮。我可以在一行上启用链接按钮并在同一网格视图的另一行上禁用它吗?我的代码在这里

  protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{LinkButton lnk2 = (LinkButton)e.Row.FindControl("LinkButton2");
    if (e.Row.RowType == DataControlRowType.DataRow)
    {

        SqlCommand cmd12 = new SqlCommand("Select testsession_status from student_vs_testsession_details where  testsession_id='" + v_testid.Text + "' ", con12);
        SqlDataReader dr12 = cmd12.ExecuteReader();
        while (dr12.Read())
        {
            string test_status = dr12[0].ToString();
            LinkButton lnk2 = (LinkButton)e.Row.FindControl("LinkButton2");
            foreach (GridViewRow row in GridView1.Rows)
            {
                if (v_testtype == "Theory Test" && test_status == "Completed")
                {
                    lnk2.Visible = true;
                }
                else
                {
                    lnk2.Visible = false;
                }

            }




        }

【问题讨论】:

  • 你的问题到底是什么,这段代码有什么不工作,你有任何错误信息吗?对我来说似乎不起作用,但如果没有它在我的调试器上,我也不知道你程序的逻辑流程是什么,我无法建议你该怎么做。 - 一个 Bug 是您没有在每一行上找到 LinkBut​​ton,第二个 Bug 是您没有将 gridview 上的每一行与数据库上的每一行连接起来。
  • 在这种情况下,您可能需要循环通过gridview控件并相应地设置链接按钮启用/禁用,因为上面的代码将只查看当前行项目。不是整行。 foreach(grid.Rows 中的 DataGridViewRow 行){ var link = row.FindControl("LinkBut​​ton2") }
  • 如何循环遍历每一行并检查启用/禁用链接按钮的条件?它没有任何错误..但正如@Deepu所说,代码只看当前行itm ..
  • 如何在每一行上找到 LinkBut​​ton,并将 gridview 上的每一行与数据库上的每一行连接起来。 ?? @Aristos 请帮忙

标签: c# asp.net gridview


【解决方案1】:

是的,您可以在 RowdataBound 事件中轻松做到这一点,但您在代码中使用了 lnk2.Visible 属性。

您可能将Visible 属性用于其他要求,但只是想确认它仅用于显示/隐藏链接按钮。要启用/禁用链接按钮,请使用链接按钮的Enabled 属性。如:

lnk2.Enabled = true;// to enable linkbutton.
lnk2.Enabled = false;// to disable linkbutton.

如果你想使用rowindex,那么你可以e.Row.RowIndex在gridview的'RowDatabound`事件中找到当前行索引。如:

if(e.Row.RowIndex==2)
{
  LinkButton lnk2 = (LinkButton)e.Row.FindControl("LinkButton2");
  lnk2.Enabled=false;
}

如果您想根据同一行中其他列的值启用/禁用链接按钮,那么您可以在Rowdatabound 事件中执行相同的操作。如:

string Namecolumnvalue = Convert.ToString(DataBinder.Eval(e.Row.DataItem, "Name"));
LinkButton lnk2 = (LinkButton)e.Row.FindControl("LinkButton2");
if(Namecolumnvalue =="Disable")
{      
  lnk2.Enabled=false;
}
else{
  lnk2.Enabled=true;
}

【讨论】:

  • Thanksalot :) :) :) @SanjeevRai
【解决方案2】:
    --------aspx page code---------

     <asp:GridView ID="gvLibrary" runat="server" AutoGenerateColumns="False" Width="100%" DataKeyNames="LibMstRefNo"
                        EmptyDataText="No Client Found" CssClass="table table-striped table-bordered" OnRowDataBound="gvLibrary_RowDataBound">
                        <Columns>
     <asp:TemplateField HeaderText="Issue">
                            <ItemTemplate>
                               <asp:LinkButton ID="lnkIssue" runat="server" Text="Issue" OnClick="lnkIssue_Click"></asp:LinkButton>
                            </ItemTemplate>
                            <HeaderStyle HorizontalAlign="Left" />
                                <ItemStyle HorizontalAlign="Left" />
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Receive">
                            <ItemTemplate>
                               <asp:LinkButton ID="lnkReceive" runat="server" Text="Receive" OnClick="lnkReceive_Click" OnClientClick="return confirm('Are you Sure?')"></asp:LinkButton>
                            </ItemTemplate>
                            <HeaderStyle HorizontalAlign="Left" />
                                <ItemStyle HorizontalAlign="Left" />
                        </asp:TemplateField>
                    </Columns>

</asp:GridView>


    ------------aspx.cs page code------------------

 protected void gvLibrary_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            string nbps = e.Row.Cells[8].Text;
            if(nbps== "&nbsp;")
            {
                nbps = "";
            }
            else
            {
                nbps = e.Row.Cells[8].Text;
            }
            if (nbps == "")
            {
                LinkButton btn = (LinkButton)e.Row.FindControl("lnkissue");
                LinkButton btn1 = (LinkButton)e.Row.FindControl("lnkReceive");
                btn.Enabled = true;
                btn1.Enabled = false;
                btn1.ForeColor = System.Drawing.Color.Red;

            }
            else
            {
                LinkButton btn = (LinkButton)e.Row.FindControl("lnkissue");
                LinkButton btn1 = (LinkButton)e.Row.FindControl("lnkReceive");
                btn.Enabled = false;
                btn.ForeColor = System.Drawing.Color.Red;
                btn1.Enabled = true;
            }

        }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-29
    • 1970-01-01
    • 1970-01-01
    • 2012-04-21
    相关资源
    最近更新 更多