日期:2014-05-17  浏览次数:20491 次

gridview中嵌套dropdownlist和textbox的问题
<asp:TemplateField HeaderText="调整原因" >
  <ItemStyle HorizontalAlign="Center" />
  <ItemTemplate>
  <asp:DropDownList ID="ddladjustreason" runat="server" OnTextChanged="ddladjustreason_TextChanged" AutoPostBack="True">
  <asp:ListItem Value="0">其他</asp:ListItem>
  <asp:ListItem Value="1">清零</asp:ListItem>
  </asp:DropDownList>
  </ItemTemplate>
</asp:TemplateField>
  <asp:TemplateField HeaderText="调整原因说明">
  <EditItemTemplate>
  <asp:TextBox ID="txtadjustreason" runat="server" Text="" Width="50px"></asp:TextBox></asp:TextBox>
  </EditItemTemplate>
</asp:TemplateField>




protected void btnAd_Click(object sender, EventArgs e)
{
  DataTable dt;
  if (ViewState["Tddetail"] == null)
  {
  dt = new DataTable();
  DataColumn dc = null;
  dc = dt.Columns.Add("ID", Type.GetType("System.Int32"));
  dc.AutoIncrement = true;//自动增加
  dc.AutoIncrementSeed = 1;//起始为1
  dc.AutoIncrementStep = 1;//步长为1
  dc.AllowDBNull = false;//
  dc = dt.Columns.Add("调整原因", Type.GetType("System.String"));
  dc = dt.Columns.Add("调整原因说明", Type.GetType("System.String"));
  }
  else
  {
  dt = ViewState["Tddetail"] as DataTable;
  }


  DataRow newRow;
  newRow = dt.NewRow();
  newRow["调整原因"] = "";
  newRow["调整原因说明"] = "";//此时dt中的调整原因和调整原因说明都是空
  gvData.EditIndex = gvData.Rows.Count;
  gvData.DataSource = dt;
  gvData.DataBind();
  ViewState["Tddetail"] = dt;
}




  protected void gvData_RowUpdating(object sender, GridViewUpdateEventArgs e)
  {

  DataTable dt;
  dt = ViewState["Tddetail"] as DataTable;

  DataRow UpdateRow;
  UpdateRow = dt.Rows[e.RowIndex];
  string adjustreason = "1";
  string adjustreasondetail = "测试";
  UpdateRow["调整原因"] = adjustreason ; //dt中应为1
  UpdateRow["调整原因说明"] = adjustreasondetail;//dt中应为测试

  ((DropDownList)gvData.Rows[e.RowIndex].FindControl("ddladjustreason")).SelectedValue = adjustreason;//应为1,对应的就是清零
  ((TextBox)gvData.Rows[e.RowIndex].FindControl("txtadjustreaon")).Text = adjustreasondetail; //应为测试
   
  gvData.EditIndex = -1;
  gvData.DataSource = dt;
  gvData.DataBind();
  ViewState["Tddetail"] = dt;
  ((DropDownList)gvData.Rows[e.RowIndex].FindControl("ddladjustreason")).Enabled = false;
  }

可是我在点更新后,调整原因说明为空,调整原因显示的是默认的“其他”而不是我手工设为1的“清零”,请问这是怎么回事?

------解决方案--------------------
探讨

还有个问题,每次我调用btnAd_Click时,gridview里所有的dropdownlist都被启用了,我希望是插入进gridview是哪行就启用哪行的dropdownlist,这个应该怎么设置?

------解决方案--------------------