none
设置GridView->EditItemTemplate中RadioButtonList的值 RRS feed

  • 问题

  • <asp:GridView DataSourceID="ads" runat="server" EmptyDataText="选定时间暂无工作" ID="GridView1" OnRowUpdating="GridView1_RowUpdating" DataKeyNames="ID" AutoGenerateColumns="false" AutoGenerateEditButton="true">

    我有一列数据,有三种可能的值(上午、下午、全天)。希望在用户点击AutoGenerateEditButton生成的编辑的时候,在<EditItemTemplate>中显示一个RadioButtonList供用户来选择。并且,默认选项是之前数据库中已经选定的,应该如何操作?

    使用GridView1_RowEditing获取不到EditItemTemplate中的控件………………

    目前这一列的代码:正常时候显示已经选择的时间,点击编辑显示radiobuttonlist.
                    <asp:TemplateField ItemStyle-Width="15%">
                        <HeaderTemplate>时间</HeaderTemplate>
                        <ItemTemplate><%#Eval("时间段")%></ItemTemplate>
                        <EditItemTemplate>
                            <asp:RadioButtonList runat="server" CssClass="form-control" ID="RadioButtonList2"  >
                                <asp:ListItem Text="上午" ></asp:ListItem>
                                <asp:ListItem Text="下午" ></asp:ListItem>
                                <asp:ListItem Text="全天" ></asp:ListItem>
                            </asp:RadioButtonList>
                        </EditItemTemplate>
                    </asp:TemplateField>


    2018年12月3日 12:39

答案

  • 你好,

    据我所知,如果你想要在绑定radiobuttonlist的默认选中值,我建议你可以在gridView1_RowDataBound事件中进行操作。

    你可以添一个hiddenfield在edittemplate里面,用来获取当前行的时间段。

    然后你可以通过Row.FindControl方法来获取页面的radiobutton。

    具体你可以参照如下代码:

    ASPX:

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="GridviewAutoGenerated.aspx.cs" Inherits="AspNetNormalIssue.Webform.GridviewAutoGenerated" %>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
    
                <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="MyId" DataSourceID="SqlDataSource1" AutoGenerateEditButton="true"   OnRowDataBound="gridView1_RowDataBound" >
                    <Columns>
                        <asp:BoundField DataField="MyId" HeaderText="MyId" InsertVisible="False" ReadOnly="True" SortExpression="MyId" />
                        <asp:BoundField DataField="FriendId" HeaderText="FriendId" SortExpression="FriendId" />
                        <asp:TemplateField HeaderText="FriendStatus" SortExpression="FriendStatus">
                            <EditItemTemplate>
                                <asp:HiddenField ID="HiddenField1" runat="server"   Value='<%# Bind("FriendStatus") %>' />
                             
                                <asp:RadioButtonList runat="server" CssClass="form-control" ID="RadioButtonList2"  >
                                <asp:ListItem Text="上午" ></asp:ListItem>
                                <asp:ListItem Text="下午" ></asp:ListItem>
                                <asp:ListItem Text="全天" ></asp:ListItem>
                            </asp:RadioButtonList>
                            </EditItemTemplate>
                            <ItemTemplate>
    
    
                                <asp:Label ID="Label1" runat="server" Text='<%# Bind("FriendStatus") %>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>
                <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:aspnet-MVCApplication-20180424014300ConnectionString %>" SelectCommand="SELECT * FROM [Friends]"></asp:SqlDataSource>
    
            </div>
        </form>
    </body>
    </html>
    

    后台:

            protected void gridView1_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    if ((e.Row.RowState & DataControlRowState.Edit) > 0)
                    {
                        var radio = (RadioButtonList)e.Row.FindControl("RadioButtonList2");
                        var re = (HiddenField)e.Row.FindControl("HiddenField1");
                        if (re.Value == "0")
                        {
                            radio.SelectedIndex = 1;
                        }
     
                    }
                }
            }

    截图:


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2018年12月10日 5:26

全部回复

  • 你好,

    据我所知,如果你想要在绑定radiobuttonlist的默认选中值,我建议你可以在gridView1_RowDataBound事件中进行操作。

    你可以添一个hiddenfield在edittemplate里面,用来获取当前行的时间段。

    然后你可以通过Row.FindControl方法来获取页面的radiobutton。

    具体你可以参照如下代码:

    ASPX:

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="GridviewAutoGenerated.aspx.cs" Inherits="AspNetNormalIssue.Webform.GridviewAutoGenerated" %>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
    
                <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="MyId" DataSourceID="SqlDataSource1" AutoGenerateEditButton="true"   OnRowDataBound="gridView1_RowDataBound" >
                    <Columns>
                        <asp:BoundField DataField="MyId" HeaderText="MyId" InsertVisible="False" ReadOnly="True" SortExpression="MyId" />
                        <asp:BoundField DataField="FriendId" HeaderText="FriendId" SortExpression="FriendId" />
                        <asp:TemplateField HeaderText="FriendStatus" SortExpression="FriendStatus">
                            <EditItemTemplate>
                                <asp:HiddenField ID="HiddenField1" runat="server"   Value='<%# Bind("FriendStatus") %>' />
                             
                                <asp:RadioButtonList runat="server" CssClass="form-control" ID="RadioButtonList2"  >
                                <asp:ListItem Text="上午" ></asp:ListItem>
                                <asp:ListItem Text="下午" ></asp:ListItem>
                                <asp:ListItem Text="全天" ></asp:ListItem>
                            </asp:RadioButtonList>
                            </EditItemTemplate>
                            <ItemTemplate>
    
    
                                <asp:Label ID="Label1" runat="server" Text='<%# Bind("FriendStatus") %>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>
                <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:aspnet-MVCApplication-20180424014300ConnectionString %>" SelectCommand="SELECT * FROM [Friends]"></asp:SqlDataSource>
    
            </div>
        </form>
    </body>
    </html>
    

    后台:

            protected void gridView1_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    if ((e.Row.RowState & DataControlRowState.Edit) > 0)
                    {
                        var radio = (RadioButtonList)e.Row.FindControl("RadioButtonList2");
                        var re = (HiddenField)e.Row.FindControl("HiddenField1");
                        if (re.Value == "0")
                        {
                            radio.SelectedIndex = 1;
                        }
     
                    }
                }
            }

    截图:


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2018年12月10日 5:26
  • 哇 太棒了!谢谢谢谢!asp:HiddenField和RowDataBound的使用学习了!
    2018年12月20日 7:31