Click here to Skip to main content
15,891,204 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
Hello,

I am facing problem in inserting selected items of checkbox inside repeaters, on button click. please help me. ASAP..........plz....Here is my code of aspx.

XML
<asp:Repeater ID="parent" runat="server" onitemcommand="parent_ItemCommand1">
                        <ItemTemplate>
                            <b>
                            <asp:CheckBox ID="services1" runat="server"  />
                            <asp:Label ID="lbl" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"BUSINESSUPLOAD_SERVICES_SERVICENAME") %>'   /></b><br />
                            <asp:Repeater ID="child" DataSource='<%#  ((DataRowView)Container.DataItem).CreateChildView("myrelation") %>'
                                runat="server">
                                <ItemTemplate>
                                <asp:CheckBox ID="subservices" runat="server" />
                                <asp:Label ID="lbl1" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "BUSINESSUPLOAD_SUBSERVICES_SUB_SERVICENAME")%>' /><br>
                                </ItemTemplate>
                            </asp:Repeater>
                        </ItemTemplate>


                    </asp:Repeater>
                     <asp:Button ID="button1" runat="server" Text="submit" CommandName="insert"
                        onclick="button1_Click" />


my code behind page.
C#
protected void parent_ItemCommand1(object source, RepeaterCommandEventArgs e)
 {
     Label lblserv = new Label();
     lblserv = (Label)e.Item.FindControl("lbl");
     string txt = lblserv.Text;

     if (e.CommandName == "insert") ;
     {
         SqlConnection con = new SqlConnection("Data Source=USER-PC\\SQLEXPRESS;Initial Catalog=MEDICALVOYAGER;Integrated Security=True");
         con.Open();
         SqlCommand cmd = new SqlCommand("insert into SERVICES_SELECTED(SERVICESNAME) values(@serv)", con);
         cmd.Parameters.AddWithValue("@serv", txt);

         cmd.ExecuteNonQuery();
         Response.Write("inserted");
         con.Close();
     }
 }
Posted
Updated 27-Jun-12 21:41pm
v2

1 solution

hi,
I have modified your and get the CheckBox here, if this checkbox is checked then it will save the values

C#
protected void parent_ItemCommand1(object source, RepeaterCommandEventArgs e)
   {
       Label lblserv = new Label();
       lblserv = (Label)e.Item.FindControl("lbl");
       string txt = lblserv.Text;

       //get the ckeckbox
       CheckBox chkServ = new CheckBox();
       chkServ = (CheckBox)e.Item.FindControl("services1");

       if (e.CommandName == "insert") ;
       {
           if (chkServ.Checked)
           {
               SqlConnection con = new SqlConnection("Data Source=USER-PC\\SQLEXPRESS;Initial Catalog=MEDICALVOYAGER;Integrated Security=True");
               con.Open();
               SqlCommand cmd = new SqlCommand("insert into SERVICES_SELECTED(SERVICESNAME) values(@serv)", con);
               cmd.Parameters.AddWithValue("@serv", txt);

               cmd.ExecuteNonQuery();
               Response.Write("inserted");
               con.Close();
           }
       }
   }

you should do same as for the child.
 
Share this answer
 
Comments
shabadiveda 28-Jun-12 5:48am    
thanx for the reply, but nothing is get inserted into the data base. any other solution? plz...
tanweer 28-Jun-12 6:00am    
try putting a break point here and see what wrong is going
shabadiveda 29-Jun-12 0:49am    
thnx for the reply, i am using the button out of the repeater if i check the checkbox inside the repeater and click the button for inserting the selected items will it b inserted? its my doubt
shabadiveda 29-Jun-12 0:50am    
i have put the break point for itemcommand, but its not firing.
shabadiveda 3-Jul-12 2:23am    
i solved by myself, using nested repeaters.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900