前台代码<%@ Page Language="C#" AutoEventWireup="true" CodeFile="CopyFrom.aspx.cs" Inherits="Management_CopyFrom" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Copy From</title>
<link rel="Stylesheet" href="../Css/mainpanel.css" />
</head>
<body>
<form id="form1" runat="server">
<div id="Main">
<div id="About_top">
<table cellpadding="0" cellspacing="0">
<tr>
<td align="center" style="background-image: url('../images/tab02.gif'); width: 139px;
height: 33px;">
Copy From
</td>
<td style="vertical-align: bottom; height: 1px;">
<div id="tab_bg">
<img src="../images/tab_bg.gif" alt="" style="width: 576px" /></div>
</td>
</tr>
<tr>
<td colspan="2" style="background-image: url('../images/search_bg.gif'); height: 33px;
text-align: left">
</td>
</tr>
</table>
</div>
<div>
<asp:GridView ID="GridViewPrivilege"
runat="server" AutoGenerateColumns="False" CellPadding="4"
ForeColor="#333333" GridLines="None" Width="100%" AllowPaging="True"
onpageindexchanging="GridViewPrivilege_PageIndexChanging"
PageSize="6" DataKeyNames="privilegeId">
<PagerSettings FirstPageText="第一页" LastPageText="最后一页" NextPageText="下一页"
PreviousPageText="上一页" Mode="NumericFirstLast" />
<FooterStyle BackColor="#cccccc" />
<RowStyle BackColor="#ffffff" />
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="cbPrivilege" runat="server" />
</ItemTemplate>
<HeaderStyle Height="24px" Width="5%" />
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:BoundField DataField="privilegeName" HeaderText="权限名称" >
<HeaderStyle Height="24px" />
<ItemStyle HorizontalAlign="Center" Width="10%" />
</asp:BoundField>
<asp:BoundField DataField="description" HeaderText="权限说明">
<ItemStyle HorizontalAlign="Center" Width="20%" />
</asp:BoundField>
</Columns>
<SelectedRowStyle Font-Bold="True" />
<HeaderStyle BackColor="#aaaaaa" Font-Bold="True" ForeColor="#ffffff" />
<AlternatingRowStyle BackColor="#eeeeee" />
</asp:GridView>
<asp:Label ID="lbDisplay" runat="server" Text=""></asp:Label><br />
<asp:Button ID="btnConfirm" runat="server" Text="应用"
onclick="btnConfirm_Click" />
<asp:Button ID="btnAdd" runat="server" Text="确定" />
<asp:Button ID="btnGiveUp" runat="server" Text="取消" />
</div>
</div>
</form>
</body>
</html>
后台代码protected void btnConfirm_Click(object sender, EventArgs e)
{
if (Request.QueryString["uid"] != null )
{
string userId = Request.QueryString["uid"];
int result = 0;
for (int i = 0; i < GridViewPrivilege.Rows.Count; i++)
{
//get select items
CheckBox cbox = (CheckBox)GridViewPrivilege.Rows[i].FindControl("cbPrivilege");
if (cbox.Checked == true)
{
string privilegeId = GridViewPrivilege.DataKeys[i].Value.ToString();
UserWithPrivilege userPriInfo = new UserWithPrivilege()
{
UserId=userId,
PrivilegeId=privilegeId,
PriGroupId=""
};
result = priMgt.AddUserPrivilege(userPriInfo);
}
}
if (result==0)
{
ClientScript.RegisterClientScriptBlock(this.GetType(), "no", "<script>alert('Add Failed!')</script>");
}
else
{
ClientScript.RegisterClientScriptBlock(this.GetType(), "yes", "<script>alert('Add successful!')</script>");
}
}
}
为什么当我选中后,CheckBox cbox = (CheckBox)GridViewPrivilege.Rows[i].FindControl("cbPrivilege");cbox.Checked的值还是false。。
<head runat="server">
<title>Copy From</title>
<link rel="Stylesheet" href="../Css/mainpanel.css" />
</head>
<body>
<form id="form1" runat="server">
<div id="Main">
<div id="About_top">
<table cellpadding="0" cellspacing="0">
<tr>
<td align="center" style="background-image: url('../images/tab02.gif'); width: 139px;
height: 33px;">
Copy From
</td>
<td style="vertical-align: bottom; height: 1px;">
<div id="tab_bg">
<img src="../images/tab_bg.gif" alt="" style="width: 576px" /></div>
</td>
</tr>
<tr>
<td colspan="2" style="background-image: url('../images/search_bg.gif'); height: 33px;
text-align: left">
</td>
</tr>
</table>
</div>
<div>
<asp:GridView ID="GridViewPrivilege"
runat="server" AutoGenerateColumns="False" CellPadding="4"
ForeColor="#333333" GridLines="None" Width="100%" AllowPaging="True"
onpageindexchanging="GridViewPrivilege_PageIndexChanging"
PageSize="6" DataKeyNames="privilegeId">
<PagerSettings FirstPageText="第一页" LastPageText="最后一页" NextPageText="下一页"
PreviousPageText="上一页" Mode="NumericFirstLast" />
<FooterStyle BackColor="#cccccc" />
<RowStyle BackColor="#ffffff" />
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="cbPrivilege" runat="server" />
</ItemTemplate>
<HeaderStyle Height="24px" Width="5%" />
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:BoundField DataField="privilegeName" HeaderText="权限名称" >
<HeaderStyle Height="24px" />
<ItemStyle HorizontalAlign="Center" Width="10%" />
</asp:BoundField>
<asp:BoundField DataField="description" HeaderText="权限说明">
<ItemStyle HorizontalAlign="Center" Width="20%" />
</asp:BoundField>
</Columns>
<SelectedRowStyle Font-Bold="True" />
<HeaderStyle BackColor="#aaaaaa" Font-Bold="True" ForeColor="#ffffff" />
<AlternatingRowStyle BackColor="#eeeeee" />
</asp:GridView>
<asp:Label ID="lbDisplay" runat="server" Text=""></asp:Label><br />
<asp:Button ID="btnConfirm" runat="server" Text="应用"
onclick="btnConfirm_Click" />
<asp:Button ID="btnAdd" runat="server" Text="确定" />
<asp:Button ID="btnGiveUp" runat="server" Text="取消" />
</div>
</div>
</form>
</body>
</html>
后台代码protected void btnConfirm_Click(object sender, EventArgs e)
{
if (Request.QueryString["uid"] != null )
{
string userId = Request.QueryString["uid"];
int result = 0;
for (int i = 0; i < GridViewPrivilege.Rows.Count; i++)
{
//get select items
CheckBox cbox = (CheckBox)GridViewPrivilege.Rows[i].FindControl("cbPrivilege");
if (cbox.Checked == true)
{
string privilegeId = GridViewPrivilege.DataKeys[i].Value.ToString();
UserWithPrivilege userPriInfo = new UserWithPrivilege()
{
UserId=userId,
PrivilegeId=privilegeId,
PriGroupId=""
};
result = priMgt.AddUserPrivilege(userPriInfo);
}
}
if (result==0)
{
ClientScript.RegisterClientScriptBlock(this.GetType(), "no", "<script>alert('Add Failed!')</script>");
}
else
{
ClientScript.RegisterClientScriptBlock(this.GetType(), "yes", "<script>alert('Add successful!')</script>");
}
}
}
为什么当我选中后,CheckBox cbox = (CheckBox)GridViewPrivilege.Rows[i].FindControl("cbPrivilege");cbox.Checked的值还是false。。
这个判断就是null值了。所以你应该在Page_Load事件中页面第一次加载时用ViewState保存传递过来的uid值,然后使用即可。
public void Page_Load(object sender,EventArgs e)
{
if(!IsPostBack)
{
ViewState["uid"] = Request.QueryString["uid"];
}
}protected void btnConfirm_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(ViewState["uid"].ToString()) )
{
string userId = Request.QueryString["uid"];
int result = 0;
for (int i = 0; i < GridViewPrivilege.Rows.Count; i++)
{
//get select items
CheckBox cbox = (CheckBox)GridViewPrivilege.Rows[i].FindControl("cbPrivilege");
if (cbox.Checked == true)
{
string privilegeId = GridViewPrivilege.DataKeys[i].Value.ToString();
UserWithPrivilege userPriInfo = new UserWithPrivilege()
{
UserId=userId,
PrivilegeId=privilegeId,
PriGroupId=""
};
result = priMgt.AddUserPrivilege(userPriInfo);
}
}
if (result==0)
{
ClientScript.RegisterClientScriptBlock(this.GetType(), "no", "<script>alert('Add Failed!')</script>");
}
else
{
ClientScript.RegisterClientScriptBlock(this.GetType(), "yes", "<script>alert('Add successful!')</script>");
}
}
}
有值的而且GridViewPrivilege.Rows.Count也可以得到 也可以找到控件,但是不知道为什么状态一直是false.
{
string privilegeId = GridViewPrivilege.DataKeys[i].Value.ToString();
UserWithPrivilege userPriInfo = new UserWithPrivilege()
{
UserId=userId,
PrivilegeId=privilegeId,
PriGroupId=""
};
result = priMgt.AddUserPrivilege(userPriInfo);
}
值是false 。if里面都不执行的啊。
有个差不多的页面,调试很正常 CheckBox cbox = (CheckBox)GridViewPrivilege.Rows[i].FindControl("cbPrivilege")可以得到true,但是这里不知道为什么- -、也可以得到gridview的长度,但是一直都是false的状态.
{
//把你的代码放在这里面
}
{
//第一次加载进行的操作
}如果还有问题,看看是否没有加载状态视图。 或者再看看autopost属性没有设置。
已经好了。谢谢大家了。