repeater中动态创建的checkboxlist或是radioboxlist取值,在线等。解决立即给分。

M_arlboro 2009-03-23 05:35:23
在做一个调查统计系统。问题和答案分别为两张表,在调查的页面中有单选或是多选。根据问题表中的字段区分单选或是多选,并在后台中动态创建checkboxlist或是radiobuttonlist,以下为测试页面
HTML代码如下:

<asp:Repeater ID="rpQuestion" runat="server">
<ItemTemplate>
第<%# Container.ItemIndex + 1%>题:<%# Eval("Q_Name") %>:<br />
选项:
<asp:HiddenField ID="hfAnswer" runat="server" />
</ItemTemplate>
</asp:Repeater>
<br />
<asp:Button ID="btnEnter" runat="server" Text="确定" OnClick="btnEnter_Click" />
<br />
结果:<asp:Label ID="lblInfo" runat="server"></asp:Label>


protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string strSql = "select * from T_Question where Q_IsUsed=1";
SqlDataAdapter sda = new SqlDataAdapter(strSql, DAL.DbUtils.GetConnection());
DataSet ds = new DataSet();
sda.Fill(ds);
if (ds.Tables[0].Rows.Count > 0)
{
rpQuestion.DataSource = ds;
rpQuestion.DataBind();
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
bool IsType = Convert.ToBoolean(ds.Tables[0].Rows[i]["Q_Type"].ToString());//获取选项方式
HiddenField hfAnswer = (HiddenField)rpQuestion.Items[i].FindControl("hfAnswer");
if (IsType)
{
CheckBoxList ckblist = new CheckBoxList();
ckblist.ID = "ckbAnswer" + i;
strSql = "select * from T_Answer where Q_ID=" + ds.Tables[0].Rows[i]["Q_ID"].ToString() + " and A_IsUsed=1";
SqlDataAdapter A_sda = new SqlDataAdapter(strSql, DAL.DbUtils.GetConnection());
DataSet A_ds = new DataSet();
A_sda.Fill(A_ds);
ckblist.DataSource = A_ds;
ckblist.DataTextField = "A_Content";
ckblist.DataValueField = "A_ID";
ckblist.DataBind();
Label lblAnswer = (Label)rpQuestion.Items[i].FindControl("lblAnswer");
rpQuestion.Items[i].Controls.Add(ckblist);
hfAnswer.Value = "1";
}
else
{
RadioButtonList rbtlist = new RadioButtonList();
rbtlist.ID = "rbtlist" + i;
strSql = "select * from T_Answer where Q_ID=" + ds.Tables[0].Rows[i]["Q_ID"].ToString() + " and A_IsUsed=1";
SqlDataAdapter A_sda = new SqlDataAdapter(strSql, DAL.DbUtils.GetConnection());
DataSet A_ds = new DataSet();
A_sda.Fill(A_ds);
rbtlist.DataSource = A_ds;
rbtlist.DataTextField = "A_Content";
rbtlist.DataValueField = "A_ID";
rbtlist.DataBind();
Label lblAnswer = (Label)rpQuestion.Items[i].FindControl("lblAnswer");
rpQuestion.Items[i].Controls.Add(rbtlist);
hfAnswer.Value = "2";
}
}
}
}
}

protected void btnEnter_Click(object sender, EventArgs e)
{
string strValue = "";
int rpCount = rpQuestion.Items.Count;
for (int i = 0; i < rpCount; i++)
{
string strCheck = "";
string strRadio = "";
HiddenField hfAnswer = (HiddenField)rpQuestion.Items[i].FindControl("hfAnswer");
if (hfAnswer.Value == "1")
{
CheckBoxList ckbAnswer = (CheckBoxList)rpQuestion.Items[i].FindControl("ckbAnswer" + i);
int s = rpQuestion.Items[i].Controls.Count;
for (int j = 0; j < ckbAnswer.Items.Count; j++)
{
if (ckbAnswer.Items[j].Selected)
{
strCheck += ckbAnswer.Items[i].Value + "|" + ckbAnswer.Items[i].Text;
}
}
strValue += "第" + i + "题你选择的是:" + strCheck + "<br>";

}
else
{
RadioButtonList rbtlist = (RadioButtonList)rpQuestion.Items[i].FindControl("rbtlist" + i);
for (int j = 0; j < rbtlist.Items.Count; j++)
{
if (rbtlist.Items[j].Selected)
{
strRadio = rbtlist.Items[i].Value + "|" + rbtlist.Items[i].Value;
}
}
strValue += "第" + i + "题你选择的是:" + strRadio + "<br>";
}
}
lblInfo.Text = strValue;
}

...全文
765 31 打赏 收藏 转发到动态 举报
写回复
用AI写文章
31 条回复
切换为时间正序
请发表友善的回复…
发表回复
cwmwss 2009-03-23
  • 打赏
  • 举报
回复
UP。学习
zzxap 2009-03-23
  • 打赏
  • 举报
