2011-12-14 23 views
1

GridViewアイテムテンプレートを使用して、テーブル内の一部のレコードを更新しようとしています。それはこのようなものだ:グリッドビューの編集中に新しい値を取得する方法は?

<asp:GridView ID="grdCursos" runat="server" CssClass="grdTable" 
       AutoGenerateColumns="False" DataKeyNames="CourseID"> 
       <Columns> 
        <asp:TemplateField HeaderText="ID" Visible="false"> 
         <ItemTemplate> 
          <asp:Label ID="lblID" runat="server" Text='<%#Bind("[CourseID]") %>'></asp:Label> 
         </ItemTemplate> 
        </asp:TemplateField> 
        <asp:TemplateField HeaderText="Nome"> 
         <EditItemTemplate> 
          <asp:TextBox ID="txtCourseName" runat="server" Text='<%#Bind("[CourseName]") %>' Width="50"></asp:TextBox> 
         </EditItemTemplate> 
         <ItemTemplate> 
          <asp:Label ID="lblCourseName" runat="server" Text='<%#Bind("[CourseName]") %>'></asp:Label> 
         </ItemTemplate> 
        </asp:TemplateField> 
        <asp:TemplateField HeaderText="# de Questões"> 
         <EditItemTemplate> 
          <asp:TextBox ID="txtMaxQuestions" runat="server" Text='<%#Bind("[CourseMaxQuestions]") %>' Width="20"></asp:TextBox> 
         </EditItemTemplate> 
         <ItemTemplate> 
          <asp:Label ID="lblMaxQuestions" runat="server" Text='<%#Bind("[CourseMaxQuestions]") %>'></asp:Label> 
         </ItemTemplate> 
        </asp:TemplateField> 
        <asp:TemplateField HeaderText="Data de atribuição"> 
         <ItemTemplate> 
          <asp:Label ID="lblCourseSince" runat="server" 
           Text='<%#Bind("[CourseSince]","{0:d}") %>'></asp:Label> 
         </ItemTemplate> 
        </asp:TemplateField> 
        <asp:TemplateField HeaderText="Nome do arquivo"> 
         <EditItemTemplate> 
          <asp:FileUpload ID="fileCourse" runat="server"/> 
         </EditItemTemplate> 
         <ItemTemplate> 
          <asp:Label ID="lblCourse" runat="server" Text='<%#Bind("[CourseFileName]") %>'></asp:Label> 
         </ItemTemplate> 
         <ItemStyle Width="200px" /> 
        </asp:TemplateField> 
        <asp:CommandField ButtonType="Image" EditImageUrl="~/site/edit.jpg" 
         ShowEditButton="True" DeleteImageUrl="~/site/cancel.jpg" 
         UpdateImageUrl="~/site/accept.jpg" CancelImageUrl="~/site/cancel.jpg" 
         ShowDeleteButton="True" CancelText="Cancelar" DeleteText="Inativar" 
         EditText="Editar" UpdateText="Atualizar"></asp:CommandField> 
        <asp:CommandField ShowEditButton="True" /> 
       </Columns> 

とすぐに私は私のGriViewの列のいくつかは、私が編集できるテキストボックスとなっ編集リンク、をクリックして。次に、更新ボタンをクリックすると、grdCursos_RowUpdatingメソッドが呼び出されます。問題は、ユーザーが入力したの新しい値を取得できないことです。私はMsgBoxと呼んで、更新したい値を表示し、古い値だけを表示します。

これは私の方法である:

Protected Sub grdCursos_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles grdCursos.RowUpdating 
    Dim updatingRow As GridViewRow = grdCursos.Rows(e.RowIndex) 'Recupera a linha que está sendo editada 

    'Recupera os dados cadastrados 
    Dim ID As Label = updatingRow.FindControl("lblID") 
    Dim txtCourseName As TextBox = updatingRow.FindControl("txtCourseName") 
    Dim txtMaxQuestions As TextBox = updatingRow.FindControl("txtMaxQuestions") 
    Dim CourseFile As FileUpload = updatingRow.FindControl("fileCourse") 

    MsgBox(String.Format("ID={0}, Nome do curso={1}, # de questoes={2}, Nome do arquivo={3}", ID.Text, txtCourseName.Text, txtMaxQuestions.Text, CourseFile.FileName)) 
    Exit Sub 
    .... 

はどうやって新しい値を得ることができますか?

+1

Sub CustomersGridView_RowUpdating(ByVal sender As Object, ByVal e As GridViewUpdateEventArgs) ' Use the CopyTo method to copy the DictionaryEntry objects in the ' NewValues collection to an array. Dim records(e.NewValues.Count - 1) As DictionaryEntry e.NewValues.CopyTo(records, 0) ' Iterate through the array and HTML encode all user-provided values ' before updating the data source. Dim entry As DictionaryEntry For Each entry In records e.NewValues(entry.Key) = Server.HtmlEncode(entry.Value.ToString()) Next End Sub 
[e.NewValues(http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridviewupdateeventargs.newvalues.aspx) –

+0

K.私はまだ.NETに慣れていません。私は最初の行にe.RowIndexの代わりにこれを変更しますか?どうも –

答えて

1

短い答え:MSDNからe.NewValues

を更新する行の非キーフィールド 名前/値ペアの修正値を含む辞書を取得します。

更新する行の 改訂された非キーフィールドの値にアクセスするには、NewValuesプロパティ(ディクショナリ)を使用します。

NewValuesプロパティには、行内の改訂された非キーフィールドの名前/値 のペアが自動的に設定されます。別のエントリは、行内の各非キーフィールドのNewValuesプロパティに追加された です。

NewValues辞書に含まSystem.Collections.DictionaryEntryオブジェクトのDictionaryEntry.Key プロパティを使用し、エントリのフィールド名を決定します。エントリの値を確認するには、 のDictionaryEntry.Valueプロパティを使用します。

プライマリキーフィールドまたはフィールドは、この辞書に含まれていません。 主キーフィールドの値にアクセスするには、キー プロパティを使用します。 行の非キーフィールドの元の値にアクセスするには、OldValuesプロパティを使用します。

例:

関連する問題