ASP.NET单选按钮列表中的中继器? [英] ASP.NET RadioButtonList in Repeater?

查看:151
本文介绍了ASP.NET单选按钮列表中的中继器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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>

和codebehind:

and codebehind:

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。如果我写如上所定义。我得到这个errror。

I cant bound datavaluefield and datatextfiled. If I write as defined above. I got this errror.

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'.

我如何可以将绑定的值和文本。

How can I bind value and text.

感谢。

推荐答案

错误解释清楚自己:

does not contain a property with the name 'OptionName'

只要改变这(在这两个,你的声明单选按钮列表的CheckBoxList

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

要:

DataTextField="OptionName" DataValueField="OptionId"

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

And that's it, you do not need to evaluate the binding, just specify the name of the property. The rest of your code looks good

的要求,来从所选项目的单选按钮列表

As Requested, to get the selected item from the 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" />

背后code:

    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;
        }
    }

这篇关于ASP.NET单选按钮列表中的中继器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