2

我的 gridview 有 2 列 - DropDownList & TextBox。DDL 由数据表进行数据绑定。DDL 的 SelectedIndex 更改将填充其文本框并添加新行。所有这些都可以正常工作,但是当添加新行时,先前行的 DDL 的选定值会重置为 0 索引。尽管文本框值保持不变。添加新行时如何保留 DDL 选择的值。

例如。如果我从第 1 行的下拉列表中选择“abc”,它会填充其文本字段并添加一个新行。但是随着回发的发生,abc 不会在第 1 行 ddl 中保持选中状态:

  登录电话

1 123456789

2

ASPX:

<asp:GridView ID="gvCommissions" runat="server"
 OnRowDataBound="gvCommissions_RowDataBound">
 <Columns>
  <asp:TemplateField HeaderText="LOGIN" ItemStyle-Width="29%">
   <ItemTemplate>
    <asp:DropDownList ID="ddlLogin" runat="server" Width="98%" 
     DataTextField="login" DataValueField="id"
     OnSelectedIndexChanged="ddlLogin_SelectedIndexChanged" AutoPostBack="true" >
     </asp:DropDownList>
   </ItemTemplate>
  </asp:TemplateField>
  <asp:TemplateField HeaderText="PHONE" ItemStyle-Width="12%">
   <ItemTemplate>
    <asp:TextBox ID="txtPhone" runat="server" Width="98%" 
    Text='<%# Eval("phone")%>' Enabled="false"></asp:TextBox>
   </ItemTemplate>
  </asp:TemplateField>
 </Columns>
</asp:GridView>

代码背后:

protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
         //Get values from DB, store to a datatable and then to a viewstate               
            GetAgentDetails();
         //Adds an empty row to grid                
            SetInitialRow(); 
        }            
    }

private void AddNewRow()
    {
      //Create a datatable 
      dtCurrentData = new DataTable();
      dtCurrentData.Columns.Add("phone");

//Store values of each row to a new datarow. Add all rows to datatable
      foreach (GridViewRow gvRow in gvCommissions.Rows)
        {
          DataRow drcurrentrow = dtCurrentData.NewRow();                      
          drcurrentrow["phone"] = ((TextBox)gvRow.FindControl("txtphone")).Text;
          dtCurrentData.Rows.Add(drcurrentrow);
        }

      //create an empty datarow and also add it to the new datatable.
        DataRow dr = dtCurrentData.NewRow();
        dr["phone"] = "";
        dtCurrentData.Rows.Add(dr);

     //Bind the new datatable to the grid
       gvCommissions.DataSource = dtCurrentData;
       gvCommissions.DataBind();
   }

protected void gvCommissions_RowDataBound(object sender, GridViewRowEventArgs e)
    {
     if (e.Row.RowType == DataControlRowType.DataRow)
      {
        DropDownList ddl = (DropDownList)e.Row.FindControl("ddlLogin");
        dtAgents = (DataTable)ViewState["AgentsTable"];
        ddl.DataSource = dtAgents;
        ddl.DataBind();
        ddl.Items.Insert(0, "");
      }            
    }
4

2 回答 2

4

当您在 AddNewRow 中进行数据绑定时,原始行将丢失。您必须将下拉选择的索引与您的文本框电话值一起存储在 dtCurrentData 中。然后,您可以使用保存在 dtCurrentData 中的值在 RowDataBound 事件中设置下拉列表的索引。

private void AddNewRow()
{
  //Create a datatable 
  dtCurrentData = new DataTable();
  dtCurrentData.Columns.Add("phone");
  //new column for dropdown index
  dtCurrentData.Columns.Add("ddlIndex");

//Store values of each row to a new datarow. Add all rows to datatable
  foreach (GridViewRow gvRow in gvCommissions.Rows)
    {
      DataRow drcurrentrow = dtCurrentData.NewRow();                      
      drcurrentrow["phone"] = ((TextBox)gvRow.FindControl("txtphone")).Text;
      //get dropdown index
      drcurrentrow["ddlIndex"] = ((DropDownList)gvRow.FindControl("ddlLogin")).SelectedIndex;
      dtCurrentData.Rows.Add(drcurrentrow);
    }

  //create an empty datarow and also add it to the new datatable.
    DataRow dr = dtCurrentData.NewRow();
    dr["phone"] = "";
    //initial drop down index
    dr["ddlIndex"] = 0;
    dtCurrentData.Rows.Add(dr);

    //save to the viewstate like your AgentsTable
    ViewState["CurrentData"] = dtCurrentData;

 //Bind the new datatable to the grid
   gvCommissions.DataSource = dtCurrentData;
   gvCommissions.DataBind();
}

protected void gvCommissions_RowDataBound(object sender, GridViewRowEventArgs e)
{
 if (e.Row.RowType == DataControlRowType.DataRow)
  {
    DropDownList ddl = (DropDownList)e.Row.FindControl("ddlLogin");
    dtAgents = (DataTable)ViewState["AgentsTable"];
    ddl.DataSource = dtAgents;
    ddl.DataBind();
    ddl.Items.Insert(0, "");

    //get the dropdown index from CurrentData
    //use the current gridview's row index, since it matches the datatable
    if (ViewState["CurrentData"] != null)
    {
     DataTable dtCurrentData = (DataTable)ViewState["CurrentData"];
     ddl.SelectedIndex = Convert.ToInt32(dtCurrentData.Rows[e.Row.RowIndex]["ddlIndex"]);
    }

  }            
}
于 2013-08-13T10:22:47.477 回答
1

RowDataBound每次都被触发(无论页面是否为pagePostBack)。

您需要if(!IsPostBack)在您为RowDataBound事件中的下拉列表执行数据绑定的代码周围添加该条件。

于 2013-08-13T08:53:15.290 回答