如何在ASP.NET和C#中动态生成HTML控件 [英] How to generate HTML Control Dynamically in asp.net and C#
问题描述
< pre lang ="xml">我想使用asp.net和C#创建一些html无线电控件购买.无线电数量的数量将是动态的.所有控件将显示在表格中.将有一个asp提交按钮.我将选择其中一些,然后单击提交按钮.然后根据我的选择,数据将存储在数据库中.
注意:我可以根据自己的设置生成HTML表和Radio.但是问题是,当我选择其中的一些单击提交按钮时,我发现了一个错误.空引用是用户不方便的..
我正在尝试通过以下代码获取单选按钮
RadioButton noRadioButton =(RadioButton)Page.FindControl("rdbN1" + oCResult.DataTableContainerSub.Rows [i] ["Id"].ToString());
如果(yesRadioButton.Checked)注意@在这种情况下,我正在发现上述错误
{
//我的代码...
}
有什么办法可以解决我的问题吗?
最好的问候
乱弹
注意:我生成的html输出在下面给出
根据我的数据库,我已经在C#中生成了以下代码
-------------------------------------------------- --------------------------------
< table& gt;< tr& gt;< td&>< div& gt;< p& gt;& lt; span id =& quot ; lblQiewgo& gt; Q1.Test Question-1</span& gt;</p& gt;
< p& gt;& lt;输入id =" rdbY1iewgo& quot; type =&"radio& amp; amp; quot; name =& quot; Aiewgo& quot; value =&"rdb1iewgo& quot; runat:server/< label for =&"rdbY1iewgo&" Yes&/label & amp; nbsp; & amp; nbsp; <输入id =" rdbN1iewgo& quot; type =&"radio& amp; amp; quot; name =& quot; Aiewgo& quot; value =&"rdb2iewgo& quot; /& lt;//amp; lt;/& lt;/& lt;/& lt;/div;/& lt;/& </td& gt;</tr& gt;< tr& gt;< td&>< div& gt;< p& >< span id =& quot; lblQkmseb& gt; Q2.Test Question-2</span& lt;/p& gt;</p</
< p& gt;& lt;输入id =" rdbY2kmseb& quot; type =&"radio& amp; amp; quot; name =& quot; Bkmseb& quot; value =&"rdb1kmseb& quot; /& lt;标签为=" rdbY2kmseb"是</label& & amp; nbsp; & amp; nbsp; <输入id =" rdbN2kmseb& quot; type =&"radio& amp; amp; quot; name =& quot; Akmseb& quot; value =&"rdb2kmseb& quot; /& lt;标签为=" rdbN2kmseb& quot;否& lt;/label& amp;> nbsp; & amp; nbsp;
< span id =& lt; lblComkmseb& gt;评论</span& gt; & amp; nbsp; & lt; textarea name =& quot; txtCom2& quot; rows =&"2"& cols =&"20&" id =& lt;/textarea></p& gt;</div& gt;</td& gt; ;</tr& gt;< tr& gt;< td& gt;& lt; div& gt;& lt; p& gt;& lt; span id =& amp; ;"lblQgtbet"& gt; Q3.Test Question-3</span& gt;</p& gt;
< p& gt;& lt;输入id =" rdbExcgtbet& quot; type =&"radio& amp; amp;" name =& quot; Cgtbet" value =&"rdb1gtbet& quot; /& lt; label for =&"rdbExcgtbet&"& gt; Excellent</label& gt;
& amp; nbsp; & amp; nbsp; <输入id =" rdbVGgtbet& quot; type =&"radio& amp; amp; quot; name =& quot; Cgtbet" value =& quot; rdb2gtbet& quot; /& lt; label for =& quot; rdbVGgtbet& quot;非常好</label& gt;
& amp; nbsp; & amp; nbsp; & lt;输入id =" rdbGgtbet& quot; type =&"radio& amp; amp; quot; name =& quot; Cgtbet"值=& quot; rdb3gtbet& quot; /& lt; label for =&"rdbGgtbet&"& gt; Good& lt//label& gt;
& amp; nbsp; & amp; nbsp; <输入id =" rdbAVgtbet& quot; type =&"radio& amp; amp; quot; name =& quot; Cgtbet" value =& quot; rdb4gtbet& quot; /& lt;标签为=&"rdbAVgtbet&"& gt;平均</label& gt;
& amp; nbsp; & amp; nbsp; & lt;输入id =" rdbPgtbet& quot; type =&"radio& amp; amp; quot; name =& quot; Cgtbet" value =& quot; rdb5gtbet& quot; /& lt;//amp; lt;/& lt;/& lt;/& lt;/& lt;/& lt;/div; </td&></tr& gt;</table& gt;
-------------------------------------------------- ------------------------
我正在页面加载中将html字符串传递给QuestionsDisplay.InnerHtml,甚至是动态生成我的html控件.
[QuestionsDisplay.InnerHtml = MyHTML代码]
如果有人知道更好的技术,请给我建议.</pre>
QuestionsDisplay.InnerHtml
<pre lang="xml">I want to create some html radio control buy using asp.net and C#. The number of radio quantity will be dynamic. All the control will be displayed in a table. There will a be a asp submit button. I will select some of them and will click the submit button. Then according to my choice data will be stored in database.
NB: I can generate the HTML table and Radio according to my setup. But problem is that when i select some of them an click the submit button then i am finding one error. Null reference was unhandy by the user..
I am trying to get the radioa button by the following code
RadioButton noRadioButton = (RadioButton)Page.FindControl("rdbN1" + oCResult.DataTableContainerSub.Rows[i]["Id"].ToString());
if (yesRadioButton.Checked) NB @ this possiotion i am finding the above error
{
//My code...
}
Is there any bod to solve my problem.
Best Regard
Rashed
NB: My generated html output is given below
According to my DataBase i have generated the below code in C#
----------------------------------------------------------------------------------
<table><tr><td><div><p><span id="lblQiewgo ">Q1.Test Question-1</span></p>
<p><input id="rdbY1iewgo " type="radio" name="Aiewgo " value="rdb1iewgo " runat:server /><label for="rdbY1iewgo ">Yes</label> &nbsp; &nbsp; <input id="rdbN1iewgo " type="radio" name="Aiewgo " value="rdb2iewgo " /><label for="rdbN1iewgo ">No</label></p></div></td></tr><tr><td><div><p><span id="lblQkmseb ">Q2.Test Question-2</span></p>
<p><input id="rdbY2kmseb " type="radio" name="Bkmseb " value="rdb1kmseb " /><label for="rdbY2kmseb ">Yes</label> &nbsp; &nbsp; <input id="rdbN2kmseb " type="radio" name="Akmseb " value="rdb2kmseb " /><label for="rdbN2kmseb ">No</label>&nbsp; &nbsp;
<span id="lblComkmseb ">Comment</span> &nbsp; <textarea name="txtCom2" rows="2"cols="20" id="txtComkmseb "></textarea></p></div></td></tr><tr><td><div><p><span id="lblQgtbet ">Q3.Test Question-3</span></p>
<p><input id="rdbExcgtbet " type="radio" name="Cgtbet " value="rdb1gtbet " /><label for="rdbExcgtbet ">Excellent</label>
&nbsp; &nbsp; <input id="rdbVGgtbet " type="radio" name="Cgtbet " value="rdb2gtbet " /><label for="rdbVGgtbet ">Very Good</label>
&nbsp; &nbsp; <input id="rdbGgtbet " type="radio" name="Cgtbet " value="rdb3gtbet " /><label for="rdbGgtbet ">Good</label>
&nbsp; &nbsp; <input id="rdbAVgtbet " type="radio" name="Cgtbet " value="rdb4gtbet " /><label for="rdbAVgtbet ">Average</label>
&nbsp; &nbsp; <input id="rdbPgtbet " type="radio" name="Cgtbet " value="rdb5gtbet " /><label for="rdbPgtbet ">Poor</label></p></div></td></tr></table>
--------------------------------------------------------------------------
I am passing the html string into QuestionsDisplay.InnerHtml in page load even to generate my html control dynamically.
[QuestionsDisplay.InnerHtml=MyHTML code]
If any one know better technique please give me the suggestion.</pre>
QuestionsDisplay.InnerHtml
推荐答案
包括您尝试过的内容将对您有所帮助.
您是否尝试过使用Repeater或CheckBoxList?
It would have been helpful of you to include what you had tried.
Have you you tried using a Repeater or a CheckBoxList?
这篇关于如何在ASP.NET和C#中动态生成HTML控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!