【问题标题】:Find which input radio button is checked in code behind查找在后面的代码中检查了哪个输入单选按钮
【发布时间】: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


【解决方案1】:

你做的几乎是对的。在您的代码中只需要更改一些小事情。 在您的单选按钮中添加runat="Server"。因为如果它不是runat="server",那么您将不会在后面的代码中找到单选按钮。看看下面的 HTML 代码:

<asp:ListView ID="ListView1" runat="server" DataSourceID="SqlDataSource_BGlist">
        <ItemTemplate> 
            <input id="Radio1" runat="server" name="BG_name" type="radio" value="<%# Eval("BG_fileName") %>"/>      
            <asp:Label ID="BG_fileNameLabel" runat="server" Text='<%# Eval("BG_fileName") %>' />
        </ItemTemplate>
    </asp:ListView>

在您后面的代码中,您正在转换为RadioButton,此 RadioButton 表示单选按钮的服务器端控制。而不是你应该使用HtmlInputRadioButton,如下所示:

foreach (ListViewItem itemRow in this.ListView1.Items)
{
    var radioBtn = (HtmlInputRadioButton)itemRow.FindControl("Radio1");
    if (radioBtn.Checked)
    {
        // Do Stuff                    
    }
}

使用 RadioButton 应用分组

<asp:ListView ID="ListView1" runat="server">
    <ItemTemplate>
        <input runat="server" name="BG_name" type="radio" ID="radio1" value='<%# Eval("Id") %>' ClientIDMode="Static"  class="radioBGName" />
        <asp:Label ID="BG_fileNameLabel" runat="server" Text='<%# Eval("Title") %>' />
    </ItemTemplate>

</asp:ListView>

<script type="text/javascript">

    $('.radioBGName').click(function () {

        var controlId = $(this).attr('name');
        $('.radioBGName').each(function () {
            if (controlId != $(this).attr('name')) {

                $(this).removeAttr('checked');
            }
        });
    });
</script>

【讨论】:

  • 不幸的是,添加 runat=server 后出现以下错误:服务器标签格式不正确
  • "/>
  • 更新您的输入标签,如此处所述&lt;input id="Radio1" name="BG_name" type="radio" runat="server" value='&lt;%# Eval("BG_fileName") %&gt;'/&gt;
  • 谢谢@SpiderCode....现在可以了。我用你的建议更新了答案: INSTEAD OF "/> 注意Value中的引号
  • 唯一有待解决的错误行为是如何只选择一个按钮....现在我可以同时选择多个按钮
【解决方案2】:
protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
{
    if (e.Item.ItemType == ListViewItemType.DataItem)
    {
        RadioButton c1 = (RadioButton)e.Item.FindControl("Radio1");
        if (radioBtn.Checked)
        {
           //do stuff                    
        }
    }
}

更新Page_Load 中,您需要知道行索引并像这样检索控件

RadioButton radio= this.ListView1.Items[<row_index>].FindControl("Radio1") as RadioButton

【讨论】:

  • 感谢代码......是否可以触发页面回发的代码?如果是怎么办?
  • 查看更新后的答案!如果是这样,请接受它是正确的。
猜你喜欢
  • 2019-01-25
  • 2013-06-11
  • 2012-04-17
  • 1970-01-01
  • 2016-02-01
  • 1970-01-01
  • 2022-09-22
  • 2011-07-02
  • 2019-01-13
相关资源
最近更新 更多