如何在回发后获得动态控制单选按钮的值 [英] How to get value of dynamic control radio button after postback

查看:62
本文介绍了如何在回发后获得动态控制单选按钮的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我正在研究创建一个动态调查,发布于动态控件的获取用户输入但是有一些不同的环境

下面是我想做什么:

首先当用户点击按钮时,它会用调查问卷的单选按钮填充动态表格

但是,我无法单击提交按钮后获取其值(用于计算分数)

除了我正在使用ajax扩展(updatePanel)进行开发

我一直在调查viewstate但是我我不知道它。


有没有人有任何想法?


这里我包含了一些我的代码:


页面中代码的一部分

展开 | 选择 | Wrap | 行号

解决方案

有几点要记住当你使用动态控件时。


首先你应该考虑动态控件的范围。


如果你创建了一个控制函数(如按钮,表格,单选按钮......)然后控件只存在于该函数中.....它将在页面上呈现(因为您将其添加到占位符中)但是当请求到来时回到服务器后,函数中声明的控件将不再存在。


这意味着动态控件必须有整个页面的范围...你不能简单地在里面声明它们一种方法。


另外你需要考虑的是ASP页面生命周期....


请看一下这篇关于


我明白,你是对的,你需要使用动态控件。 />

我建议重新访问GridView的想法......或者你可以考虑创建一个模板控件。有关模板化用户控件的示例,请查看这篇文章


查看 ITemplate界面,这篇文章关于构建模板化自定义ASP.NET服务器控件,本文关于使用模板字段 ....这应该可以帮助你入门。


如果你有任何问题请告诉我。


-Frinny


Hi guys,

I am looking into create a dynamic survey as posted in Get User Input From Dynamic Controls but with some different environment
Below is what i am trying to do:
First when the user click the button, it will populate a dynamic table with radio button for the survey questionnaire
However, i was unable to get its value (for score calculation) after clicking the submit button
Beside i am using an ajax extension (updatePanel) for the development
I have been look into viewstate but i hv no idea with it.

Does anyone have any ideas?

Here i included some of my code:

part of code in page

Expand|Select|Wrap|Line Numbers

解决方案

Well there are a few things to keep in mind when you''re using dynamic controls.

First of all you should consider the scope of the dynamic controls.

If you create a control in a function (like a button, table, radiobutton...) then the control only exists within that function.....it will be rendered on the page (because you added it to the placeholder) but when the request comes back to the server the controls declared in the function will no longer exist.

This means that dynamic controls have to have a scope for the whole page...you cannot simply declare them inside a method.

The other thing you have to consider is the ASP Page Life Cycle....

Please take a look this article about how to use dynamic controls in ASP.NET....


I don''t think you need to use dynamic controls for your application.
You should be using a RadioButtonList instead of dynamically creating individual RadioButtons. You can assign a DataSource to the RadioButtonList. The RadioButtonList will automatically create the necessary RadioButtons for the DataSource that it''s bound to.

This means that your RadioButtonList is not dynamic, but the datasource that it''s bound to IS....so the content of the RadioButtonList is dynamic.

For example check out this article on how to bind a RadioButtonList to a Custom Object...if you''re just using a DataBase then it''s even easier :)



This will save you a lot of problems.


Hi Frinavale.
Thank you for your reply.

Well, the outcome i want to get is as shown in the image below:


I don''t think that it can be done (column by column) by using radio button list.

At first, i was using gridview to generate the survey. However, i was unable to do it using data source due to dynamic columns number. Besides, the header for the radio button is also generated dynamically. Therefore, i have no choice but to go for dynamic controls generation.

Do you have any idea or solution for this?

I very appreciated your help :)


@ive1122
Ah I understand, you''re right, you need to use dynamic controls.

I would recommend revisiting the GridView idea...or you could consider creating a templated control. For an example of a templated user control check out this post.

Check out the ITemplate interface, this article about Building Templated Custom ASP.NET Server Controls, this article on working with template fields....that should probably get you started.

If you have any questions let me know.

-Frinny


这篇关于如何在回发后获得动态控制单选按钮的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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