2012-07-27 82 views
0

當用戶在編輯模板中更改文本框中的文本並單擊更新時,當我嘗試抓取這些新值時,它仍然在爲文本框的舊值加分。從代碼背後更新GridView行

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
     DataKeyNames="CompanyID" CellPadding="4" 
     GridLines="None" Width="1079px" ForeColor="#333333" 
     OnRowCancelingEdit="GridView1_RowCancelling" 
     OnRowUpdating="GridView1_RowUpdating" 
     OnRowEditing="GridView1_RowEditing"> 


     <AlternatingRowStyle BackColor="White" ForeColor="#284775" /> 
     <Columns> 
      <asp:TemplateField ShowHeader="False"> 

       <EditItemTemplate> 
        <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" 
         CommandName="Update" CommandArgument='<%# Eval("CompanyID") %>' Text="Update"></asp:LinkButton> 
        &nbsp;<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" 
         CommandName="Cancel" Text="Cancel"></asp:LinkButton> 
       </EditItemTemplate> 
       <ItemTemplate> 
        <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" 
         CommandName="Edit" Text="Edit" ></asp:LinkButton> 
       </ItemTemplate> 


      </asp:TemplateField> 



      <asp:TemplateField HeaderText="Issue Date"> 

       <ItemTemplate> 
        <asp:Label runat="server" ID="IssueDate" Text='<%#Eval("IssueDate") %>' />      
       </ItemTemplate> 

       <EditItemTemplate> 
        <asp:TextBox runat="server" ID="txtIssueDate" Text='<%#Eval("IssueDate") %>' /> 
       </EditItemTemplate> 

      </asp:TemplateField> 


      <asp:TemplateField HeaderText="Notice Intent Response Due"> 

       <ItemTemplate> 
        <asp:Label runat="server" ID="NoticeIntentResponseDue" Text='<%#Eval("NoticeIntentResponseDue") %>' />      
       </ItemTemplate> 

       <EditItemTemplate> 
        <asp:TextBox runat="server" ID="txtNoticeIntentResponseDue" Text='<%#Eval("NoticeIntentResponseDue") %>' /> 
       </EditItemTemplate> 

      </asp:TemplateField> 


      <asp:TemplateField HeaderText="Deadline For Questions"> 

       <ItemTemplate> 
        <asp:Label runat="server" ID="DeadlineForQuestions" Text='<%#Eval("DeadlineForQuestions") %>' />      
       </ItemTemplate> 

       <EditItemTemplate> 
        <asp:TextBox runat="server" ID="txtDeadlineForQuestions" Text='<%#Eval("DeadlineForQuestions") %>' /> 
       </EditItemTemplate> 

      </asp:TemplateField> 


      <asp:TemplateField HeaderText="Bids Due"> 

       <ItemTemplate> 
        <asp:Label runat="server" ID="BidsDue" Text='<%#Eval("BidsDue") %>' />      
       </ItemTemplate> 

       <EditItemTemplate> 
        <asp:TextBox runat="server" ID="txtBidsDue" Text='<%#Eval("BidsDue") %>' /> 
       </EditItemTemplate> 

      </asp:TemplateField> 


      <asp:TemplateField HeaderText="Shortlist Notice"> 

       <ItemTemplate> 
        <asp:Label runat="server" ID="ShortlistNotice" Text='<%#Eval("ShortlistNotice") %>' />      
       </ItemTemplate> 

       <EditItemTemplate> 
        <asp:TextBox runat="server" ID="txtShortlistNotice" Text='<%#Eval("ShortlistNotice") %>' /> 
       </EditItemTemplate> 

      </asp:TemplateField> 


      <asp:TemplateField HeaderText="Final Selection"> 

       <ItemTemplate> 
        <asp:Label runat="server" ID="FinalSelection" Text='<%#Eval("FinalSelection") %>' />      
       </ItemTemplate> 

       <EditItemTemplate> 
        <asp:TextBox runat="server" ID="txtFinalSelection" Text='<%#Eval("FinalSelection") %>' /> 
       </EditItemTemplate> 

      </asp:TemplateField> 





      <asp:TemplateField Visible="false" HeaderText="CompanyID"> 

       <ItemTemplate> 
        <asp:Label runat="server" ID="CompanyID" Text='<%#Eval("CompanyID") %>' /> 
       </ItemTemplate> 

      </asp:TemplateField> 




     </Columns> 



    </asp:GridView> 

