2014-10-08 75 views
0

我有一個gridview文本框的項目號,desc數量,成本,extncost。當itemno在文本框desc中輸入並且項目的成本將通過文本框事件更改自動發生。 這裏,當我在網格視圖中添加一個新行時,最後輸入的值的值就消失了。 當我使用斷點檢查時,我能夠看到數據表中最後輸入的值。 由於在添加新行時有空白文本框,現在系統會考慮用於文本更改事件的空白文本框。所以,最後輸入的值也不顯示。一行中的文本框更改也會影響其他行。如何在添加新行後在gridview中保留行值?

這裏是ASPX.page代碼:

<asp:UpdatePanel ID="gin_pnlupdt" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="true"> 
       <ContentTemplate> 
        <asp:GridView ID="grv_gindet" runat="server" ShowFooter="True" AutoGenerateColumns="False" 
         CellPadding="4" ForeColor="#333333" GridLines="None" OnRowDeleting="grvStudentDetails_RowDeleting" 
          OnRowDataBound="grv_gindtrowcmd" OnRowCommand="grv_gindetrowcmd"> 
         <Columns> 
          <asp:BoundField DataField="RowNumber" HeaderText="SNo" /> 
          <asp:TemplateField HeaderText="Item Number"> 
           <ItemTemplate> 
            <asp:TextBox ID="txt_itemno" runat="server" OnTextChanged="txt_itemno_changed" AutoPostBack="True"></asp:TextBox> 
           </ItemTemplate> 
          </asp:TemplateField> 
          <asp:TemplateField HeaderText="Item Description"> 
           <ItemTemplate> 
            <asp:Label ID="txt_itemdesc" runat="server"></asp:Label> 
           </ItemTemplate> 
          </asp:TemplateField> 
          <asp:TemplateField HeaderText="Quantity"> 
           <ItemTemplate> 
            <asp:TextBox ID="txt_qty" runat="server"></asp:TextBox> 
           </ItemTemplate> 
          </asp:TemplateField> 
          <asp:TemplateField HeaderText="Unit Cost"> 
           <ItemTemplate> 
            <asp:Label ID="txt_ucost" runat="server"></asp:Label> 
           </ItemTemplate> 
          </asp:TemplateField> 
          <asp:TemplateField HeaderText="Extended Cost"> 
           <ItemTemplate> 
            <asp:Label ID="txt_extncost" runat="server"></asp:Label> 
           </ItemTemplate> 
           <FooterStyle HorizontalAlign="Right" /> 
           <FooterTemplate> 
            <asp:Button ID="ButtonAdd" runat="server" Text="Add New Row" OnClick="BtnAddRow" /> 
           </FooterTemplate> 
          </asp:TemplateField> 
          <asp:CommandField ShowDeleteButton="True" /> 
         </Columns> 
         <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /> 
         <RowStyle BackColor="#EFF3FB" /> 
         <EditRowStyle BackColor="#2461BF" /> 
         <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" /> 
         <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" /> 
         <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /> 
         <AlternatingRowStyle BackColor="White" /> 
        </asp:GridView> 
       </ContentTemplate> 
       <Triggers> 
        <asp:AsyncPostBackTrigger ControlID="grv_gindet" /> 
       </Triggers> 
      </asp:UpdatePanel> 

這裏是添加新行的CS代碼:

protected void AddNewRow() 
    { 

     int rowIndex = 0; 

     if (ViewState["CurrentTable"] != null) 
     { 
      DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"]; 
      DataRow drCurrentRow = null; 
      if (dtCurrentTable.Rows.Count > 0) 
      { 
       for (int i = 1; i <= dtCurrentTable.Rows.Count; i++) 
       { 
        TextBox TextBoxItem = 
         (TextBox)grv_gindet.Rows[rowIndex].Cells[1].FindControl("txt_itemno"); 
        Label TextBoxDesc = 
         (Label)grv_gindet.Rows[rowIndex].Cells[2].FindControl("txt_itemdesc"); 
        TextBox TextBoxQty = 
         (TextBox)grv_gindet.Rows[rowIndex].Cells[3].FindControl("txt_qty"); 
        Label TextBoxucost = 
         (Label)grv_gindet.Rows[rowIndex].Cells[4].FindControl("txt_ucost"); 
        Label TextBoxextncost = 
         (Label)grv_gindet.Rows[rowIndex].Cells[5].FindControl("txt_extncost"); 
        drCurrentRow = dtCurrentTable.NewRow(); 
        drCurrentRow["RowNumber"] = i + 1; 

        dtCurrentTable.Rows[i - 1]["Col1"] = TextBoxItem.Text; 
        dtCurrentTable.Rows[i - 1]["Col2"] = TextBoxDesc.Text; 
        dtCurrentTable.Rows[i - 1]["Col3"] = TextBoxQty.Text; 
        dtCurrentTable.Rows[i - 1]["Col4"] = TextBoxucost.Text; 
        dtCurrentTable.Rows[i - 1]["Col5"] = TextBoxextncost.Text; 
        rowIndex++; 
       } 
       dtCurrentTable.Rows.Add(drCurrentRow); 
       ViewState["CurrentTable"] = dtCurrentTable; 

       grv_gindet.DataSource = dtCurrentTable; 
       grv_gindet.DataBind(); 
      } 
     } 
     else 
     { 
      Response.Write("ViewState is null"); 
     } 
     SetPreviousData(); 

    } 

