【发布时间】:2014-09-13 07:58:00
【问题描述】:
我有以下包含输入单选按钮的 ListView:
<asp:ListView ID="ListView1" runat="server" DataSourceID="SqlDataSource_BGlist">
<ItemTemplate>
<input id="Radio1" name="BG_name" type="radio" value="<%# Eval("BG_fileName") % >"/>
<asp:Label ID="BG_fileNameLabel" runat="server" Text='<%# Eval("BG_fileName") %>' />
</ItemTemplate>
</asp:ListView>
<asp:SqlDataSource ID="SqlDataSource_BGlist" runat="server" ConnectionString="Data Source=tcp:cg26trmnla.database.windows.net,1433;Initial Catalog=cookniche;Integrated Security=False;User ID=PublicSQLcookniche@cg26trmnla;Password=Abounakhle80+;Connect Timeout=30;Encrypt=True" ProviderName="System.Data.SqlClient" SelectCommand="SELECT [BG_fileName] FROM [BackgroundImages]"></asp:SqlDataSource>
我想检查从后面的代码中检查了哪个单选按钮。我正在使用以下代码,但显然不正确。
foreach (ListViewItem itemRow in this.ListView1.Items)
{
RadioButton radioBtn = new RadioButton();
radioBtn = (RadioButton)itemRow.FindControl("Radio1");
if (radioBtn.Checked)
{
//do stuff
}
}
【问题讨论】:
-
你应该使用 RadioButtonList 代替,这样可以省去很多麻烦。要继续使用您的代码,请使您的单选按钮 ID 独一无二。
-
我尝试了 RadioButtonList 并将其连接到数据源以检索值,但 RadioButton 不允许您在其块中包含 html 代码,我必须为每个列表项添加图片。这就是我使用输入类型单选的原因,因为它更灵活.....
-
@Gloria: 那么你可以简单地使用 jquery 来完成
-
感谢您的帮助。我也不允许使用 jquery ......必须有一种方法可以在后面的代码中找到我的单选按钮,但我只是不知道如何......我做过一次,但不幸的是我丢失了代码....
-
@Gloria :看看我的回答。希望你能找到你的解决方案。如果对您有帮助,请不要忘记将其标记为答案。这样它也可以帮助其他开发人员。
标签: asp.net forms listview radio-button