问题描述
|
我正在从存储过程中检索数据,并使用它通过ASP.Net Repeater来填充问题列表,如下所示。
当所有问题属于同一类型时,它会非常有用。但是我现在想提出两种不同类型的问题,直到运行时才知道。
例如,一个将仅具有“是”或“否”广播框,而另一个将具有5个广播框,允许最终用户对1 -5进行评分。
从存储过程返回的数据中将有一个字段,它将标识问题的类型,我想在运行时读取该值,然后显示适当的HTML。
看来我应该使用Repeater的ItemDataBound事件,但是我不知道该怎么做?
<asp:Panel ID=\"pnlQuestions\" runat=\"server\">
<asp:Repeater ID=\"Repeater1\" runat=\"server\" DataSourceID=\"DBDataSource\"
onitemdatabound=\"Repeater1_ItemDataBound\">
<ItemTemplate>
<div class=\"Question\">
/*The HTML that is written here should depend on the value
in one of the fields returned from the Stored Procedure*/
</div>
</ItemTemplate>
<FooterTemplate>
<div id=\"Footer\">
<label for=\"AdditionalCommentsText\" class=\"AdditionalCommentsLabel\">Are there any additional comments or referrals you can provide at this time?</label>
<textarea id=\"\"AdditionalCommentsText\" class=\"AdditionalCommentsText\" cols=\"80\" rows=\"3\" name=\"Q<%# DataBinder.Eval(Container.DataItem,\"ID\") %>AddtlComments\"></textarea>
<asp:Button class=\"SubmitButton\" runat=\"server\" Text=\"Submit\" />
</div>
</FooterTemplate>
</asp:Repeater>
<asp:sqlDataSource ID=\"DBDataSource\" runat=\"server\"
ConnectionString=\"<%$ ConnectionStrings:connString %>\"
SelectCommand=\"usp_GetActiveQuestions\" SelectCommandType=\"StoredProcedure\">
</asp:sqlDataSource>
</asp:Panel>
解决方法
<ItemTemplate>
<div class=\"Question\">
<asp:RadioButtonList runat=\"server\" Visible=\'<%# Eval(\"QuestionType\").ToString() == \"YesNo\" %>\' >
<asp:ListItem Value=\"True\" Text=\"Yes\" />
<asp:ListItem Value=\"False\" Text=\"No\" />
</asp:RadioButtonList>
<asp:RadioButtonList runat=\"server\" Visible=\'<%# Eval(\"QuestionType\").ToString() != \"YesNo\" %>\' >
<asp:ListItem Value=\"1\" Text=\"1\" />
<asp:ListItem Value=\"2\" Text=\"2\" />
<asp:ListItem Value=\"3\" Text=\"3\" />
<asp:ListItem Value=\"4\" Text=\"4\" />
<asp:ListItem Value=\"5\" Text=\"5\" />
</asp:RadioButtonList>
</div>
</ItemTemplate>
, 您可以绑定到RadioButtonList。
这个问题的答案可能会帮助您。
如何使用c#将数据动态绑定到asp:repeater中的RadioButtonList?