C# 启用和禁用gridview上的链接按钮

C# 启用和禁用gridview上的链接按钮,c#,asp.net,gridview,C#,Asp.net,Gridview,我想根据条件在gridview的某些行上启用或禁用linkbutton。。我可以在一行上启用linkbutton并在同一网格视图的另一行上禁用它吗?我的代码在这里 protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) {LinkButton lnk2 = (LinkButton)e.Row.FindControl("LinkButton2"); if (e.Row.RowType ==

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

  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;
                }

            }




        }

是的,您可以在RowdataBound事件中轻松地执行此操作,但您在代码中使用了
lnk2.Visible
属性

您可能正在为另一个需求使用
Visible
属性,但只想确认它仅用于显示/隐藏链接按钮。要启用/取消启用Linkbutton,请使用Linkbutton的
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;
}
如果要基于同一行中其他列的值启用/禁用Linkbutton,则可以在
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;
}
-----------aspx页面代码---------

您的问题是什么,此代码中有什么不起作用,是否有任何错误消息?。对我来说似乎不工作,但如果我的调试器上没有它,我就不知道你程序的逻辑流程,我不能建议你怎么做一个错误是您没有在每一行上找到LinkButton,第二个错误是您没有将gridview上的每一行与数据库上的每一行连接起来。在这种情况下,您可能需要通过gridview控件进行循环,并相应地设置link button enable/disable,因为上面的代码将只查看当前行项目。不是整行。foreach(DataGridViewRow-in-grid.Rows){var-link=row.FindControl(“LinkButton2”)}如何循环遍历每一行并检查启用/禁用linkbutton的条件?它工作正常,没有任何错误。但正如@Deepu所说的,代码只显示当前行itm。我如何在每行上找到link按钮,并将gridview上的每行与数据库上的每行连接起来@亚里士多德请帮忙谢谢萨洛特:):)@SanjeevRai
    --------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;
            }

        }
    }