[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2008-11-10
1
Medium Priority
?
576 Views
Last Modified: 2013-12-17
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
Comment
Question by:fippif
1 Comment
 
LVL 20

Accepted Solution

by:
Juan_Barrera earned 2000 total points
ID: 22927212
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

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses
Course of the Month18 days, 22 hours left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question