2017-10-20 4 views
1

私は奇妙な状況に陥った。 これは私のGridViewののASCXコードGridView onRowUpdatingイベントのテキストボックスへのアクセス

<asp:gridview ID="grdBooks" class="grdBooks" runat="server" width="100%" DataKeyNames="RecId,RefAuthorsRecId,RefBooksTypeRecId" autogeneratecolumns="false" onDataBound="grdBooks_dataBound" onPreRender="grdBooks_preRender" onrowediting="grdBooks_RowEdit" onrowupdating="grbBooks_onRowUpdating" onrowupdated="grdBooks_onUpdated" autogenerateeditbutton="true" onrowcancelingedit="grdBooks_onCancelingEdit"> 
      <Columns> 
       <asp:TemplateField HeaderText="Title"> 
        <EditItemTemplate> 
         <asp:TextBox ID="txtgrdTitle" runat="server" width="200px"></asp:TextBox> 
         <asp:Label ID="lblgrdTitle2" runat="server" visible="false" Text='<%# Eval("Title") %>'></asp:Label> 
        </EditItemTemplate> 
        <ItemTemplate> 
         <asp:Label ID="lblgrdTitle" runat="server" Text='<%# Bind("Title") %>'></asp:Label> 
        </ItemTemplate> 
       </asp:TemplateField>..... 

の一部である。これは、 "onRowEdit" と "onRowUpdating"

protected void grdBooks_RowEdit(object sender, GridViewEditEventArgs e) 
{ 
    grdBooks.EditIndex = e.NewEditIndex; 
    grdBooks.DataSource = book; 
    grdBooks.DataBind(); 
    TextBox txtTitle = grdBooks.Rows[e.NewEditIndex].FindControl("txtgrdTitle") as TextBox; 
    Label lblTitle = grdBooks.Rows[e.NewEditIndex].FindControl("lblgrdTitle") as Label; 
    txtTitle.Text = lblTitle.Text; 
} 
protected void grbBooks_onRowUpdating(object sender, GridViewUpdateEventArgs e) 
{ 
    TextBox txtTitle = grdBooks.Rows[e.RowIndex].FindControl("txtgrdTitle") as TextBox; 
    .... 
} 

の一部である問題がある:

  • rowEditはうまく動作し、編集可能なテキストボックスでグリッドのすべての行を変更します。問題は、テキストボックスに何かを書いて、「更新」を押して「onRowUpdate」メソッドに入るときです。私は初期化したテキストボックスオブジェクトに新しいテキスト(挿入したもの)を持っていません。私は古いものを持っています。

(私はeditTemplateで元のタイトルの値を持っている目に見えないラベルを使用していますが、テキストボックスのテキストにEval( "title")を付ける前に、これはproblだから私はこれを別の方法で試してみてください)

+0

'e.NewValues'ディクショナリを使用してください。 –

答えて

0

あなたのデータソースはbookです。データテーブルの現在の行を取得し、その行の値を更新してください。

GridViewRow gridEditRow = this.grdBooks.Rows[e.RowIndex]; 
DataRow CurentRow = book.Rows[gridEditRow.DataItemIndex]; 

CurentRow["Title"] = (gridEditRow.FindControl("txtgrdTitle") as TextBox).Text; 
+0

問題はdtに入力されません。私はテキストボックスに書き込んだテキストを再検索することができないのですか? .Textの値は常に ""(空)です。これは私の例であり、あなたのコードでもあるので、findControlを正確な行に入れて –

関連する問題