嵌套在Repeater里DropDownList怎么把选中的值保存下来
2个回答
展开全部
如果是想获取选中值一楼的方法就可以,如果是想重新绑定时保存原来的值,可以在绑定前先将所有选中值保存在全局集合或哈希表中,然后重写页面的PreRender事件处理方法,在里面将之前保存的选中值重新设置给DropDownList,主要的代码如下:
//重新绑定数据源的按钮事件
protected void btn_Click(object sender, EventArgs e)
{
foreach (RepeaterItem item in rpt.Items)
{
DropDownList ddl = item.FindControl("ddl") as DropDownList;
if (ddl != null)
{
lt.Add(ddl.SelectedItem.Value);
}
}
rpt.DataSource = new UserInfo().GetOutColor();
rpt.DataBind();
}
//存储选中值的全局集合
List<string> lt = new List<string>();
//根据选中值设置Dropdownlist选择项
private void SelectItem( DropDownList ddl,string value)
{
ddl.SelectedIndex = ddl.Items.IndexOf(ddl.Items.FindByValue(value));
}
//页面PreRender事件处理方法
protected override void OnPreRender(EventArgs e)
{
int count = Math.Min(rpt.Items.Count, lt.Count);
for (int i = 0; i < count; i++)
{
DropDownList ddl = rpt.Items[i].FindControl("ddl") as DropDownList;
SelectItem(ddl, lt[i]);
}
base.OnPreRender(e);
}
关于PreRender事件,你如果不懂可以去网上搜资料看下
//重新绑定数据源的按钮事件
protected void btn_Click(object sender, EventArgs e)
{
foreach (RepeaterItem item in rpt.Items)
{
DropDownList ddl = item.FindControl("ddl") as DropDownList;
if (ddl != null)
{
lt.Add(ddl.SelectedItem.Value);
}
}
rpt.DataSource = new UserInfo().GetOutColor();
rpt.DataBind();
}
//存储选中值的全局集合
List<string> lt = new List<string>();
//根据选中值设置Dropdownlist选择项
private void SelectItem( DropDownList ddl,string value)
{
ddl.SelectedIndex = ddl.Items.IndexOf(ddl.Items.FindByValue(value));
}
//页面PreRender事件处理方法
protected override void OnPreRender(EventArgs e)
{
int count = Math.Min(rpt.Items.Count, lt.Count);
for (int i = 0; i < count; i++)
{
DropDownList ddl = rpt.Items[i].FindControl("ddl") as DropDownList;
SelectItem(ddl, lt[i]);
}
base.OnPreRender(e);
}
关于PreRender事件,你如果不懂可以去网上搜资料看下
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2012-04-10
展开全部
遍历Repeater 的items
foreach item in repeaterId.items
{
DropDownList ddl= item.FindControl("DropDownListID") as DropDownList;
if(ddl!=null)
{
string selectedVal=ddl.SelectedValue;
}
}
foreach item in repeaterId.items
{
DropDownList ddl= item.FindControl("DropDownListID") as DropDownList;
if(ddl!=null)
{
string selectedVal=ddl.SelectedValue;
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询