更新按鈕調用此函數:

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) 

    { 

     int key = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value); 

     Label CompanyID = (Label)GridView1.Rows[e.RowIndex].FindControl("txtCompanyID"); 

     TextBox thisIssueDate = (TextBox)(GridView1.Rows[e.RowIndex].FindControl("txtIssueDate")); 

     TextBox NoticeIntentResponseDue = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtNoticeIntentResponseDue"); 

     Response.Write(NoticeIntentResponseDue.Text + " " + thisIssueDate.Text); 
     Response.End(); 

     TextBox DeadlineForQuestions = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtDeadlineForQuestions"); 

     TextBox BidsDue = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtBidsDue"); 

     TextBox ShortlistNotice = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtShortlistNotice"); 

     TextBox FinalSelection = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtFinalSelection"); 
    } 

的響應,顯示我被抓住的價值仍然是盒子的origonal文本值。不是你在盒子裏輸入的內容。

+1

你在哪裏綁定GridView?您可能希望確保在「if(!IsPostBack){}」內部執行此操作,因爲編輯命令可能會觸發回傳,從而將網格重新綁定爲其原始值。 – 2012-07-27 19:48:12

+0

該問題看起來像您還將編輯項目模板列與數據表中的數據綁定在一起,並且當您在代碼中獲取代碼中的數據時沒有獲得用戶在編輯模式下更新的更新數據,並且仍然獲取舊數據。 – 2012-07-27 19:55:52

+0

運行您的代碼而不綁定編輯項目模板字段並檢查您的代碼是否有效。 – 2012-07-27 19:59:56

回答

0

我想通了,德里克說得對。它必須處理頁面加載中的回發中的綁定數據。我每次都會綁定數據,而不是第一次。謝謝

0

在網格視圖行更新事件中添加以下條件

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) 
{ 
if (e.Row.RowState == DataControlRowState.Edit) 
{ 


    int key = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value); 

    Label CompanyID = (Label)GridView1.Rows[e.RowIndex].FindControl("txtCompanyID"); 

    TextBox thisIssueDate = (TextBox)(GridView1.Rows[e.RowIndex].FindControl("txtIssueDate")); 

    TextBox NoticeIntentResponseDue = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtNoticeIntentResponseDue"); 

    Response.Write(NoticeIntentResponseDue.Text + " " + thisIssueDate.Text); 
    Response.End(); 

    TextBox DeadlineForQuestions = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtDeadlineForQuestions"); 

    TextBox BidsDue = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtBidsDue"); 

    TextBox ShortlistNotice = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtShortlistNotice"); 

    TextBox FinalSelection = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtFinalSelection"); 
    } 
    } 

更新: 問題看起來你也從數據表中的數據綁定你的編輯項目模板列,而當你獲取代碼背後的數據沒有得到用戶在編輯模式下更新的更新數據,並且您仍然獲取舊數據。 If you remove the Binding from the Edit Item Template feilds then your code will work.

+0

仍然做同樣的事情,即使當我沒有綁定到編輯模板 – mwhite14 2012-07-27 20:09:58

+0

你是否添加了這種編碼if(e.RowState == DataControlRowState。編輯)'? – 2012-07-27 20:10:40

+0

idk爲什麼但它不讓我打電話給e.Row。我正在使用以下系統 - 使用系統; using System.Collections.Generic;使用System.Linq的 ; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data.SqlClient; using System.Data; using System.Data.Common; using System.Text.RegularExpressions; – mwhite14 2012-07-27 20:14:02