gridview下获取checkboxlist的值
作者:admin 日期:2007-07-19
本来估计很简单的问题,却卡壳了。一开始用的是datalist,在确定程序没错误的情况下,居然获取不到在gridview中的值,郁闷了一下午,终于想开了,换gridview来试试,结果一下子就OK了。
起初使用datalist,在datalist中嵌套checkboxlist,后台代码我是这样写的
protected void AddRight()
{
int roleId = Convert.ToInt32(Request.QueryString["RoleId"]);
for (int i = 0; i <= dlstModuleRight.Items.Count - 1; i++)
{
CheckBoxList chkRights = (CheckBoxList)dlstModuleRight.Items[i].FindControl("chkRoleRight");
//此句可以攻取ModuleId
Label lblId = (Label)dlstModuleRight.Items[i].FindControl("lblModuleId");
int ModuleId= Convert.ToInt32(lblId.Text);
for(int j=0;j {
if (chkRights.Items[j].Selected==true)
{
Rights objRights = new Rights();
objRights.Add(roleId,ModuleId, chkRights.Items[j].Value);
}
Response.Write(chkRights.Text);
}
可是,却取不到的值的郁闷,我又问了其它人,好像都没什么法子。这样不行,那只能再试着另一种写法了,我又用了以下代码去检验一下:
protected void AddRight()
{
int roleId = Convert.ToInt32(Request.QueryString["RoleId"]);
for (int i = 0; i <= dlstModuleRight.Items.Count - 1; i++)
{
if (this.dlstModuleRight.Items[i].ItemType == ListItemType.AlternatingItem || this.dlstModuleRight.Items[i].ItemType == ListItemType.Item)
{
Label lblId = (Label)dlstModuleRight.Items[i].FindControl("lblModuleId");
CheckBoxList chkRights = (CheckBoxList)dlstModuleRight.Items[i].FindControl("chkRights");
CheckBox chk = (CheckBox)dlstModuleRight.Items[i].FindControl("chk");
if (lblId != null && chkRights != null)
{
int iModuleID = int.Parse(lblId.Text);
int iRightValue = 0;foreach(ListItem li in chkRights.Items)
{
if(li.Selected)
{
iRightValue += int.Parse(li.Value);
Response.Write(iRightValue.ToString());
}
}
}
}
结果还是没有搞到checkboxlist里面的选择值,很是不爽,但又实在找不到错,希望各位帮忙指正一下:)
百思不得其解后,我索性把datalist撤了,换成gridview,这下爽了,一下子就搞掂了,呵呵[em13][em13]
代码也不复杂
protected void imgbtSure_Click(object sender, ImageClickEventArgs e)
{
int RoleId = Convert.ToInt32(Request.QueryString["RoleId"]);
Rights objRights = new Rights();
for (int i = 0; i < this.gdvRoleRight.Rows.Count; i++)
{
CheckBoxList chkRight = (CheckBoxList)this.gdvRoleRight.Rows[i].Cells[3].FindControl("chkRight");
int ModuleId = Convert.ToInt32(this.gdvRoleRight.Rows[i].Cells[0].Text.Trim());
foreach (ListItem li in chkRight.Items)
{
if (li.Selected)
{
Response.Write(li.Text + "
");
objRights.Add(RoleId, ModuleId, li.Value);
}
}
}
}
写在这里,供大家参与,同时欢迎大家相互交流:)
[em02][em02]
起初使用datalist,在datalist中嵌套checkboxlist,后台代码我是这样写的
protected void AddRight()
{
int roleId = Convert.ToInt32(Request.QueryString["RoleId"]);
for (int i = 0; i <= dlstModuleRight.Items.Count - 1; i++)
{
CheckBoxList chkRights = (CheckBoxList)dlstModuleRight.Items[i].FindControl("chkRoleRight");
//此句可以攻取ModuleId
Label lblId = (Label)dlstModuleRight.Items[i].FindControl("lblModuleId");
int ModuleId= Convert.ToInt32(lblId.Text);
for(int j=0;j
if (chkRights.Items[j].Selected==true)
{
Rights objRights = new Rights();
objRights.Add(roleId,ModuleId, chkRights.Items[j].Value);
}
Response.Write(chkRights.Text);
}
可是,却取不到的值的郁闷,我又问了其它人,好像都没什么法子。这样不行,那只能再试着另一种写法了,我又用了以下代码去检验一下:
protected void AddRight()
{
int roleId = Convert.ToInt32(Request.QueryString["RoleId"]);
for (int i = 0; i <= dlstModuleRight.Items.Count - 1; i++)
{
if (this.dlstModuleRight.Items[i].ItemType == ListItemType.AlternatingItem || this.dlstModuleRight.Items[i].ItemType == ListItemType.Item)
{
Label lblId = (Label)dlstModuleRight.Items[i].FindControl("lblModuleId");
CheckBoxList chkRights = (CheckBoxList)dlstModuleRight.Items[i].FindControl("chkRights");
CheckBox chk = (CheckBox)dlstModuleRight.Items[i].FindControl("chk");
if (lblId != null && chkRights != null)
{
int iModuleID = int.Parse(lblId.Text);
int iRightValue = 0;foreach(ListItem li in chkRights.Items)
{
if(li.Selected)
{
iRightValue += int.Parse(li.Value);
Response.Write(iRightValue.ToString());
}
}
}
}
结果还是没有搞到checkboxlist里面的选择值,很是不爽,但又实在找不到错,希望各位帮忙指正一下:)
百思不得其解后,我索性把datalist撤了,换成gridview,这下爽了,一下子就搞掂了,呵呵[em13][em13]
代码也不复杂
protected void imgbtSure_Click(object sender, ImageClickEventArgs e)
{
int RoleId = Convert.ToInt32(Request.QueryString["RoleId"]);
Rights objRights = new Rights();
for (int i = 0; i < this.gdvRoleRight.Rows.Count; i++)
{
CheckBoxList chkRight = (CheckBoxList)this.gdvRoleRight.Rows[i].Cells[3].FindControl("chkRight");
int ModuleId = Convert.ToInt32(this.gdvRoleRight.Rows[i].Cells[0].Text.Trim());
foreach (ListItem li in chkRight.Items)
{
if (li.Selected)
{
Response.Write(li.Text + "
");
objRights.Add(RoleId, ModuleId, li.Value);
}
}
}
}
写在这里,供大家参与,同时欢迎大家相互交流:)
[em02][em02]
评论: 0 | 引用: 0 | 查看次数: 24467
发表评论