当前位置:编程学习 > C#/ASP.NET >>

GridView嵌套RadioButtonList的绑定问题

--------------------编程问答-------------------- 我顶我顶我顶 --------------------编程问答-------------------- 参考下 

  <asp:GridView ID="GridView1" runat="server">
    <asp:TemplateField HeaderText="性别">
                        <ItemTemplate>
                            <asp:Label ID="lblSex" runat="server" Text='<%# Eval("sex") %>'></asp:Label>
                      
                            <asp:RadioButtonList ID="rblSex" runat="server">
                            </asp:RadioButtonList>

                            <asp:HiddenField ID="id" runat="server" Value='<%# Eval("id") %>' />
             
                    </asp:TemplateField>
    </asp:GridView>


   protected void gvUser_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            //判断是否是数据项
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //判断是否是编辑状态或交替行
                if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
                {
                   
                    RadioButtonList rblSex = (RadioButtonList)e.Row.FindControl("rblSex");//找到RadioButtonList
                    string id =( (HiddenField)e.Row.FindControl("rblSex")).Value;//找到关联键
                    rblSex.DataSource = xxxManager.GetXXById(id);//绑定RadioButtonList
                    rblSex.DataBind();
     
                }
            }
        }
--------------------编程问答-------------------- 是一种方法,
但是能否不在_RowDataBound里循环调用,
有没有方案支持在绑定GridView时直接将objectB.propertyA绑定给radiobuttonlist的?


引用 2 楼  的回复:
参考下 

  <asp:GridView ID="GridView1" runat="server">
    <asp:TemplateField HeaderText="性别">
                        <ItemTemplate>
                            <asp:Label ID="lblSex" runat="serv……
--------------------编程问答-------------------- 找到了一个折中的方案:


List<Option> Options = DataBinder.Eval(e.Row.DataItem, "Options") as List<Option>;
RadioButtonList rbl = e.Row.FindControl("rblOption") as RadioButtonList;
rbl.DataSource=Options;
rbl.DataBind();
--------------------编程问答-------------------- 目的是不再循环里调用数据库接口。
引用 4 楼 sapphirehdl 的回复:
找到了一个折中的方案:

C# code?1234List<Option> Options = DataBinder.Eval(e.Row.DataItem, "Options") as List<Option>;RadioButtonList rbl = e.Row.FindControl("rblOption") as RadioButtonList;rbl.Da……
--------------------编程问答-------------------- 参考下 

  <asp:GridView ID="GridView1" runat="server">
    <asp:TemplateField HeaderText="性别">
                        <ItemTemplate>
                            <asp:Label ID="lblSex" runat="server" Text='<%# Eval("sex") %>'></asp:Label>
                      
                            <asp:RadioButtonList ID="rblSex" runat="server">
                            </asp:RadioButtonList>

                            <asp:HiddenField ID="id" runat="server" Value='<%# Eval("id") %>' />
             
                    </asp:TemplateField>
    </asp:GridView>



   protected void gvUser_RowDataBound(object sender, GridViewRowEventArgs e)

        {

            //判断是否是数据项

            if (e.Row.RowType == DataControlRowType.DataRow)

            {

                //判断是否是编辑状态或交替行

                if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)

                {

                   

                    RadioButtonList rblSex = (RadioButtonList)e.Row.FindControl("rblSex");//找到RadioButtonList

                    string id =( (HiddenField)e.Row.FindControl("rblSex")).Value;//找到关联键

                    rblSex.DataSource = xxxManager.GetXXById(id);//绑定RadioButtonList

                    rblSex.DataBind();

     

                }

            }

        }
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,