我在repeater中嵌套了一个checkboxlist控件,现在我要取得checkboxlist的选中值,该如何实现,大家帮忙看看。非常感谢。相关代码:前台:
<asp:Repeater ID="repposition" runat="server"
onitemdatabound="repposition_ItemDataBound">
<ItemTemplate>
<asp:HiddenField ID="hidshow" runat="server" Value='<%#Eval("showID") %>' />
<span style="border: 0px solid #325FB5; background-color: #F1F6FC; font-size:16px; font-weight:bold;">
<%#Eval("showExplain")%></span>
<asp:CheckBoxList ID="chkPostion" runat="server" RepeatColumns="4" RepeatDirection="Horizontal" AutoPostBack="true"
Width="623px">
</asp:CheckBoxList>
</ItemTemplate>
</asp:Repeater>
后台:private void BindFpoistion()
{
WT.BLL.ShowPosition bll_sp = new WT.BLL.ShowPosition();
repposition.DataSource = bll_sp.GetList(" fatherID=0");
repposition.DataBind();
}
protected void repposition_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
WT.BLL.ShowPosition bll_sp = new WT.BLL.ShowPosition();
CheckBoxList cbl;
int id;
if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
{
cbl = (CheckBoxList)e.Item.FindControl("chkPostion");
id = Convert.ToInt32(((HiddenField)e.Item.FindControl("hidshow")).Value);
cbl.DataSource = bll_sp.GetList(" fatherID=" + id);
cbl.DataValueField = "showID";
cbl.DataTextField = "showExplain";
cbl.DataBind();
string postion = "";
for (int y = 0; y < cbl.Items.Count; y++)
{
if (cbl.Items[y].Selected)
{
postion += cbl.Items[y].Value + ",";
}
}
ViewState["position"] = postion;
}
} 红色部分是我尝试弄的,不好使,大家看一下,推荐一下实现方法。非常感谢!
<asp:Repeater ID="repposition" runat="server"
onitemdatabound="repposition_ItemDataBound">
<ItemTemplate>
<asp:HiddenField ID="hidshow" runat="server" Value='<%#Eval("showID") %>' />
<span style="border: 0px solid #325FB5; background-color: #F1F6FC; font-size:16px; font-weight:bold;">
<%#Eval("showExplain")%></span>
<asp:CheckBoxList ID="chkPostion" runat="server" RepeatColumns="4" RepeatDirection="Horizontal" AutoPostBack="true"
Width="623px">
</asp:CheckBoxList>
</ItemTemplate>
</asp:Repeater>
后台:private void BindFpoistion()
{
WT.BLL.ShowPosition bll_sp = new WT.BLL.ShowPosition();
repposition.DataSource = bll_sp.GetList(" fatherID=0");
repposition.DataBind();
}
protected void repposition_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
WT.BLL.ShowPosition bll_sp = new WT.BLL.ShowPosition();
CheckBoxList cbl;
int id;
if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
{
cbl = (CheckBoxList)e.Item.FindControl("chkPostion");
id = Convert.ToInt32(((HiddenField)e.Item.FindControl("hidshow")).Value);
cbl.DataSource = bll_sp.GetList(" fatherID=" + id);
cbl.DataValueField = "showID";
cbl.DataTextField = "showExplain";
cbl.DataBind();
string postion = "";
for (int y = 0; y < cbl.Items.Count; y++)
{
if (cbl.Items[y].Selected)
{
postion += cbl.Items[y].Value + ",";
}
}
ViewState["position"] = postion;
}
} 红色部分是我尝试弄的,不好使,大家看一下,推荐一下实现方法。非常感谢!
你想它什么时候取值哇?
CheckBoxList cbl = (CheckBoxList)repposition.Items[index].FindControl("chkPostion");
index是你要找的第n行,找到后就可以通过循环来判断是否Selected
前台:
<asp:Repeater ID="repposition" runat="server"
onitemdatabound="repposition_ItemDataBound" >
<ItemTemplate>
<asp:HiddenField ID="hidshow" runat="server" Value='<%#Eval("showID") %>' />
<span style="border: 0px solid #325FB5; background-color: #F1F6FC; font-size:16px; font-weight:bold;">
<%#Eval("showExplain")%></span>
<asp:CheckBoxList ID="chkPostion" runat="server" OnSelectedIndexChanged="chkSelectJudge" RepeatColumns="4" RepeatDirection="Horizontal"
Width="623px">
</asp:CheckBoxList>
</ItemTemplate>
</asp:Repeater>
后台:
protected void repposition_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
WT.BLL.ShowPosition bll_sp = new WT.BLL.ShowPosition();
CheckBoxList cbl;
int id;
if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
{
cbl = (CheckBoxList)e.Item.FindControl("chkPostion");
id = Convert.ToInt32(((HiddenField)e.Item.FindControl("hidshow")).Value);
cbl.DataSource = bll_sp.GetList(" fatherID=" + id);
cbl.DataValueField = "showID";
cbl.DataTextField = "showExplain";
cbl.DataBind();
}
}
protected void chkSelectJudge(object sender, EventArgs e)
{
CheckBoxList chkl; //= new CheckBoxList();
chkl = (CheckBoxList)sender;
string postion = "";
for (int i = 0; i < chkl.Items.Count; i++)
{
if (chkl.Items[i].Selected)
{
postion += chkl.Items[i].Value + ",";
}
}
ViewState["position"] = postion;
}蓝色部分是我在checkboxlist中加了个事件,然后在后台(红色部分)编写这个事件的相应方法,测试后的结果是,只能取到最后一级的子级复选框,之前的一级下的复选框取不到值,调试一下结果发现第一次进来时,chkl.Items.Count的大小只为一级下的子级复选框的数量而不是全部子级复选框的数量。他每次都要重新进来获取相应一级下的复选框数量。 各位帮忙看一下解决方法,非常感谢!
这是DataBind事件啊
应该有一个 按钮来取值吧
不可能绑定的时候就取值吧
这个东西呢最好用JS 来写 然后把值赋到HiddenField中 后台通过这个来 得到
或者 你在事件中找到此Repeater 然后再通过E.XXX来得到CheckBox.然后For 再进行判断..
后台加上:protected void Button1_Click(object sender, EventArgs e)
{
ViewState["position"] = null; for (int index = 0; index < repposition.Items.Count; index++)
{
CheckBoxList chkl = (CheckBoxList)repposition.Items[index].FindControl("chkPostion"); string postion = "";
for (int i = 0; i < chkl.Items.Count; i++)
{
if (chkl.Items[i].Selected)
{
postion += chkl.Items[i].Text + ",";
}
}
ViewState["position"] = string.Concat(ViewState["position"], postion);
}
}