Click here to Skip to main content
13,455,909 members (46,708 online)
Rate this:
Please Sign up or sign in to vote.
See more:
My code is as below and my problem is that when I used this code in Visual Studio
2008 it will not work properly. (I used this in my office, but not working)
Actually problem is that when I try to select the check box it cannot be selected.
Auto post back is true.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 

            BackColor="White" BorderColor="#CC9966" BorderWidth="1px" 

            CellPadding="4" DataKeyNames="Eid,Fname" 

            onrowediting="GridView1_RowEditing" onrowupdating="GridView1_RowUpdating" 

            onselectedindexchanged="GridView1_SelectedIndexChanged" ShowFooter="True" 


            style="z-index: 1; left: 126px; top: 262px; position: absolute; height: 140px; width: 448px" 

            onrowcommand="GridView1_RowCommand" AllowSorting="True" 

            onpageindexchanging="GridView1_PageIndexChanging" onsorting="GridView1_Sorting" 

            PageSize="2" AllowPaging="True" BorderStyle="None">
            <pagersettings firstpagetext="First" lastpagetext="Last" nextpagetext="Next">
                PreviousPageText="Prev" />
            <footerstyle backcolor="#FFFFCC" forecolor="#330099" />
                <asp:TemplateField HeaderText="Eid" SortExpression="Eid">
                        <asp:Label ID="Label1" runat="server" Text='<%# Eval("EId") %>'>
                        <asp:TextBox ID="TextBox4" runat="server" 

                        <asp:Label ID="Label1" runat="server" Text='<%# Bind("EId") %>'>


                <asp:TemplateField HeaderText="Salary" SortExpression="Salary">
                        <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Salary") %>'>
                        <asp:TextBox ID="TextBox5" runat="server" 

                        <asp:Label ID="Label2" runat="server" Text='<%# Bind("Salary") %>'>
                <asp:TemplateField HeaderText="Name" SortExpression="Name">
                        <asp:DropDownList ID="DropDownList1" runat="server" 

                            SelectedValue='<%# Eval("FName") %>' 

                            DataSourceID="SqlDataSource1" DataTextField="Fname" DataValueField="Fname">
                        <asp:SqlDataSource ID="SqlDataSource1" runat="server" 

                            ConnectionString="<%$ ConnectionStrings:masterConnectionString %>" 

                            SelectCommand="SELECT [Fname] FROM [Employee]">
                        <asp:TextBox ID="TextBox6" runat="server" 

                        <asp:Label ID="Label3" runat="server" Text='<%# Bind("FName") %>'>
                <%--<asp:CheckBoxField DataField="IsTemp" />--%>
                <asp:TemplateField HeaderText="Edit" ShowHeader="False">
                        <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" 

                            CommandName="Update" Text="Update">
                         <asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" 

                            CommandName="Cancel" Text="Cancel">
                        <asp:LinkButton ID="LinkButton3" runat="server" CommandName="Insert">Insert
                        <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" 

                            CommandName="Edit" Text="Edit">
                <asp:CommandField ShowDeleteButton="True" />
                <asp:CommandField ShowSelectButton="True" />
                        <asp:CheckBox ID="CheckBox2" runat="server" AutoPostBack="True" 

                        <asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="True"  OnCheckedChanged="showtxtbox" Checked="False" Enabled="True" EnableViewState="True" ViewStateMode="Inherit" /><asp:TextBox ID="TextBox7" runat="server" Visible="false">
                <asp:HyperLinkField DataNavigateUrlFields="Salary" 

                    DataNavigateUrlFormatString="Default2.aspx?sal={0}" DataTextField="FName" />
                <asp:ButtonField ButtonType="Button" DataTextField="Salary" Text="Button" />
                <asp:TemplateField HeaderText="txtvalue" SortExpression="txtvalue">
                        <asp:Label ID="Label12" runat="server" Text='<%# Eval("txtvalue") %>'>
                        <asp:Label ID="Label12" runat="server" Text='<%# Bind("txtvalue") %>'>
            <pagerstyle backcolor="#FFFFCC" forecolor="#330099">
                HorizontalAlign="Center" />
            <rowstyle backcolor="White" forecolor="#330099" />
            <SelectedRowStyle BackColor="#FFCC66" ForeColor="#663399" Font-Bold="True" />
            <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" />
            <sortedascendingcellstyle backcolor="#FEFCEB" />
            <sortedascendingheaderstyle backcolor="#AF0101" />
            <sorteddescendingcellstyle backcolor="#F6F0C0" />
            <sorteddescendingheaderstyle backcolor="#7E0000" />

My cs file code is:
protected void showtxtbox(object sender, EventArgs e)
        CheckBox cb = (CheckBox)sender;
        GridViewRow gvr=(GridViewRow) cb.NamingContainer;
        int rrowindex = gvr.RowIndex;
        CheckBox cbb = (CheckBox)GridView1.Rows[rrowindex].Cells[6].Controls[1];
        TextBox txt=(TextBox)GridView1.Rows[rrowindex].Cells[6].Controls[2];
            if (cbb.Checked)
                txt.Visible = true;
            if (cbb.Checked == false)
                txt.Visible = false;
Posted 24-Dec-12 6:39am
Updated 24-Dec-12 8:37am
khiladi786 24-Dec-12 12:41pm
hey bros ..give response...
Member 8197993 24-Dec-12 22:06pm
It looks like you have the checked state = false. If I am not mistaken, a checkbox is always unchecked by default. Try removing the "checked="false" and see if that will allow you to check it then... Though I almost never use checkboxes, so don't hold me to it.
khiladi786 25-Dec-12 6:26am
bro, u r right, but i tried also by removing this "checked=false" but it not working...
actually my this code work well in vs2010 but when i use this code in my vs2008 , than there is problem accour, i select the checkbox but it cann't be selected..
Member 8197993 25-Dec-12 10:39am
Is there a particular reason you are going back and forth between 2010 and 2008? Which platform are you developing under? I use 2010 myself, and am not familiar with 2008 and what changes there are between the 2.

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

so if you have a checkbox for each row of the grid using template field, and upon checking the checkbox you want to show a textbox as visible? try this solution below, you can convert it to c#.

For Each row As GridViewRow In GridView.Rows
    Dim cbb As CheckBox = DirectCast(row.FindControl("CheckBox1"), CheckBox)
    Dim txt As Textbox = DirectCast(row.FindControl("textbox1"), textbox)
    If checkbox.Checked Then
        Dim rowid As Integer = Convert.ToInt32(GridView.DataKeys(row.RowIndex).Value)
        If cbb.Checked = False Then
    txt.Visible = False
    End If

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web04-2016 | 2.8.180322.1 | Last Updated 31 Dec 2012
Copyright © CodeProject, 1999-2018
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100