如何在GridView编辑模式下获取下拉列表的选择值?

5

在我的应用程序中,当我编辑网格视图中的一行时,我会从下拉列表中选择一些新数据。

我是这样填充下拉列表的:

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

但是当我从模板按下“更新”按钮并进入“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
    }

有人有什么想法吗?

我尝试了很多方法在“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

谢谢,Jeff
更新
我的aspx页面中的项模板如下:
 <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>

4
你是每次页面回传都会对 GridView 进行数据绑定,还是只在 !Page.IsPostback 的情况下才进行绑定?你应该只在第一次绑定时以及在更改数据源之后进行绑定。 - Tim Schmelter
是的,我正在每次页面加载时进行GridView数据绑定。 - Jeff Norman
1
使用 if (!Page.IsPostBack){} 包裹它。 - Pankaj
AAAA 这个救了我的命啊!!我没想到它会那么蠢!非常感谢! - Jeff Norman
4个回答

1

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

编辑:@Tim Schmelter应将他的评论添加为答案。同时,我会为其他读者进行解释:您需要检查是否有回发(请参见上面的评论)。


我的下拉菜单定义如下:<EditItemTemplate> <asp:DropDownList ID="emailsDL" runat="server" Width="150"> </asp:DropDownList> </EditItemTemplate> - Jeff Norman

0

我曾经遇到过同样的问题,但是采用了不同的解决方案。我在GridView上实现了自定义分页和自定义分页器,并添加了RowCommand方法,在此过程中使用新的页面索引重新绑定了网格。然而,正如所发生的那样,RowCommand方法也会在更新期间被调用。

因此,请务必检查您在代码中任何其他位置可能进行绑定的地方。希望这能帮助其他人。

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
    string commandName = e.CommandName;
switch (commandName) { case "Page1": HandlePageCommand(e); //绑定应该在这里发生 break; }
//此行位于错误的位置,导致错误 BindGrid1(); }

0

您可以声明一个 ComboBox mainBX = new Combobox();,并且您可以声明一个事件

private void onSeletedIndexChange(object sender,EventArgs e)
{
        mainBX = (ComboBox)(sender);
}

接下来,您应该在GridView中迭代foreach ComboBox并添加此事件。 然后,您需要做的就是获取mainBX.seletedItem();

我认为这就是您所需要的,如果不是,请见谅。


0

RowDataBound事件中设置选定值,应该像这样做:

(DropDownList)e.Row.Cells[/*index*/].Controls[/*index, or use FindControl*/]).Items.FindByValue(((DataRowView)e.Row.DataItem).Row.ItemArray[/*columnnumber*/].ToString()).Selected = true;

FindByValue函数在“正常”视图模式下查找字段的当前文本值,并将DropDownList设置为该值。

当然,这需要封装在中。

if ((e.Row.RowState & DataControlRowState.Edit) == DataControlRowState.Edit)
{
}

关于你的“获取更新值”的问题,我必须诚实地说我不知道,因为你的方法和我的完全一样,唯一的区别是:我的方法可行。
如果有帮助的话,这是我的代码:
 protected void gridVariables_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {

        string control = ((DropDownList)gridVariables.Rows[e.RowIndex].Cells[3].Controls[1]).SelectedValue;
        gridVariables.EditIndex = -1;
        this.gridVariables_DataBind(control, e.RowIndex);
    }

private void gridVariables_DataBind(string control, int index)
    {
        DataTable dt = (DataTable)Session["variableTable"]; //features a DataTable with the Contents of the Gridview
        dt.Rows[index]["ControlType"] = control;
        gridVariables.DataSource = dt;
        gridVariables.DataBind();
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接