如何在gridview的编辑模式下从下拉列表中获取所选值?

Jef*_*man 5 c# asp.net gridview editmode drop-down-menu

在我的应用程序中,当我在gridview中编辑一行时,我从下拉列表中选择一些新数据.

我正在填充这样的下拉列表:

 protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if ((e.Row.RowState & DataControlRowState.Edit) > 0)
            {
                DropDownList emailsListDL = (DropDownList)e.Row.FindControl("emailsDL");
                emailsListDL.DataSource = allUsersEmails;//list of strings with the emails of the users
                emailsListDL.DataBind();
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

但是,当我按下模板中的"更新"按钮并进入"RowUpdating"事件时,下拉列表中的选定值每次都是该下拉列表中的第一个值.

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        DropDownList emailsListDL = (DropDownList)GridViewAdvertisers.Rows[e.RowIndex].FindControl("emailsDL");
        string email = emailsListDL.SelectedValue; //the selected value is every time the first value from the dropdownlist
    }
Run Code Online (Sandbox Code Playgroud)

有没有人有任何想法?

我已经尝试了很多方法来设置'RowDataBound'事件中的选定值,但没有运气.我试过这个:

1. emailsListDL.SelectedIndex = emailsListDL.Items.IndexOf(emailsListDL.Items.FindByValue(DataBinder.Eval(e.Row.DataItem, "OwnerMail").ToString()));
2. emailsListDL.SelectedValue = GridViewAdvertisers.DataKeys[e.Row.RowIndex]["OwnerMail"].ToString();
3. emailsListDL.SelectedValue = GridViewAdvertisers.Rows[e.Row.RowIndex].Cells[1].Text;
//ownerMail is a string (object) from the list of objects that I put as datasource to the gridview
Run Code Online (Sandbox Code Playgroud)

谢谢,杰夫

更新

aspx页面中的My Item模板是:

 <asp:TemplateField ItemStyle-HorizontalAlign="Center" ItemStyle-VerticalAlign="Middle"
                ItemStyle-Width="150px" HeaderText="Owner Email" HeaderStyle-HorizontalAlign="Left"
                HeaderStyle-BorderWidth="1px" HeaderStyle-BorderColor="#e1e1e1">
                <ItemTemplate>
                    <asp:Label ID="LabelEmail" runat="server" Text='<%# Bind("OwnerMail")%>'></asp:Label>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:DropDownList ID="emailsDL" runat="server" Width="150">
                    </asp:DropDownList>
                </EditItemTemplate>
                <HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" Font-Bold="True"></HeaderStyle>
                <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="180px" BorderWidth="1px"
                    BorderColor="#e1e1e1"></ItemStyle>
    </asp:TemplateField>
Run Code Online (Sandbox Code Playgroud)

WEF*_*EFX 1

如果您未在 DropDownList 定义中定义 SelectedIndex,则 SelectedIndex 将始终默认为 0。

编辑:@Tim Schmelter 应该添加他的评论作为答案。与此同时,我将为其他读者解释一下:您需要检查回发(请参阅上面的评论)。