1

I want to update data when I checked in gridview. Before I shoude get value from checkbox but my code return data from database only. I want current value after I checked.

CodeBehind:-

protected void chkSelected_CheckedChanged(object sender, EventArgs e)
{
    for (int rowIndex = 0; rowIndex < GridView1.Rows.Count; rowIndex++)
    {
        if (Convert.ToString(GridView1.Rows[rowIndex].Cells[4].Text) != "")
        {
            Response.Write("true");
        }
        else
        {
            Response.Write("fasle");
        }
    }
}

Design Code:-

<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1"
              CellPadding="4" GridLines="None" ForeColor="#333333" Font-Size="Smaller" 
              AutoGenerateColumns="False">
    <RowStyle BackColor="#EFF3FB" />
    <Columns>
        <asp:BoundField DataField="LevelID" HeaderText="ลำดับข้อ" ReadOnly="True" 
                        ItemStyle-Width="50"  >
        </asp:BoundField>
        <asp:BoundField DataField="LevelDesc" HeaderText="คำถาม" ReadOnly="True" 
                        ItemStyle-Width="250"  >
        </asp:BoundField>
        <asp:BoundField DataField="ChoiceID" HeaderText="ข้อย่อย" ReadOnly="True" 
                        ItemStyle-Width="50"  >
        </asp:BoundField>
        <asp:BoundField DataField="ChoiceDesc" HeaderText="คำถามย่อย" ReadOnly="True" 
                        ItemStyle-Width="400"  >
        </asp:BoundField>
        <asp:TemplateField HeaderText="ใช่">          
            <ItemTemplate>              
                <asp:CheckBox ID="chkSelected" runat="server" Checked='<%# Eval("Selected").ToString().Equals("True") %>'
                     AutoPostBack="true" OnCheckedChanged="chkSelected_CheckedChanged" CssClass="chkBox" />          
            </ItemTemplate>       
        </asp:TemplateField> 
    </Columns>
    <FooterStyle BackColor="#507CD1" ForeColor="White" Font-Bold="True" />
    <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
    <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
    <HeaderStyle BackColor="#0000CD" Font-Bold="True" ForeColor="White" />
    <EditRowStyle BackColor="#2461BF" />
    <AlternatingRowStyle BackColor="White" />
</asp:GridView>

1 Answer 1

3

use GridViewRow as follow...You can get the current row of the checkbox which is checked using NamingContainer property as follow...

Edit:- Change the markup and add Hiddenfield to hold value for Selected as follow...

<asp:TemplateField HeaderText="ใช่">          
            <ItemTemplate>              
                <asp:CheckBox ID="chkSelected" runat="server"  Checked='<%# Eval("Selected").ToString().Equals("True") %>'
                     AutoPostBack="true" OnCheckedChanged="chkSelected_CheckedChanged" CssClass="chkBox" />     
                <asp:HiddenField ID="hiddenField1" Value='<%# Eval("Selected").ToString() %>' runat="server" />    
            </ItemTemplate>       
 </asp:TemplateField> 

Then you can get the Hiddenfield as follow and it's value as well

protected void chkSelected_CheckedChanged(object sender, EventArgs e)
    {
         GridViewRow row = (GridViewRow)(((CheckBox)sender).NamingContainer);
         HiddenField hdnCheck=(HiddenField)row.Cells[4].FindControl("hiddenField1");
         if (Convert.ToString(hdnCheck.Value != "")
           {
               Response.Write("true");
           }
           else
           {
               Response.Write("false");
           }

         // Edit: You can easily get Checkbox which has been checked, and do your logic
         CheckBox chkSelect=(CheckBox)sender;
         if (chkSelect.Checked)
           {
               Response.Write("true");
           }
           else
           {
               Response.Write("false");
           }    
    }
Sign up to request clarification or add additional context in comments.

6 Comments

Your code it's work! but if data in database is 'True' when I checked it's return 'false' and if data in database is 'Null' when I checked it's return 'false' too.
by looking at your code you have bound the "Selected" value to checkbox,which will be shown when gridview is rendered...on click of that checkbox what exactly you want to achieve?
I want current status of checkbox. If data in database is true when I checked on checkbox it's return value to false.
I try it but it return data in database only when I checked on checkbox. If data in database is false when I checked it return false allway and data in database is true when I checked it retrun true allway too.
you want when checkbox is checked it should return true and vice versa regardless of what value is there in database?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.