从动态控件获取用户输入
我正在寻找创建一个动态调查。 我在其中从数据库生成所有问题控件。 下面是我想要做的示例(没有数据库部分)。 我能够显示问题,如下所示。 我无法读取用户的输入。
有没有人有什么想法。
我已经研究了视图状态,但我似乎无法让它工作。
当通过回发重新加载页面时,控件就会消失。 我已经查看了我能想到的所有事件,但页面上没有控件。 直到我在 Page_Load 事件中再次创建它们。
我在哪里可以找到动态创建的控件上的用户输入的值?
页面
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
代码文件
protected override void OnPreLoad(EventArgs e)
{
base.OnPreLoad(e);
foreach (Control item in form1.Controls)
{ }
}
protected void Page_PreInit(object sender, EventArgs e)
{
foreach (Control item in form1.Controls)
{ }
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
foreach (Control item in form1.Controls)
{ }
}
protected override void OnSaveStateComplete(EventArgs e)
{
base.OnSaveStateComplete(e);
foreach (Control item in form1.Controls)
{ }
}
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
foreach (Control item in form1.Controls)
{ }
}
protected void Page_Load(object sender, EventArgs e)
{
RadioButton rb;
rb = new RadioButton();
rb.ID = "rb_1";
rb.Text = "yes";
rb.GroupName = "question";
form1.Controls.Add(rb);
rb = new RadioButton();
rb.ID = "rb_2";
rb.Text = "no";
rb.GroupName = "question";
form1.Controls.Add(rb);
rb = new RadioButton();
rb.ID = "rb_3";
rb.Text = "other";
rb.GroupName = "question";
form1.Controls.Add(rb);
TextBox tb = new TextBox();
form1.Controls.Add(tb);
Button btn = new Button();
btn.Text = "Save";
form1.Controls.Add(btn);
foreach (Control item in form1.Controls)
{
}
}
I am looking to create a dynamic survey. Where I generate all the question controls from the database. Below is an example of what I am trying to do (without the database part). I am able to display questions as seen below. I am unable to read the users input.
Does anyone have any ideas.
I have looked into the viewstate, but I can not seem to get it to work.
When the page is reloaded through a Post Back the controls are gone. I have looked at every event I can think of and there are no controls on the page. Until I create them again on the Page_Load event.
Where do I look to find the value of the user input on dynamicly created controls?
Page
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
Code File
protected override void OnPreLoad(EventArgs e)
{
base.OnPreLoad(e);
foreach (Control item in form1.Controls)
{ }
}
protected void Page_PreInit(object sender, EventArgs e)
{
foreach (Control item in form1.Controls)
{ }
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
foreach (Control item in form1.Controls)
{ }
}
protected override void OnSaveStateComplete(EventArgs e)
{
base.OnSaveStateComplete(e);
foreach (Control item in form1.Controls)
{ }
}
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
foreach (Control item in form1.Controls)
{ }
}
protected void Page_Load(object sender, EventArgs e)
{
RadioButton rb;
rb = new RadioButton();
rb.ID = "rb_1";
rb.Text = "yes";
rb.GroupName = "question";
form1.Controls.Add(rb);
rb = new RadioButton();
rb.ID = "rb_2";
rb.Text = "no";
rb.GroupName = "question";
form1.Controls.Add(rb);
rb = new RadioButton();
rb.ID = "rb_3";
rb.Text = "other";
rb.GroupName = "question";
form1.Controls.Add(rb);
TextBox tb = new TextBox();
form1.Controls.Add(tb);
Button btn = new Button();
btn.Text = "Save";
form1.Controls.Add(btn);
foreach (Control item in form1.Controls)
{
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您必须生成具有唯一 ID 的控件。 您必须能够使用数据库中的一个问题对每个控件 ID 进行数学计算。 完成此操作后,您可以将特定问题的答案写回到数据库中。
You have to generate the controls with an unique id. You must be able to math each control-id with exactly one question in your database. After doing this, you are able to write back the answer to a specific question into a datbase.
编辑:在 OnInit 函数中创建控件(在 Init 事件期间)。 检查此页面。
尝试使“rb”或“tb”变量具有类范围。 或者至少将对创建的控件的引用存储在列表(或数组)中,然后在需要时尝试通过它访问它们。
由于您在 OnInit 函数中创建控件,因此它们是在加载视图状态之前创建的。 这样,当加载视图状态时,用户发布的值将从视图状态恢复到控件,因此您可以从代码中访问它。 您只需要保留对控件的引用即可。
Edit: Create the controls in the OnInit function (during the Init event). Check this page.
Try to make 'rb' or 'tb' variables class-scoped. Or at least store the reference to the created controls in a list (or array) and then try to access them through it when you want.
Since you create the control in the OnInit function, they are created before loading of view state. So that when the view state gets loaded, values posted by the user are restored from the view state to the control and thus you can access it from your code. You just need to keep the reference to the controls.