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>
cspolton
  • 4,495
  • 4
  • 26
  • 34
SueSaya
  • 57
  • 1
  • 1
  • 9

1 Answers1

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");
           }    
    }
ErikusMaximus
  • 1,150
  • 3
  • 13
  • 28
Amol Kolekar
  • 2,307
  • 5
  • 30
  • 45
  • 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. – SueSaya Oct 26 '12 at 06:32
  • 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? – Amol Kolekar Oct 26 '12 at 06:38
  • I want current status of checkbox. If data in database is true when I checked on checkbox it's return value to false. – SueSaya Oct 26 '12 at 06:42
  • 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. – SueSaya Oct 26 '12 at 07:22
  • you want when checkbox is checked it should return true and vice versa regardless of what value is there in database? – Amol Kolekar Oct 26 '12 at 07:29
  • Yes, first time data show from database but if I checked on checkbox I want it return current value and regardless value in database. – SueSaya Oct 26 '12 at 07:53