【问题标题】:ASP.NET RadioButtonList in Repeater?中继器中的 ASP.NET RadioButtonList?
【发布时间】:2012-06-20 02:37:11
【问题描述】:

aspx 文件:

<asp:Repeater ID="Repeater_sorular" runat="server">
   <HeaderTemplate>
   </HeaderTemplate>
   <ItemTemplate>
      <div class="div_soru">
         <div class="div_soru_wrapper">
             <%#Eval("Subject")%>
             <asp:RadioButtonList ID="RadioButtonList_secenekler" runat="server" Visible='<%# Eval("TypeId").ToString() == "1" %>'
                DataSource='<%#Eval("Secenekler")%>' DataTextField='<%#Eval("OptionName")%>' DataValueField='<%#Eval("OptionId")%>'>
             </asp:RadioButtonList>
             <asp:CheckBoxList ID="CheckBoxList_secenekler" runat="server" Visible='<%# Eval("TypeId").ToString() == "2" %>'
                DataSource='<%#Eval("Secenekler")%>' DataTextField='<%#Eval("OptionName")%>' DataValueField='<%#Eval("OptionId")%>'>
             </asp:CheckBoxList>
         </div>
      </div>
   </ItemTemplate>
   <FooterTemplate>
   </FooterTemplate>
</asp:Repeater>

和代码隐藏:

SpAnketDataContext db = new SpAnketDataContext();

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        BindRepeaterSorular();
    }
}

protected void ImageButton_kaydet_OnCommand(object source, CommandEventArgs e)
{

}

private void BindRepeaterSorular()
{
    int anket_id = 3;

    var sorular = from soru in db.TableSurveyQuestions
                  where soru.SurveyId == anket_id
                  select new
                  {
                      soru.TypeId,
                      soru.Subject,
                      soru.QuestionId,
                      soru.SurveyId,
                      soru.QueueNo,
                      SurveyTitle = soru.TableSurvey.Title,
                      TypeName = soru.TableSurveyQuestionType.TypeName,

                      Secenekler = from secenekler in soru.TableSurveyOptions
                                   select new
                                   {
                                       secenekler.OptionId,
                                       secenekler.OptionName,
                                       secenekler.QuestionId,
                                   }
                  };

    Repeater_sorular.DataSource = sorular;
    Repeater_sorular.DataBind();
}

还有我的问题:

我无法绑定 datavaluefield 和 datatextfiled。如果我按照上面的定义写。我得到了这个错误。

DataBinding: '<>f__AnonymousType1`8[[System.Nullable`1[[System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.Nullable`1[[System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.Nullable`1[[System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.Collections.Generic.IEnumerable`1[[<>f__AnonymousT...' does not contain a property with the name 'OptionName'.

如何绑定值和文本。

谢谢。

【问题讨论】:

    标签: asp.net repeater checkboxlist radiobuttonlist


    【解决方案1】:

    错误解释清楚:

    does not contain a property with the name 'OptionName'
    

    只需更改此设置(在您的 RadioButtonListCheckBoxList 声明中):

    DataTextField='<%#Eval("OptionName")%>' DataValueField='<%#Eval("OptionId")%>'
    

    收件人:

    DataTextField="OptionName" DataValueField="OptionId"
    

    就是这样,您不需要评估绑定,只需指定属性的名称。您的其余代码看起来不错

    已编辑

    根据要求,从RadioButtonList 获取所选项目:

    <asp:GridView runat="server" OnRowCommand="grdProducts_RowCommand" ID="grdProducts">
        <Columns>
            <asp:TemplateField ShowHeader="False">
                <ItemTemplate>
                    <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="false" 
                        CommandName="myLink" CommandArgument='<%# DataBinder.Eval(Container, "RowIndex") %>' Text="Button"></asp:LinkButton>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:RadioButtonList DataTextField="NestedValue" DataValueField="ID" runat="server" DataSource='<%# Eval("Nested") %>' ID="radios">
                    </asp:RadioButtonList>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>
    
    <asp:Label runat="server" ID="lblMessage" />
    

    后面的代码:

        protected void grdProducts_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "myLink":
                    var row = this.grdProducts.Rows[int.Parse(e.CommandArgument.ToString())];
                    var radios = row.FindControl("radios") as RadioButtonList;
                    this.lblMessage.Text += "<br/>" + radios.UniqueID;
                    this.lblMessage.Text += "<br/> dedede " + radios.SelectedValue;
                    break;
            }
        }
    

    【讨论】:

    • 我想问另一个问题。那么我怎样才能获得这些列表项的价值。你能给我一个关于这个的起点吗?可能是链接等。谢谢。
    • 感谢您的回答。但我想用一个提交按钮获取所有值。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多