回复
Tb_ttlsh.Text = drv["SuoShuTaoTiLiuShuiHao"].ToString();
string zqda = drv["ZhengQueDaAn"].ToString();
string[] da = zqda.Split(new char[] {',' }); //使用Split()通过逗号将字符串拆分
foreach (string xx in da)
{
for (int i = 0; i < CBL_da.Items.Count; i++)
{
if (CBL_da.Items[i].Text == xx)
{
CBL_da.Items[i].Selected = true;
}

}
}
//Tb_ttlsh是TextBox控件,Drv是DataRowView视图对象
zzxap 2009-03-23
  • 打赏
  • 举报
回复
string[] result = a.Split('.');
List<string> list = new List<string>();
for(int i=0;i<result.Length;i++)
{
list.Add(result[i]);
}
for(int i=0;i<CheckBoxList.Items.Count;i++)
{
if(list.Contains(CheckBoxList.Items[i].Text))
CheckBoxList.Items[i].Selected=true;
}

zzxap 2009-03-23
  • 打赏
  • 举报
回复
int[] index = {1,2,3,5};//值从数据库中读取
for(i=0;i<index.length;i++)
{
CheckBoxList1.Items[index[i]].Selected = true;
}

Robin 2009-03-23
  • 打赏
  • 举报
回复


#region 调用存储过程来执行CustomerAnswers的插入和其他表的修改

private bool GetInsertCustomerAnswersAndUpdateResearchOptions(CustomerAnswers customerAnswers, string optionId, int count,int taskId)
{
CustomerAnswersBL customerAnswersBL = new CustomerAnswersBL();
return customerAnswersBL.InsertCustomerAnswersAndUpdateCustomer(customerAnswers, optionId, count,taskId);
}

#endregion




#region 定义一个执行添加、修改的公共方法

/// <summary>
/// 执行添加、修改的公共方法。
/// </summary>
/// <param name="customerId">客户编号</param>
/// <param name="researchId">问卷编号</param>
/// <param name="questionId">问题编号</param>
/// <param name="answer">问题答案</param>
/// <param name="count">设置提交的次数</param>
private void ExecuteInsertOrUpdate(int customerId,string customerCode, int researchId, int questionId, string answer, int count, string optionId,int taskId)
{
bool flag = false;
CustomerAnswers customerAnswers = new CustomerAnswers();
customerAnswers.customerCode = customerCode;
customerAnswers.CustomerId = customerId;
customerAnswers.ResearchId = researchId;
customerAnswers.QuestionId = questionId;
customerAnswers.Answer = answer;
customerAnswers.Createtime = DateTime.Now;
flag = this.GetInsertCustomerAnswersAndUpdateResearchOptions(customerAnswers, optionId, count,taskId);
if (flag == true)
{
//ClientScript.RegisterStartupScript(ClientScript.GetType(), "", "<script>alert('保存成功!!');Exit();</script>");
}
else
{
//ClientScript.RegisterStartupScript(ClientScript.GetType(), "", "<script>alert('保存失败!!');Exit();</script>");
}
}

#endregion

#region 定义一个属性来实现传递参数.

//public string CustomerCode
//{
// get { return customerCode; }
// set { customerCode = value; }
//}

int researchId=0;

public int ResearchId
{
get { return researchId; }
set { researchId = value;
this.lbResearchName.Text = GetResearchName(researchId);
this.GetBinderDataList(researchId);
}
}
#endregion

#region 定义一个公共方法来实现添加数据.

