Question

I have created a table in side the edititemtamlate of grid view.

<asp:TemplateField>
<EditItemTemplate>
    <%--    <asp:Panel ID="pnltable" runat="server">--%>
<table border="1px" cellpadding="0px" cellspacing="0px" >
<tr>
    <td>
        <asp:Label Text="T1" runat="server" ID="Label111"></asp:Label>
    </td>
    <td>
        <asp:Label Text="T2" runat="server" ID="Label112"></asp:Label>
    </td>
    <td>
        <asp:Label Text="T3" runat="server" ID="Label113"></asp:Label>
    </td>
    <td>
        <asp:Label Text="T4" runat="server" ID="Label114"></asp:Label>
    </td>
    <td>
        <asp:Label Text="T5" runat="server" ID="Label115"></asp:Label>
    </td>
    <td>
        <asp:Label Text="T6" runat="server" ID="Label116"></asp:Label>
    </td>
    <td>
        <asp:Label Text="T7" runat="server" ID="Label117"></asp:Label>
    </td>
    <td>
        <asp:Label Text="T8" runat="server" ID="Label118"></asp:Label>
    </td>
    <td>
        <asp:Label Text="T9" runat="server" ID="Label119"></asp:Label>
    </td>
    <td>
        <asp:Label Text="T10" runat="server" ID="Label1110"></asp:Label>
    </td>
</tr>
<tr>
    <td>
        <asp:CheckBox ID="CheckBox11" runat="server" AutoPostBack="true" />
    </td>
    <td>
        <asp:CheckBox ID="CheckBox12" runat="server" AutoPostBack="true" />
    </td>
    <td>
        <asp:CheckBox ID="CheckBox13" runat="server" AutoPostBack="true" />
    </td>
    <td>
        <asp:CheckBox ID="CheckBox14" runat="server" AutoPostBack="true" />
    </td>
    <td>
        <asp:CheckBox ID="CheckBox15" runat="server" AutoPostBack="true" />
    </td>
    <td>
        <asp:CheckBox ID="CheckBox16" runat="server"  AutoPostBack="true"/>
    </td>
    <td>
        <asp:CheckBox ID="CheckBox17" runat="server" AutoPostBack="true" />
    </td>
    <td>
        <asp:CheckBox ID="CheckBox18" runat="server" AutoPostBack="true" />
    </td>
    <td>
        <asp:CheckBox ID="CheckBox19" runat="server" AutoPostBack="true" />
    </td>
    <td>
        <asp:CheckBox ID="CheckBox20" runat="server" AutoPostBack="true" />
    </td>
</tr>
</table>
    <%--</asp:Panel>--%>
</EditItemTemplate>

When user clicks on the edit button this table shows with the value from the tables.. For this I write the code in editcommand like this which work perfectly.

Guid SubscriptionId = (Guid)Subscriptions.DataKeys[e.NewEditIndex].Value;
var s = m.bussinesCollection.BussinesPlanning.GetSingleSubVersionTrueFalse(SubscriptionId);
CheckBox d11 = (CheckBox)Subscriptions.Rows[e.NewEditIndex].FindControl("CheckBox11");
CheckBox d12 = (CheckBox)Subscriptions.Rows[e.NewEditIndex].FindControl("CheckBox12");
CheckBox d13 = (CheckBox)Subscriptions.Rows[e.NewEditIndex].FindControl("CheckBox13");
CheckBox d14 = (CheckBox)Subscriptions.Rows[e.NewEditIndex].FindControl("CheckBox14");
CheckBox d15 = (CheckBox)Subscriptions.Rows[e.NewEditIndex].FindControl("CheckBox15");
CheckBox d16 = (CheckBox)Subscriptions.Rows[e.NewEditIndex].FindControl("CheckBox16");
CheckBox d17 = (CheckBox)Subscriptions.Rows[e.NewEditIndex].FindControl("CheckBox17");
CheckBox d18 = (CheckBox)Subscriptions.Rows[e.NewEditIndex].FindControl("CheckBox18");
CheckBox d19 = (CheckBox)Subscriptions.Rows[e.NewEditIndex].FindControl("CheckBox19");
CheckBox d20 = (CheckBox)Subscriptions.Rows[e.NewEditIndex].FindControl("CheckBox20");

    if (d11 != null){
        d11.Checked = s.T1.Value;
    }
    if (d12 != null){
        d12.Checked = s.T2.Value;
    }
    if (d13 != null){
        d13.Checked = s.T3.Value;
    }
    if (d14 != null){
        d14.Checked = s.T4.Value;
    }
    if (d15 != null){
        d15.Checked = s.T5.Value;
    }
    if (d16 != null){
        d16.Checked = s.T6.Value;
    }
    if (d17 != null){
        d17.Checked = s.T7.Value;
    }
    if (d18 != null){
        d18.Checked = s.T8.Value;
    }
    if (d19 != null) {
        d19.Checked = s.T9.Value;
    }
    if (d20 != null){
        d20.Checked = s.T10.Value;
    }

