62,267
社区成员
发帖
与我相关
我的任务
分享
以下为测试页面
<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;
}
#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
}