如何从asp.net 3.5中的网格视图中选择下拉列表项

时间:2014-04-02 13:05:03

标签: c# asp.net

我正在使用网格视图。在这里,我只想添加一个管理员可以从下拉列表中更改值的功能。这是我的网格视图设计:

<asp:TemplateField HeaderText="Status" SortExpression="status_name">
    <HeaderTemplate>
        <asp:LinkButton ID="lbut_sortstatus1" runat="server" CommandArgument="status_name" CommandName="Sort" CssClass="normaltext" Font-Bold="true" Text="Status">
        </asp:LinkButton>
        <asp:PlaceHolder ID="placeholderstatus1" runat="server"></asp:PlaceHolder>
    </HeaderTemplate>
    <ItemTemplate>
        <asp:UpdatePanel ID="UpdatePanel2" runat="server">
            <ContentTemplate>
                <asp:DropDownList ID="DDL_StatusList1" runat="server" DataTextField="status_name" DataValueField="Id" AppendDataBoundItems="true" AutoPostBack="True" onselectedindexchanged="DDL_GroupList1_SelectedIndexChanged">
                </asp:DropDownList>
            </ContentTemplate>
            <Triggers>
                <asp:AsyncPostBackTrigger ControlID ="DDL_StatusList1" EventName="SelectedIndexChanged" />  
            </Triggers>
        </asp:UpdatePanel>
        <asp:HiddenField ID="hd_status" runat="server" Value='<%#Eval("status_name") %>'/>
    </ItemTemplate>

这是我的代码:

    protected void GV_ViewUserList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
          if (e.Row.RowType == DataControlRowType.DataRow)
           {
                DropDownList ddl = (DropDownList)e.Row.Cells[7].FindControl("DDL_StatusList1");
                HiddenField hdValue = ((HiddenField)e.Row.FindControl("hd_status"));
                System.Web.UI.WebControls.ListItem item1 = ddl.Items.FindByText(hdValue.Value.ToString());
                if (item1 != null)
                {
                    item1.Selected = true;
                }
                DropDownList ddl1 = (DropDownList)e.Row.Cells[8].FindControl("DDL_GroupList1");
                HiddenField hdValue1 = ((HiddenField)e.Row.FindControl("hd_group"));
                System.Web.UI.WebControls.ListItem item2 = ddl1.Items.FindByText(hdValue1.Value.ToString());
                if (item2 != null)
                {
                    item2.Selected = true;
                }
            }
        }
      }

我该怎么办?

请帮帮我..

1 个答案:

答案 0 :(得分:0)

简单使用这种技术:

DropDownList ddl = (DropDownList)e.Row.FindControl("DDL_StatusList1");
            ddl.SelectedValue = DataBinder.Eval(e.Row.DataItem,"status_id").ToString();
            if (DataBinder.Eval(e.Row.DataItem, "group_id").ToString() != "")
            {
            DropDownList ddl1 = (DropDownList)e.Row.FindControl("DDL_GroupList1");
            ddl1.SelectedValue = DataBinder.Eval(e.Row.DataItem, "group_id").ToString();
            }