Now in the update command when i change the checkbox value and try to update it like in this code in update command

CheckBox c1 = (CheckBox)(Subscriptions.Rows[e.RowIndex].FindControl("CheckBox11"));
CheckBox c2 = (CheckBox)(Subscriptions.Rows[e.RowIndex].Cells[7].FindControl("CheckBox12"));
CheckBox c3 = (CheckBox)(Subscriptions.Rows[e.RowIndex].FindControl("CheckBox13"));
CheckBox c4 = (CheckBox)(Subscriptions.Rows[e.RowIndex].FindControl("CheckBox14"));
CheckBox c5 = (CheckBox)(Subscriptions.Rows[e.RowIndex].FindControl("CheckBox15"));
CheckBox c6 = (CheckBox)(Subscriptions.Rows[e.RowIndex].FindControl("CheckBox16"));
CheckBox c7 = (CheckBox)(Subscriptions.Rows[e.RowIndex].FindControl("CheckBox17"));
CheckBox c8 = (CheckBox)(Subscriptions.Rows[e.RowIndex].FindControl("CheckBox18"));
CheckBox c9 = (CheckBox)(Subscriptions.Rows[e.RowIndex].FindControl("CheckBox19"));
CheckBox c10 = (CheckBox)(Subscriptions.Rows[e.RowIndex].FindControl("CheckBox20"));

if (c1 != null && c2 != null && c3 != null && c4 != null && c5 != null && c6 != null && c7 != null && c8 != null && c9 != null && c10 != null)
{
    m.bussinesCollection.BussinesPlanning.UpdateSubVersionTrueFalseForUpdate(subscriptionID, c1.Checked, c2.Checked, c3.Checked, c4.Checked, c5.Checked, c6.Checked, c7.Checked, c8.Checked, c9.Checked, c10.Checked);
}

But here it is not giving any error but when I debug the code I show that, in the checkbox controls c1 to c10 in update command it always gets value Checked = false

even if i check the checkbox.

So that it updates in a wrong way.

Can any tell me where I am wrong..?

Was it helpful?

Solution

This is small example what i tried

protected void Page_Load(object sender, EventArgs e)
{
  if (!Page.IsPostBack) {
       bindgv();
  }
}

public void bindgv()
{
  SqlDataAdapter dap = new SqlDataAdapter("select * from mytable", Con);
  DataSet ds = new DataSet();
  dap.Fill(ds);
  GridView1.DataSource = ds.Tables[0];
  GridView1.DataBind();
}

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
  GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
  CheckBox c1 = (CheckBox)GridView1.Rows[e.RowIndex].FindControl("CheckBox1");
  CheckBox c2 = (CheckBox)GridView1.Rows[e.RowIndex].FindControl("CheckBox2");
  CheckBox c3 = (CheckBox)GridView1.Rows[e.RowIndex].FindControl("CheckBox3");
  Label lblid = (Label)GridView1.Rows[e.RowIndex].FindControl("lblid");
  updateMethod(lblid.Text, c1.Checked, c2.Checked, c3.Checked);

}

