首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

大家好,好长时间不见了!一个特急的有关问题,再线等,请帮忙

2011-12-22 
大家好,好长时间不见了!!一个特急的问题,再线等,请帮忙描述:我在GridView中的第一列中添加了checkbox,要实

大家好,好长时间不见了!!一个特急的问题,再线等,请帮忙
描述:
我在GridView中的第一列中添加了checkbox,要实现,当点选择某列的checkbox后,要捕捉到checkbox的状态,并取得相应行的其它数据信息。
问题:
但是,我在button事件中总是捕捉不到checkbox   的选中状态,能找见gridview中的checkbox,但是他的状态总是为false.

代码:.aspx:
        
                              <asp:GridView   ID= "GridView1 "   runat= "server "   Width= "534px "   AutoGenerateColumns= "False "   OnSelectedIndexChanged= "GridView1_SelectedIndexChanged ">
                                      <Columns>
                                              <asp:TemplateField>
                                                      <EditItemTemplate>
                                                              <asp:CheckBox   ID= "CheckBox1 "   runat= "server "   />
                                                      </EditItemTemplate>
                                                      <ItemTemplate>
                                                              <asp:CheckBox   ID= "CheckBox1 "   runat= "server "   />
                                                      </ItemTemplate>
                                              </asp:TemplateField>
                                              <asp:BoundField   DataField= "roleInfoID "   HeaderText= "角色代码 "   />
                                              <asp:BoundField   DataField= "roleInfoName "   HeaderText= "角色名称 "   />
                                      </Columns>
                              </asp:GridView>
                              &nbsp;&nbsp; <asp:CheckBox   ID= "CheckBox2 "   runat= "server "   OnCheckedChanged= "CheckBox2_CheckedChanged "


                                      Text= "全选 "   />
                              &nbsp;
                              <asp:Button   ID= "Button1 "   runat= "server "   Text= "提交 "   OnClick= "Button1_Click "   Width= "58px "/> &nbsp;
                      </td> </tr>  


代码.aspx.cs:
  protected   void   Button1_Click(object   sender,   EventArgs   e)
        {
                int   k   =   0;
                string   Message   =   " ";
                  string   url   =   this.PageRoot   +   "UserInformation/RoleToUser.aspx ";
                SysUserRoleCVO   sysUserRoleCVO   =   new   SysUserRoleCVO();
                CheckBox   cbox;

                //   进行用户和角色的关联string   nc=this.Request.Form.Get( "CheckBox1 ");
                for   (   k   =   0;   k   <   GridView1.Rows.Count;   k++)
                {
                          cbox   =   (CheckBox)GridView1.Rows[k].FindControl( "CheckBox1 ");
                          //((CheckBox)GridView1.Rows[i].FindControl( "CheckBox1 ")).Checked
                        if   (cbox.Checked   ==   true)
                        {
                        sysUserRoleCVO.RoleInfoID=   int.Parse   (((TextBox)(GridView1.Rows[k].Cells[1].Controls[0])).Text.ToString().Trim());
                        sysUserRoleCVO.UserCode   =sysUserInfoCVO[this.DropDownList1   .SelectedIndex].UserCode   ;

                        Message   +=   roleToUser.Add(sysUserRoleCVO);
                        }
                }

                foreach   (GridViewRow   row   in   GridView1.Rows)
                {
                        CheckBox   cb   =   (CheckBox)row.FindControl( "CheckBox1 ");

                        string   id1   =   this.GridView1.DataKeys[row.RowIndex].Values[1].ToString();


                        if   (cb.Checked   ==   true)
                        {
                                string   id   =   this.GridView1.DataKeys[row.RowIndex].Values[1].ToString();
                        }
                }
                Response.Redirect(this.PageRoot   +   "common/TimeoutErr.aspx?Message= "   +   Message   +   "&url= "   +   url);
        }

定义checkbox:CheckBox   cbox;

