Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
3.18/5 (3 votes)
See more:
Hi everyone,
I have gridview which contains the checkbox as templatefield.I have tried so hard but i am still unable to get the desired results,that is if check box is checked,than perform action-1 otherwise perform action-2,but everytime it is performing action-2.Below is my code i need little help from your side.

Gridview code:

C#
<asp:GridView ID="final" runat="server" AutoGenerateColumns="False";>
        <Columns>
<asp:BoundField DataField="name" HeaderText="Employee Name" SortExpression="date" />
<asp:BoundField DataField="ldate" HeaderText="Date Of Leave" SortExpression="ldate"
                   />
<asp:TemplateField HeaderText="Half/Full">
   <ItemTemplate>
            <asp:RadioButtonList ID="RadioButtonList1" runat="server">
                            <asp:ListItem Enabled="true" Value="Half">Half</asp:ListItem>
                            <asp:ListItem Enabled="true" Value="Full">Full</asp:ListItem>
           </asp:RadioButtonList>
   </ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Approve">
    <ItemTemplate>
     <asp:CheckBox ID="CheckBox1" runat="server" />
    </ItemTemplate>
</asp:TemplateField>
        </Columns>
</asp:GridView>



code i have made to check the radio and checkbox:

C#
DataTable dtable = new DataTable();
dtable.Columns.Add(new DataColumn("Date", typeof(DateTime)));
dtable.Columns.Add(new DataColumn("Half/Full", typeof(float)));
dtable.Columns.Add(new DataColumn("Status", typeof(string)));
Session["dt"] = dtable;
SqlConnection conn = new SqlConnection();
conn.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["leave"].ConnectionString;
conn.Open();
foreach (GridViewRow gvrow in final.Rows)
{
     dtable=(DataTable)Session["dt"];
     CheckBox chk = (CheckBox)gvrow.FindControl("CheckBox1");
     if (chk != null & chk.Checked)
     {
          RadioButtonList rButton = (RadioButtonList)gvrow.FindControl("RadioButtonList1");
          if (rButton.SelectedValue == "Half")
          {
               //perform action-1
          }
          else
          {
              //perform action-1
          }
     }
  else
     {
          perform action-2
     }
}


everytime its going into the last else...why?Anyhelp would be realy realy appreciated as i am trying for this since last one week.
Posted
Updated 24-Mar-13 18:25pm
v3
Comments
[no name] 25-Mar-13 0:43am    
Try to use
rButton.SelectedItem.Value or rButton.SelectedIndex .
vinodkumarnie 25-Mar-13 0:47am    
What exactly you are doing here..? Are you written this code in RowDataBound or RowCommand even or .. ...?
Thanks7872 25-Mar-13 0:50am    
@anupan singh:its not working for chk!=null && chk.checked than its not issue for rbutton.My question is how to check checkbox is checked or not?
Thanks7872 25-Mar-13 0:52am    
@vinodkumarnie:I have one button below the gridview and on the click of that button i am performing some operations based on checkbox value,if its checked than checking for radiobutton,but the code is not working for even checkbox in the first place.

1 solution

Hey Rohan,

What ever the things you have done is correct except the following thing.
C#
foreach (GridViewRow gvrow in final.Rows)
{
     dtable=(DataTable)Session["dt"];
     CheckBox chk = (CheckBox)gvrow.FindControl("CheckBox1");
     if (chk != null & chk.Checked)
     {
          RadioButtonList rButton = (RadioButtonList)gvrow.FindControl("RadioButtonList1");
          if (rButton.SelectedValue == "Half")
          {
               //perform action-1
          }
          else
          {
              //perform action-1
          }
     }
  else
     {
          perform action-2
     }
}


This shows your check box is not at all getting selected.Please fill the grid and check the grid fill method in the code section
C#
if(!IsPostBack)
{
    //Code to fill Grid with check box
}



GoodLuck
 
Share this answer
 
Comments
Thanks7872 28-Mar-13 5:51am    
i solved this somedays ago.The problem was the same that you suggested and now its working fine.As your solution is closely related to my question,i upvoted the same.Regards.

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