在GridView上启用和禁用链接按钮

4

我希望根据条件在GridView的某些行上启用或禁用LinkButton。我能够在同一个GridView的一行上启用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;
                }

            }




        }

你的问题实际上是什么?这段代码有什么问题?你有任何错误信息吗?在我看来,它似乎没有工作,但是如果没有在我的调试器中运行它,也不知道你程序的逻辑流程,我无法为你提供建议。-一个错误是你没有在每一行找到LinkButton,第二个错误是你没有将gridview上的每一行与数据库上的每一行连接起来。 - Aristos
在这种情况下,您可能需要循环遍历GridView控件,并相应地设置链接按钮的启用/禁用状态,因为上面的代码只会检查当前行项,而不是整个行。foreach (DataGridViewRow row in grid.Rows) { var link = row.FindControl("LinkButton2") } - Deepu Madhusoodanan
如何遍历每一行并检查启用/禁用 LinkButton 的条件? 它可以正常工作而没有任何错误...但是,就像 @Deepu 所说的那样,代码只查看当前行 itm.. - ARATHY
我该如何找到每行上的LinkButton,并将gridview上的每行与数据库上的每行连接起来?@Aristos请帮忙。 - ARATHY
2个回答

5

是的,你可以在RowDataBound事件中轻松地完成它,但你在代码中使用了lnk2.Visible属性。

你可能正在使用Visible属性来满足其他要求,但我想确认一下它仅用于显示/隐藏LinkButton。要启用/禁用LinkButton,请使用Linkbutton的Enabled属性,如下所示:

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

如果您想使用rowindex来实现,那么您可以在gridview的'RowDatabound'事件中使用e.Row.RowIndex来查找当前行的索引,如下所示:
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;
}

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

        }
    }

enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接