简体   繁体   中英

How to Enable or Disable Text Box in GridView using C#

I want to be able to enable/disable a textbox inside gridview. I have case statement and in my case statement when Case = 1 then I want to disable a texbox called txtType in my gridview. Here is my code:

SqlDataAdapter da = new SqlDataAdapter(@"select * from my table ", con);
DataTable dtTable = new DataTable();
da.SelectCommand.Parameters.AddWithValue("@RSP_SET_SK", (RSP_SET_SK));
da.Fill(dtTable);

GridView1.DataSource = dtTable.DefaultView;
GridView1.DataBind();

DataRow dtTable_row = dtTable.Rows[0];

if (dtTable.Rows.Count > 0)
{
    DDL_TYPE.SelectedValue = dtTable_row.Field<string>("TYPE").ToString();
    ddlPr.SelectedValue = dtTable_row.Field<Int32>("ID").ToString();
}

DataRow row1 = dtTable.Rows[0];
int temp = Convert.ToInt32(row1["STATUS"]);

switch (temp)
{
   case 1:
     lblStatus.Text = temp + " - Initial Test.";

   break;
}

Since it is in a gridview, you will need to use this..

GridViewRow gvr = (GridViewRow)(sender as Control).Parent.Parent;
string type = ((TextBox).gvr.FindControl("txtType"));

Then to disable or enable you can use..

type.Enabled = true;

I hope that gives you the general idea how to do it.

Edit: You might need to use a foreach loop.

foreach(GridViewRow gvr in GridView1.Rows)
{
    string type = ((TextBox).gvr.FindControl("txtType"));
    type.Enabled = true/false;
}

This should do it I would imagine. I do use this foreach pretty often in one of my applications.

Edit 2:

I just realized I put a period where it should not. My bad. So it should be this.

string type = ((TextBox)gvr.FindControl("txtType"));

or

TextBox type = ((TextBox)gvr.FindControl("txtType"));

or you can just enable it straight up like this..

((TextBox)gvr.FindControl("txtType")).Enabled = true/false;

You should do this in RowDataBound Event . Below link has sample code which can help you.

Reference

You can do this in RowDataBound event. First, make sure you set OnRowDataBound property in the aspx code like this:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowDataBound="GridView1_RowDataBound">
</asp:GridView>

Then add the following in code behind:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        DataRowView data = (DataRowView)e.Row.DataItem;
        TextBox txtType = (TextBox)e.Row.FindControl("txtType");
        int status = Convert.ToInt32(data["STATUS"]);
        if (status == 1)
        {
            txtType.Enabled = false;
        }
    }
}
 a TextBox into a GridView

<asp:GridView ID="mygrid" runat="server">
 <Columns>
  <asp:TemplateField meta:resourcekey="TemplateFieldResource4">
   <ItemTemplate>
    <asp:TextBox ID="mytextBoxID" runat="server" Text="0,00" Enabled="false" />
   </ItemTemplate>
   <HeaderStyle Width="10%" HorizontalAlign="Right"/>
   <ItemStyle HorizontalAlign="Right" />
  </asp:TemplateField>
  </Columns>
</asp:GridView>

protected void any_Click(object sender, EventArgs e) {
   foreach (GridViewRow gvr in gvData.Rows)
       ((TextBox)gvr.FindControl("mytextBoxID")).Enabled = true;
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM