5

在我的应用程序中,当我在 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();
            }
        }
    }

但是,当我按下模板中的“更新”按钮并输入“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

谢谢,杰夫

更新

我来自 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

4 回答 4

1

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

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

于 2011-05-12T13:55:39.907 回答
0

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

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

接下来你应该在 GridView 中迭代 foreach ComboBox 并添加这个 Event 。你应该做的就是,拿 mainBX.seletedItem();

我想这就是你需要的,如果不道歉的话。

于 2011-05-12T19:10:02.773 回答
0

要在 -Event 中设置选定值RowDataBound,您应该这样做:

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

FindByValue 在“正常”视图模式下查找 Field 的当前 Textvalue,并将 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();
    }
于 2011-05-13T07:13:16.977 回答
0

我在使用不同的解决方案时遇到了同样的问题,我使用自定义分页器在 GridView 上实现了自定义分页,并在此过程中添加了 RowCommand 方法,该方法使用新的页面索引重新绑定网格。尽管在更新期间也调用了 RowCommand 方法,但它发生了。

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

protected void GridView1_RowCommand(对象发送者,GridViewCommandEventArgs e)
{
    字符串 commandName = e.CommandName;

    开关(命令名)
        {
            案例“第1页”:
                处理页面命令(e);
                //绑定应该发生在这里
                休息;
        }

        //此行位置错误,导致bug    
        BindGrid1();
}
于 2013-08-13T23:51:03.563 回答