• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 593
  • Last Modified:

How to handle OnCheckedChanged event of a TextBox when TextBox is part of a GridView

I have a gridview (please see attached code).


I want to be able to handle the OnCheckedChanged event of the checkbox within the gridrow as followed:


protected void chk1_CheckChanged(Object sender, EventArgs e)
{
      if (ch1.Checked)
            txt1.Enabled = false;
      else if (!ch1.Checked)
            txt1.Enabled = true;
}


Is there a way to make this happen? Thx.
<asp:GridView ID="grd" runat="server" AutoGenerateColumns="False" >
 
<Columns>
                <asp:TemplateField>
                    <HeaderTemplate>
                        <table cellspacing="0" cellpadding="0" border="0" style="height: 25px; font-size: 12px;">
                            <tr style="height: 25px">                                
                                
                                <td align="left" style="width: 70px">
                                check</td>    
                                <td align="left" style="width: 20px">
                                    Text</td>                                
                            </tr>
                        </table>
                    </HeaderTemplate>
                    <ItemTemplate>
                        <table cellspacing="0" cellpadding="0" border="0" style="height: 25px; width: 600px">
                            <tr style="height: 25px">                               
                                
                                <td align="center"style="width: 70px">
                                    <asp:CheckBox ID="chk1" runat="server" AutoPostBack="true" CausesValidation="false" 
                                    OnCheckedChanged="chk1_CheckChanged" />
                                </td>    
                                <td align="left" style="width: 10px">
                                    <asp:TextBox ID="txt1" runat="server" MaxLength="3" /></td>                                
                            </tr>
                        </table>
                        <br />
                        
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
            <HeaderStyle Font-Size="12pt" />
        </asp:GridView>

Open in new window

0
fippif
Asked:
fippif
1 Solution
 
Juan_BarreraCommented:
Hi there, try this code:


  protected void chk1_CheckChanged(Object sender, EventArgs e)
    {
        CheckBox ch1 = (CheckBox)sender;
        GridViewRow row = (GridViewRow)ch1.NamingContainer;
        TextBox txt1 = (TextBox)row.FindControl("txt1");
        if (ch1.Checked)
            txt1.Enabled = false;
        else if (!ch1.Checked)
            txt1.Enabled = true;
    }
 
    protected void grd_RowDataBound(Object sender, GridViewRowEventArgs e)
    {
 if(e.Row.RowType == DataControlRowType.DataRow)
{
        CheckBox ch1 = (CheckBox)e.Row.FindControl("chk1");
        ch1.CheckedChanged += chk1_CheckChanged;    
    }
    }

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now