多文本框的值得存放和赋值
问题:在一个页面中有多项考核成绩,如:礼貌、表达技巧、电话控制、倾听技巧、情绪控制等多项考核,而没想考核又有多项考核标准,如:评分、内容、对话实例等,会这样下来,一个页面会有几百个内容需要存储,如果新建张表的话会要上百个字段,这明显是不合适,面对上面的为题我是这样解决的:
1 在该页面中按考核标准建立字段,如果有10个考核标准就建立是个字段。
2 每项考核相对应的考核标准要填写的文本框用有规律的命名方法,如前缀相同的名字+数字(我是这样命名的)
3 在后台根据相应的名字循环遍历取出值,后加分隔符,最后存入相应的字段中。
页面局部图如下:
后台处理的主要方法:
插入数据库中的时候:
//取出所有对话实例列中的值,拼接成字符串赋值给Instance,Score,Content也是同样的方法
StringBuilder Instance = new StringBuilder();
StringBuilder Score = new StringBuilder();
StringBuilder TutorContent = new StringBuilder();
for (int i = 1; i <= 52; i++)
{
if (Page.FindControl("tbx_Instance" + i).GetType() == typeof(TextBox))
{
Instance.Append((Page.FindControl("tbx_Instance" + i) as TextBox).Text + "#&");
}
if (Page.FindControl("tbx_content" + i).GetType() == typeof(TextBox))
{
TutorContent.Append((Page.FindControl("tbx_content" + i) as TextBox).Text + "#&");
}
if (Page.FindControl("ddl_S" + i).GetType() == typeof(DropDownList))
{
Score.Append((Page.FindControl("ddl_S" + i) as DropDownList).SelectedValue + "#&");
}
StringBuilder Instance = new StringBuilder();
StringBuilder Score = new StringBuilder();
StringBuilder TutorContent = new StringBuilder();
for (int i = 1; i <= 52; i++)
{
if (Page.FindControl("tbx_Instance" + i).GetType() == typeof(TextBox))
{
Instance.Append((Page.FindControl("tbx_Instance" + i) as TextBox).Text + "#&");
}
if (Page.FindControl("tbx_content" + i).GetType() == typeof(TextBox))
{
TutorContent.Append((Page.FindControl("tbx_content" + i) as TextBox).Text + "#&");
}
if (Page.FindControl("ddl_S" + i).GetType() == typeof(DropDownList))
{
Score.Append((Page.FindControl("ddl_S" + i) as DropDownList).SelectedValue + "#&");
}
数据库中取出,给相应的文本赋值:
//给对话实例文本框赋值
string intance = dtCS.Rows[0]["Example"].ToString();
string[] sInstance = Regex.Split(intance, "#&", RegexOptions.IgnoreCase);
for (int i = 1; i < sInstance.Length; i++)
{
(Page.FindControl("tbx_Instance" + i) as TextBox).Text = sInstance[i-1];
}
//给辅导内容文本框赋值
string content = dtCS.Rows[0]["TutorContent"].ToString();
string[] sContent = Regex.Split(content, "#&", RegexOptions.IgnoreCase);
for (int i = 1; i < sContent.Length; i++)
{
(Page.FindControl("tbx_content" + i) as TextBox).Text = sContent[i-1];
}
//给评分赋值
string Score = dtCS.Rows[0]["Score"].ToString();
string[] sScore = Regex.Split(Score, "#&", RegexOptions.IgnoreCase);
for (int i = 1; i < sScore.Length; i++)
{
(Page.FindControl("ddl_S" + i) as DropDownList).SelectedValue = sScore[i-1];
}
string[] sInstance = Regex.Split(intance, "#&", RegexOptions.IgnoreCase);
for (int i = 1; i < sInstance.Length; i++)
{
(Page.FindControl("tbx_Instance" + i) as TextBox).Text = sInstance[i-1];
}
//给辅导内容文本框赋值
string content = dtCS.Rows[0]["TutorContent"].ToString();
string[] sContent = Regex.Split(content, "#&", RegexOptions.IgnoreCase);
for (int i = 1; i < sContent.Length; i++)
{
(Page.FindControl("tbx_content" + i) as TextBox).Text = sContent[i-1];
}
//给评分赋值
string Score = dtCS.Rows[0]["Score"].ToString();
string[] sScore = Regex.Split(Score, "#&", RegexOptions.IgnoreCase);
for (int i = 1; i < sScore.Length; i++)
{
(Page.FindControl("ddl_S" + i) as DropDownList).SelectedValue = sScore[i-1];
}