将动态创建的控件数据保存到数据库中 [英] Saving Dynamically Created Controls Data Into a Database

查看:106
本文介绍了将动态创建的控件数据保存到数据库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我试图找出如何保存动态创建的控件中捕获的数据。



所以会发生什么是在表单加载我会加载动态控件:





switch(WhichControl)

{

caseddl:

DropDownList ddl = new DropDownList();

ddl.ID = string.Format(ddList {0},icounter);

ddl.DataSource = myDataSource();

ddl.DataTextField =TextField;

ddl.DataValueField =DataField;

ddl.ClientIDMode = System.Web.UI.ClientIDMode.Static;

ddl.DataBind();

row.Cells [1] .Controls.Add(ddl);

break;

caserb:

RadioButtonList rb = new RadioButtonList();

rb.DataSource = myDataSource();

rb.DataTextField =TextField;

rb.DataValueField =DataField;

rb.ID = string.Format(rList {0} ,icounter);

rb.ClientIDMode = System.Web.UI.ClientIDMode.Static;

row.Cells [1] .Controls.Add(rb);

休息;

casetb:

TextBox tb = new TextBox();

tb.TextMode = TextBoxMode.SingleLine;

tb.ID = string.Format(textBox {0},icounter);

tb.ClientIDMode = System.We b.UI.ClientIDMode.Static;

row.Cells [1] .Controls.Add(tb);

break;



默认值:

休息;



}



现在我的问题是如何最好地阅读用户选择的选项并将其保存到数据库。



一个例子将不胜感激。当我尝试遍历页面时,我无法获得控件。



谢谢,

解决方案

我从您的文本中了解到,您必须使用'Request.Form(控件名称属性值)',因此从表单集合中获取控件,然后您可以使用这些控件并获取它们的值以保存在数据库中。

Hi everyone,

I am trying to find out how I can save the data captured in dynamically created controls.

So what happens is on the form load I will load dynamic controls:


switch ("WhichControl")
{
case "ddl":
DropDownList ddl = new DropDownList();
ddl.ID = string.Format("ddList{0}", icounter);
ddl.DataSource = myDataSource();
ddl.DataTextField = "TextField";
ddl.DataValueField = "DataField";
ddl.ClientIDMode = System.Web.UI.ClientIDMode.Static;
ddl.DataBind();
row.Cells[1].Controls.Add(ddl);
break;
case "rb":
RadioButtonList rb = new RadioButtonList();
rb.DataSource = myDataSource();
rb.DataTextField = "TextField";
rb.DataValueField = "DataField";
rb.ID = string.Format("rList{0}", icounter);
rb.ClientIDMode = System.Web.UI.ClientIDMode.Static;
row.Cells[1].Controls.Add(rb);
break;
case "tb":
TextBox tb = new TextBox();
tb.TextMode = TextBoxMode.SingleLine;
tb.ID = string.Format("textBox{0}", icounter);
tb.ClientIDMode = System.Web.UI.ClientIDMode.Static;
row.Cells[1].Controls.Add(tb);
break;

default:
break;

}

now my question is how best can i read the options selected by the user and save it to the database.

An example would be greatly appreciated. When I try to loop through the page I can't get the controls.

Thanks,

解决方案

As much I understood from your text is you have to use 'Request.Form("Control Name Attribute value")' so get the control from form collection and than you can use these control and fetch their value to save in database.


这篇关于将动态创建的控件数据保存到数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