在线调查系统的取值问题?or你有更好的思路?
数据库用两个表,一个存调查主题,另一个存对应主题的选项.
显示面页用DataList显示主题,并在ItemTemplate里放了个PlaceHolder用于显示该主题对应的选项,因为调查有单选或多选的可能,所不能直接放RodioButtonList或CheckBoxList. datalist_ItemDataBound事件代码:
protected void dListShowSbject_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
//给调查主题添加编号
if (e.Item.ItemIndex >= 0)
{
((Label)e.Item.FindControl("lblAutoID")).Text =Convert.ToString(e.Item.ItemIndex + 1);
}
//邦定对应调查主题的选项
int pollid = int.Parse(this.dListShowSbject.DataKeys[e.Item.ItemIndex].ToString());
string selectMode = pl().GetPollSelectMode(pollid).ToString();//取得选择模式(selectmode)
RadioButtonList rbl = new RadioButtonList();
rbl.DataSource = pl().GetPollOptions(pollid);
rbl.DataValueField = "id";
rbl.DataTextField = "options";
CheckBoxList cbl = new CheckBoxList();
cbl.DataSource = pl().GetPollOptions(pollid);
cbl.DataValueField = "id";
cbl.DataTextField = "options";
PlaceHolder pollHolder = (PlaceHolder)e.Item.FindControl("pollHolder");
if (selectMode == "1")//判断是否是单选
{
rbl.DataBind();
pollHolder.Controls.Add(rbl);//RadioButtonList绑定
}
else
{
cbl.DataBind();
pollHolder.Controls.Add(cbl);//CheckBoxList绑定
}
}
}
显示没有问题
取值:我是在一个button里用循环来取值的,但是报错了,
代码:
protected void btnSubmit_Click(object sender, EventArgs e)
{
for (int i = 0; i < this.dListShowSbject.Items.Count; i++)
{
int pollid = int.Parse(this.dListShowSbject.DataKeys[i].ToString());
string selectMode = pl().GetPollSelectMode(pollid).ToString();//取得选择模式(selectmode)
if (selectMode == "1")
{
RadioButtonList rbl = ((RadioButtonList)(this.dListShowSbject.Items[i].FindControl("pollHolder").Controls[0]));
int opid = int.Parse(rbl.SelectedValue);
Response.Write(opid);//测试能否取到值
}
else
{
CheckBoxList cbl = (CheckBoxList)(this.dListShowSbject.Items[i].FindControl("pollHolder").Controls[0]);
string opid = "";
for (int j = 0; j < cbl.Items.Count; i++)
{
if (cbl.Items[j].Selected)
{
opid += cbl.Items[j].Value + ",";
}
if (opid != string.Empty)
{
Response.Write(opid);//测试能否取到值
}
}
}
}
}
错误提示:指定的参数已超出有效值的范围。
参数名: index
红色代码:RadioButtonList rbl = ((RadioButtonList)(this.dListShowSbject.Items[i].FindControl("pollHolder").Controls[0]));
找了半天的错了,我自己测了一下,发现如果是静态在PlaceHolder放的控件就能找到,但在代码添加的到PlaceHolder的控件就提示超出范围.
请各位大哥帮忙看看,或者你们有好的思路也讲讲,先谢谢了,在线等候...