【问题标题】:Selecting a value in a RadioButtonList from data in a Repeater control从 Repeater 控件中的数据中选择 RadioButtonList 中的值
【发布时间】:2012-08-14 00:05:48
【问题描述】:

我有一个中继器控件,其中有两个单选按钮选择性别(男性,女性)。 当我保存页面时,我将选定的值“M”或“F”保存到数据中。当我加载页面时,我希望根据保存的内容选择单选按钮。

因此,在这种情况下,我的中继器具有 (Container.DataItem).Sex,它等于“M”或“F”。如何在我的单选按钮中使用这些数据来选择合适的值?

这是我想使用的那种功能(但它不存在):

<asp:RadioButtonList runat="server" SelectedValue='<%# ((Dependent)(Container.DataItem)).Sex %>' />

请注意,我无法操作代码隐藏中的单选按钮,因为我无权访问各个中继器项目。

【问题讨论】:

    标签: asp.net


    【解决方案1】:

    您需要做的是使用 2 个RadioButton 控件(1 个女性,1 个男性)。

    然后指定GroupName,以便在选择另一个时取消选择一个。 假设GroupName="Sex"

    然后根据你的DataItem指定每个控件的检查时间:

    <asp:RadioButton ID="radioMale" runat="server" GroupName="Sex" Checked="<%# ((Dependent)(Container.DataItem)).Sex == 'M' %>" />
    <asp:RadioButton ID="radioFemale" runat="server" GroupName="Sex" Checked="<%# ((Dependent)(Container.DataItem)).Sex == 'F' %>" />
    

    【讨论】:

    • 谢谢,这是最接近我目标的解决方案。
    【解决方案2】:

    在这里查看这个答案Setting selected value on a dropdownlist from codebehind in a repeater in a formview,您应该能够执行以下操作:

    <asp:RadioButtonList runat="server" SelectedValue='<%# Eval("Sex") %>' />
    

    【讨论】:

      【解决方案3】:

      请注意,我无法操作 隐藏代码,因为我无权访问单个中继器 项目。

      这就是ItemDataBound 的用途...

       <asp:Repeater id="Repeater1" OnItemDataBound="repeaterDatabound" runat="server"> 
      

      关于后面的代码:

      protected void repeaterDatabound(object Sender, RepeaterItemEventArgs e)
      {
          if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
          {
              (e.Item.FindControl("Repeater1") as RadioButtonList).SelectedValue=((Dependent)e.Item.DataItem).Sex.ToString();
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-03-11
        • 2011-08-05
        • 1970-01-01
        相关资源
        最近更新 更多