/// <summary>
/// 定义公共方法,来实现插入操作.
/// </summary>
public void InsertCustomer(int researchId,int customerId,string customerCode,int taskId)
{
//int researchId = Convert.ToInt32(Request.QueryString["researchId"]);

ResearchOptionsBL researchOptionsBL = new ResearchOptionsBL();
SqlDataReader sdr = researchOptionsBL.GetResearchQuestionsAndResearchOptions(researchId);

while (sdr.Read())
{
int questionId = Convert.ToInt32(sdr["questionId"]);
int questionType = Convert.ToInt32(sdr["questionType"]);

string answer = "";
string answer1 = "";
string answer2 = "";
string optionId = "";
string optionIds = "";

#region
for (int i = count; i < dlQuestions.Items.Count; i++)
{
if (dlQuestions.Items[i].ItemType == ListItemType.Item || dlQuestions.Items[i].ItemType == ListItemType.AlternatingItem)
{
switch (questionType)
{
case 0:
RadioButtonList radioButtonList = dlQuestions.Items[i].FindControl("radioButtonList") as RadioButtonList;
if (radioButtonList != null)
{
if (radioButtonList.SelectedIndex != -1)
{
optionId = radioButtonList.SelectedValue + ",";
answer = radioButtonList.Items[radioButtonList.SelectedIndex].Text;
}
else
{
//ClientScript.RegisterStartupScript(ClientScript.GetType(), "", "<script>alert('请填写完整!');</script>");
}
}
break;
case 1:
CheckBoxList checkBoxList = dlQuestions.Items[i].FindControl("checkBoxList") as CheckBoxList;
if (checkBoxList != null)
{
if (checkBoxList.SelectedIndex != -1)
{
for (int j = 0; j < checkBoxList.Items.Count; j++)
{
if (checkBoxList.Items[j].Selected)
{
answer1 += checkBoxList.Items[j].Text + "~";
optionIds += checkBoxList.Items[j].Value + ",";
}
}
}
else
{
//ClientScript.RegisterStartupScript(ClientScript.GetType(), "", "<script>alert('请填写完整!');</script>");
}
}
break;
default:
TextBox textbox = dlQuestions.Items[i].FindControl("textBox") as TextBox;
if (textbox != null)
{
if (textbox.Text == "" || textbox.Text == null)
{
//ClientScript.RegisterStartupScript(ClientScript.GetType(), "", "<script>alert('请填写完整!');</script>");
}
else
{
answer2 = textbox.Text;
}
}
break;
}
if (answer1 != "" && answer1 != null && answer1 != "~")
{
break;
}
if (answer != "" && answer != null)
{
break;
}
if (answer2 != "" && answer2 != null)
{
break;
}
}
}
#endregion

if (answer != null && answer != "")
{
this.ExecuteInsertOrUpdate(customerId,customerCode, researchId, questionId, answer, count, optionId,taskId);
}
if (answer1 != null && answer1 != "")
{
answer1 = answer1.Substring(0, answer1.Length - 1);
this.ExecuteInsertOrUpdate(customerId,customerCode, researchId, questionId, answer1, count, optionIds,taskId);
}
if (answer2 != null && answer2 != "")
{
this.ExecuteInsertOrUpdate(customerId,customerCode, researchId, questionId, answer2, count, "",taskId);
}

count++;
}
if (!sdr.IsClosed)
{
sdr.Close();
}
}

#endregion
}
xfreyes 2009-03-23
  • 打赏
  • 举报
回复
用datalist多好布局
javacaspnet 2009-03-23
  • 打赏
  • 举报
回复
如果是单个的取值用Text
如果是多个的List取值用SelectedValue
coodd 2009-03-23
  • 打赏
  • 举报
回复
9楼说得对。还有代码太凌乱,产生A_ds的过程应该放在数据访问层,比如DAL下面的类。
Teng_s2000 2009-03-23
  • 打赏
  • 举报
回复
看着比较费劲!
pgameli 2009-03-23
  • 打赏
  • 举报
回复
我以前做过问卷调查的项目
你应该用Repeater 嵌套Repeater
外层的Repeater显示题目,内层的显示选项
在内层的Repeater里面你把checkboxlist和radiobuttonlist都放上,设为隐藏

后台绑定数据的时候,外层Repeater绑定题目,内层绑定选项,内层绑定的时候就判断时候是单选还是多选或者是文本
然后把相应的控件设为可见,

就是这样一个思路
lz参考下
我姓区不姓区 2009-03-23
  • 打赏
  • 举报
回复
试试把动态生成CheckBoxList和RadioButtonList的代码放到if(!IsPostBack)外面
jlj84237485 2009-03-23
  • 打赏
  • 举报
回复
帮顶一下
hy_lihuan 2009-03-23
  • 打赏
  • 举报
回复
你的问题是什么啊?程序错了?
sxmonsy 2009-03-23
  • 打赏
  • 举报
回复
http://blog.csdn.net/chenguang79/archive/2008/07/04/2611575.aspx
这有个例子.
你看下就明白了.
zhgroup 2009-03-23
  • 打赏
  • 举报
回复
我没用过这种方式处理,可以给你一个我的思路:
对于问题,你的数据库中一定有唯一编码,对于checkbox或radio类型的表单,可以在后台输出,
对于checkbox,可以以[“数据库唯一编码”_chk]这样的名称作为其唯一标识,radio也可采用此种方式,
然后在提交数据库,可以通过Request.Forms中,根据名称获取前台checkbox/radio的值
liuyeede 2009-03-23
  • 打赏
  • 举报
回复
你现在的数据绑定正确无误?
M_arlboro 2009-03-23
  • 打赏
  • 举报
回复
呵呵。忘记加注释,因为是测试页面,代码也不规范。各位牛人别见怪
houndsky 2009-03-23
  • 打赏
  • 举报
回复
顶起!
M_arlboro 2009-03-23
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 ojlovecd 的回复:]
试试把动态生成CheckBoxList和RadioButtonList的代码放到if(!IsPostBack)外面
[/Quote]
测试成功!能说说为什么会出现这样的情况吗?有点弄不明白。虽然这种方法不可取。因为点提交后又加载一次数据。
zhoudengpan102 2009-03-23
  • 打赏
  • 举报
回复
你应该用Repeater 嵌套Repeater
外层的Repeater显示题目,内层的显示选项
在内层的Repeater里面你把checkboxlist和radiobuttonlist都放上,设为隐藏
加载更多回复(10)

62,267

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