for循环:
for   (   k   =   0;   k   <   GridView1.Rows.Count;   k++)
                {
                          cbox   =   (CheckBox)GridView1.Rows[k].FindControl( "CheckBox1 ");
                          //((CheckBox)GridView1.Rows[i].FindControl( "CheckBox1 ")).Checked
                        if   (cbox.Checked   ==   true)
                        {
其中:cbox.Checked   纵使显示为false


谢谢,急等!!

[解决办法]
当点选择某列的checkbox后,要捕捉到checkbox的状态,并取得相应行的其它数据信息。

以前做过的例程
private void DataGrid1_ItemCreated(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if((e.Item.ItemType==ListItemType.Item)|| e.Item.ItemType==ListItemType.AlternatingItem))
{
CheckBox cb_Department =(CheckBox)e.Item.FindControl( "cb_Department ");
cb_Department.AutoPostBack=true;
cb_Department.CheckedChanged+=new EventHandler(cb_Department_CheckedChanged);
}

}
[解决办法]
我把我的代码贴出来,供你参考:
<%@ Page Language= "C# " MasterPageFile= "~/Admin/DefaultMaster.master " AutoEventWireup= "true " CodeFile= "UserBooking.aspx.cs " Inherits= "Admin_UserBooking " Title= "Untitled Page " %>
<asp:Content ID= "Content1 " ContentPlaceHolderID= "ContentPlaceHolder1 " Runat= "Server ">
<table id= "table17 " bgcolor= "#008080 " border= "0 " height= "33 " width= "100% ">
<tr>
<td width= "104 ">
<b> 检查预约 </b> </td>
<td>
</td>
</tr>
</table>
<table id= "table19 " border= "1 " style= "border-collapse: collapse " width= "100% ">
<tr>
<td style= "height: 20px ">
预约列表 </td>
</tr>
</table>
<asp:GridView ID= "gvData " runat= "server " AutoGenerateColumns= "False " Width= "100% ">
<Columns>
<asp:TemplateField HeaderText= "选择 ">


<ItemTemplate>
<asp:CheckBox ID= "cbChoose " runat= "server " />
<asp:HiddenField ID= "hfID " runat= "server " />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField HeaderText= "日期 " DataField= "CreateDate " />
<asp:BoundField HeaderText= "检查项目 " DataField= "CheckItem " />
<asp:BoundField HeaderText= "状态 " DataField= "AuditingState " />
<asp:TemplateField HeaderText= "操作 ">
<ItemTemplate>
<asp:HyperLink ID= "hkView " runat= "server "> 查看 </asp:HyperLink> | <asp:HyperLink ID= "hkEdit "
runat= "server "> 审核 </asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EmptyDataTemplate>
<strong>
<table>
<tr>
<td width= "100% ">
没有记录。 </td>
</tr>
</table>
</strong>
</EmptyDataTemplate>
<PagerTemplate>
asdfasdfasdf
</PagerTemplate>
</asp:GridView>
<table border= "0 " style= "width: 100% ">
<tr>
<td nowrap= "nowrap ">
<span style= "font-size: 10pt "> 选择: <asp:LinkButton ID= "btnAll " runat= "server " CommandName= "All "
OnCommand= "Choose "> 全选 </asp:LinkButton> | <asp:LinkButton ID= "btnReverse " runat= "server "
CommandName= "reverse " OnCommand= "Choose "> 反选 </asp:LinkButton> | <asp:LinkButton ID= "btnDelete "
runat= "server " OnClick= "btnDelete_Click " OnClientClick= "return DeleteConfirm() "> 删除 </asp:LinkButton> </span> </td>
<td nowrap= "nowrap " style= "color: #000000 ">
<span style= "font-size: 10pt "> [ <asp:Literal ID= "ltCurrentPage " runat= "server "> </asp:Literal> /
<asp:Literal ID= "ltPageCount " runat= "server "> </asp:Literal> ]
[ <asp:LinkButton ID= "btnFirstPage " runat= "server " CommandName= "First " OnCommand= "PageClick "> 首页 </asp:LinkButton> </span> <span
style= "font-size: 10pt "> </span> <span style= "font-size: 10pt "> ] [ <asp:LinkButton ID= "btnPrePage "
runat= "server " CommandName= "Pre "> 上一页 </asp:LinkButton> </span> <span style= "font-size: 10pt "> </span> <span
style= "font-size: 10pt "> ] [ <asp:LinkButton ID= "btnNextPage " runat= "server " CommandName= "Next "> 下一页 </asp:LinkButton> </span> <span
style= "font-size: 10pt "> </span> <span style= "font-size: 10pt "> ] [ <asp:LinkButton ID= "btnEndPage "


runat= "server " CommandName= "End "> 末页 </asp:LinkButton> </span> <span style= "font-size: 10pt "> </span> <span
style= "font-size: 10pt "> ] 转到 <asp:TextBox ID= "txtNumberPage " runat= "server " Width= "21px "> </asp:TextBox>
<asp:Button ID= "btnGo " runat= "server " CommandName= "Number " OnCommand= "PageClick "
Text= "go " /> </span> </td>
</tr>
</table>
</asp:Content>

代码部分:
/// <summary>
/// 选择
/// </summary>
/// <param name= "sender "> </param>
/// <param name= "e "> </param>
protected void Choose(object sender, CommandEventArgs e)
{
switch (e.CommandName)
{
case "All ":
foreach (GridViewRow row in gvData.Rows)
{
CheckBox cb = (CheckBox)row.FindControl( "cbChoose ");
if (cb != null)
{
cb.Checked = true;
}
}
break;
case "reverse ":
foreach (GridViewRow row in gvData.Rows)
{
CheckBox cb = (CheckBox)row.FindControl( "cbChoose ");
if (cb != null)
{
cb.Checked = !cb.Checked;
}
}
break;
}
}

热点排行