代碼retrive以前的數據:

private void SetPreviousData() 
    { 
     int rowIndex = 0; 
     if (ViewState["CurrentTable"] != null) 
     { 
      DataTable dt = (DataTable)ViewState["CurrentTable"]; 
      if (dt.Rows.Count > 0) 
      { 
       for (int i = 0; i < dt.Rows.Count; i++) 
       { 
        TextBox TextBoxItem = (TextBox)grv_gindet.Rows[rowIndex].Cells[1].FindControl("txt_itemno"); 
        Label TextBoxDesc = (Label)grv_gindet.Rows[rowIndex].Cells[2].FindControl("txt_itemdesc"); 
        TextBox TextBoxQty = 
         (TextBox)grv_gindet.Rows[rowIndex].Cells[3].FindControl("txt_qty"); 
        Label TextBoxucost = 
         (Label)grv_gindet.Rows[rowIndex].Cells[4].FindControl("txt_ucost"); 
        Label TextBoxextncost = 
         (Label)grv_gindet.Rows[rowIndex].Cells[5].FindControl("txt_extncost"); 

        TextBoxItem.Text = dt.Rows[i]["Col1"].ToString(); 
        TextBoxDesc.Text = dt.Rows[i]["Col2"].ToString(); 
        TextBoxQty.Text = dt.Rows[i]["Col3"].ToString(); 
        TextBoxucost.Text = dt.Rows[i]["Col4"].ToString(); 
        TextBoxextncost.Text = dt.Rows[i]["Col5"].ToString(); 
        rowIndex++; 
       } 
      } 
     } 
    } 

文本框更改事件:

protected void txt_itemno_changed(object sender, EventArgs e) 
    { 

     //TextBox thisTextBox = (TextBox)sender; 
     //GridViewRow thisGridViewRow = (GridViewRow)thisTextBox.Parent.Parent; 
     //int row = thisGridViewRow.RowIndex; 
     GridViewRow currentrow = (GridViewRow)((TextBox)sender).Parent.Parent; 
     TextBox thisTextBox = (TextBox)currentrow.FindControl("txt_itemno"); 
     int row = currentrow.RowIndex; 
     //rowChanged[row] = true; 
     using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["AWCC"].ConnectionString)) 
     { 
      con.Open(); 
      SqlCommand cmd = new SqlCommand("SELECT ITEMDET.ITEMDESC,RGITEMDET.UNITCOST FROM ITEMDET JOIN RGITEMDET ON RGITEMDET.ITEMNO=ITEMDET.ITEMNO WHERE ITEMDET.ITEMNO ='" + thisTextBox.Text + "' ", con); 
      SqlDataReader dr = cmd.ExecuteReader(); 
      while (dr.Read()) 
      { 
       grv_gindet.Rows[row].Cells[2].Text = dr["ITEMDESC"].ToString(); 
       grv_gindet.Rows[row].Cells[4].Text = dr["UNITCOST"].ToString(); 
      } 
     } 
     thisTextBox.Enabled = false; 

    } 

的gridview的子元素的回傳控制代碼:

 protected void grv_gindtrowcmd(object sender, GridViewRowEventArgs e) 
    { 
     try 
     { 

      TextBox txtitm = e.Row.FindControl("txt_itemno") as TextBox; 
      LinkButton lnkbtn = e.Row.FindControl("ShowDeleteButton") as LinkButton; 
      ScriptManager.GetCurrent(this).RegisterAsyncPostBackControl(txtitm); 
      ScriptManager.GetCurrent(this).RegisterAsyncPostBackControl(lnkbtn); 

     } 
     catch 
     { 

     } 
    } 
    protected void grv_gindetrowcmd(object sender, GridViewCommandEventArgs e) 
    { 
     try 
     { 
      Button btnad = grv_gindet.FooterRow.FindControl("ButtonAdd") as Button; 
      ScriptManager.GetCurrent(this).RegisterAsyncPostBackControl(btnad); 
     } 
     catch 
     { 

     } 
    } 

Kinldy提供解決方案儘快,做要緊。

回答

0

我希望這會幫助你,在Textchange事件改變這種

​​3210
相關問題