2011-05-12 97 views
4

在我的应用程序中,当我在gridview中编辑一行时,我从下拉列表中选择了一些新数据。如何在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

你DataBind每个回发你的GridView或只有'!Page.IsPostback'?你应该只在第一次和你改变它的数据源之后这样做。 – 2011-05-12 13:56:51

+0

是的,我databinding在每一页的gridview_Load – 2011-05-12 14:03:16

+0

然后你有你的答案;) – 2011-05-12 14:04:26

回答

1

将selectedIndex将始终默认为0,如果你没有在你的DropDownList定义中定义它。

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

+0

我的下拉的定义是这样的: 2011-05-12 14:01:49

0

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

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

和未来,你应该在迭代的foreach GridView的组合框和添加此事件。比所有你应该做的是,采取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发现场的电流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(); 
    } 
0

我曾与一个不同的解决同样的问题,我已经实现自定义分页的GridView控件与自定义寻呼机,并在过程中加入的RowCommand方法,这是一个新的页面索引重新绑定网格。虽然RowCommand方法在更新过程中也被调用,但它发生了。

所以一定要检查其他位置你可能会在你的代码中的任何地方绑定。 希望这可以帮助别人。

 
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) 
{ 
    string commandName = e.CommandName; 

    switch (commandName) 
     { 
      case "Page1": 
       HandlePageCommand(e); 
       //binding should happen here 
       break; 
     } 

     //this line is in the wrong location, causing the bug  
     BindGrid1(); 
} 
相关问题