【问题标题】:How can I get the selected value from dropdownlist in edit mode of a gridview?如何在gridview的编辑模式下从下拉列表中获取选定的值?
【发布时间】:2011-05-12 13:51:59
【问题描述】:

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

【问题讨论】:

  • 您是否在每次回发时或仅在 !Page.IsPostback 时对您的 GridView 进行数据绑定?你应该只在第一次这样做,并且在你改变它的数据源之后这样做。
  • 是的,我在每个 page_Load 都对 gridview 进行数据绑定
  • 用 if (!Page.IsPostBack){}括起来
  • AAAA 这救了我的命!!没想到这么傻!非常感谢!

标签: c# asp.net gridview drop-down-menu editmode


【解决方案1】:

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

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

【讨论】:

  • 我的下拉列表定义是这样的:跨度>
【解决方案2】:

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

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

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

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

【讨论】:

    【解决方案3】:

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

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

    【讨论】:

      【解决方案4】:

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

      所以一定要检查任何其他位置您可能在代码中的任何位置绑定。 希望这对其他人有帮助。

      protected void GridView1_RowCommand(对象发送者,GridViewCommandEventArgs e) { 字符串 commandName = e.CommandName; 开关(命令名) { 案例“第1页”: 处理页面命令(e); //绑定应该发生在这里 休息; } //此行位置错误,导致bug BindGrid1(); }

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-11-26
        • 2021-01-01
        • 1970-01-01
        • 2014-10-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多