Enable and disable link button on gridview

早过忘川 提交于 2019-12-20 01:37:22

问题


I wants to enable or disable linkbutton on some rows of gridview based on condition.. Can i enable linkbutton on one row and disable it on another row of same grid view ??my code is here

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

            }




        }

回答1:


Yes you can easily do it in RowdataBound Event, but you have used lnk2.Visible property in your code.

you may be using Visible property for another requirement but just want to confirm you that it is used to show/hide the Linkbutton only. To enable/disble a Linkbutton, use Enabled property of Linkbutton. as:

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

If You want to do it using rowindex, then you can e.Row.RowIndex to find the current row index inside 'RowDatabound` event of gridview. as:

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

If you want to enable/ disable Linkbutton based on value of some other column in the same row, then you can do the same inside Rowdatabound event. as:

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



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

        }
    }



来源:https://stackoverflow.com/questions/17207271/enable-and-disable-link-button-on-gridview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!