检查gridview内的复选框是否已选中? [英] check checkbox inside gridview either it is checked or not?

查看:70
本文介绍了检查gridview内的复选框是否已选中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我有gridview,其中包含复选框作为templatefield。我已经尝试过如此努力,但我仍然无法获得所需的结果,即如果选中复选框,则执行action-1否则执行action-2,但每次执行action-2.Below是我的代码我需要你的帮助。



Gridview代码:< br $>


< asp:GridView ID =   final runat =   server AutoGenerateColumns =   False; >  
<柱体和GT;
< asp:BoundField DataField = name HeaderText = 员工姓名 SortExpression = 日期 />
< asp:BoundField DataField = ldate HeaderText = 离开日期 SortExpression = ldate
/>
< asp:TemplateField HeaderText = 半/全 >
< ItemTemplate>
< asp:RadioButtonList ID = RadioButtonList1 runat = server >
< asp:ListItem Enabled = true值= 一半>一半< / asp:ListItem >
< asp:ListItem已启用= true值= 完整>完整< / asp:ListItem >
< / asp:RadioButtonList >
< / ItemTemplate >
< / asp:TemplateField >
< asp:TemplateField HeaderText = < span class =code-string> 批准 >
<&的ItemTemplate GT;
< asp:CheckBox ID = CheckBox1 runat = server />
< / ItemTemplate >
< / asp:TemplateField >
< / >
< / asp:GridView >







代码我已检查过收音机和复选框:



 DataTable dtable =  new  DataTable(); 
dtable.Columns.Add( new DataColumn( 日期 typeof (DateTime)));
dtable.Columns.Add( new DataColumn( 半/完全 typeof float )));
dtable.Columns.Add( new DataColumn( 状态 typeof string )));
会话[ 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 == 一半
{
// 执行操作-1
}
其他
{
// < span class =code-comment>执行操作-1
}
}
其他
{
执行行动-2
}

}





每次它进入了最后的其他...为什么?Anyhelp真的很有意思,因为我正在尝试这个以来的一周。

解决方案

嘿Rohan,



除了以下内容之外,您所做的事情是正确的。

  foreach (GridView行gvrow  in  final.Rows)
{
dtable =(DataTable)Session [ dt];
CheckBox chk =(CheckBox)gvrow.FindControl( CheckBox1);
如果(chk!= null & chk.Checked)
{

RadioButtonList rButton =(RadioButtonList)gvrow.FindControl( RadioButtonList1);
if (rButton.SelectedValue == 一半
{
// 执行操作-1
}
其他
{
// < span class =code-comment>执行操作-1
}
}
其他
{
执行操作-2
}
}





这表示您的复选框不在所有选中。请填写网格并检查代码部分中的网格填充方法

  if (!IsPostBack)
{
// 使用复选框填充网格的代码
}







好运


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:

<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:

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.

解决方案

Hey Rohan,

What ever the things you have done is correct except the following thing.

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

if(!IsPostBack)
{
    //Code to fill Grid with check box
}




GoodLuck


这篇关于检查gridview内的复选框是否已选中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