public void updateMethod(string id, bool c1, bool c2, bool c3)
{
  using (SqlConnection con = new SqlConnection(myCon))
  {
     con.Open();
     SqlCommand cmd = new SqlCommand("update mytable set c1= @c1value , c2=@c2value, c3= @c3value where id=@id", con);
     cmd.Parameters.AddWithValue("@c1value", c1);
     cmd.Parameters.AddWithValue("@c2value", c2);
     cmd.Parameters.AddWithValue("@c3value", c3);
     cmd.Parameters.AddWithValue("@id", id);
     cmd.ExecuteNonQuery();
  }
    GridView1.EditIndex = -1;
    bindgv();
}

protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
  GridView1.EditIndex = e.NewEditIndex;
  bindgv();
}

protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
  GridView1.EditIndex = -1;
  bindgv();
}

HTML MarkUp

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
onrowediting="GridView1_RowEditing" 
onrowcancelingedit="GridView1_RowCancelingEdit" 
onrowupdating="GridView1_RowUpdating">
<Columns>
    <asp:TemplateField HeaderText="ID">
        <ItemTemplate>
            <asp:Label ID="lblid" runat="server" Text='<%# Bind("id") %>'></asp:Label>
        </ItemTemplate>

    </asp:TemplateField>
    <asp:TemplateField HeaderText="Name">
        <ItemTemplate>
            <asp:Label ID="Label3" runat="server" Text='<%# Bind("name") %>'></asp:Label>
        </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="checkbox">
    <ItemTemplate>
    <table>
    <tr>   
    <td><asp:Label ID="Label2" runat="server" Text='<%# Bind("c1") %>'></asp:Label></td>
    <td><asp:Label ID="Label5" runat="server" Text='<%# Bind("c2") %>'></asp:Label></td>
    <td><asp:Label ID="Label4" runat="server" Text='<%# Bind("c3") %>'></asp:Label></td>
    </tr>
     <tr>  
     <td> <asp:CheckBox ID="CheckBox1" runat="server" Checked='<%# Bind("c1") %>' /></td>
     <td><asp:CheckBox ID="CheckBox2" runat="server" Checked='<%# Bind("c2") %>' /></td>
     <td><asp:CheckBox ID="CheckBox3" runat="server" Checked='<%# Bind("c3") %>' /></td>
     </tr>
    </table>
   </ItemTemplate>
 <EditItemTemplate>
   <table>
    <tr>   
    <td><asp:Label ID="Label2" runat="server" Text='<%# Bind("c1") %>'></asp:Label></td>
    <td><asp:Label ID="Label5" runat="server" Text='<%# Bind("c2") %>'></asp:Label></td>
    <td><asp:Label ID="Label4" runat="server" Text='<%# Bind("c3") %>'></asp:Label></td>
   </tr>
   <tr>  
    <td> <asp:CheckBox ID="CheckBox1" runat="server" Checked='<%# Bind("c1") %>' /></td>
    <td><asp:CheckBox ID="CheckBox2" runat="server" Checked='<%# Bind("c2") %>' /></td>
    <td><asp:CheckBox ID="CheckBox3" runat="server" Checked='<%# Bind("c3") %>' /></td>
  </tr>
   </table>
        </EditItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField ShowHeader="False">
        <ItemTemplate>
            <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" 
                CommandName="Edit" Text="Edit"></asp:LinkButton>
        </ItemTemplate>
        <EditItemTemplate>
            <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" 
                CommandName="Update" Text="Update"></asp:LinkButton>
            &nbsp;<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" 
                CommandName="Cancel" Text="Cancel"></asp:LinkButton>
        </EditItemTemplate>
    </asp:TemplateField>
</Columns>
</asp:GridView>

ScreenShot:

ScreenShot

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top