如何在 GridView 的 OnRowCommand 中使用 FindControl



我正在尝试在网格视图的RowCommand中使用findcontrol TextBox。但错误对象引用未设置为对象的实例。

请帮帮我。

设计

    <asp:GridView ID="gvMaster" runat="server" AllowPaging="true" AutoGenerateColumns="False"  OnRowCommand="gvMaster_RowCommand" Width="100%">  
        <Columns>   
<asp:TemplateField HeaderText="">
     <ItemTemplate>
      <asp:ImageButton ID="ibtnEdit" runat="server" CommandName="edit" ImageUrl="~/images/edit.gif" ToolTip="Insert/Edit" />
       </ItemTemplate>
         <EditItemTemplate>
           <table>
             <tr>
               <td nowrap="nowrap">
                  <asp:ImageButton ID="ibtnSave" runat="server" CommandName="update" ImageUrl="~/images/icon-floppy.gif" ToolTip="Save" />
               </td>
               <td nowrap="nowrap">
                  <asp:ImageButton ID="ibtnCancel" runat="server" CommandName="cancel" ImageUrl="~/images/icon-cancel.gif" ToolTip="Cancel" />
               </td>
             </tr>
           </table>
          </EditItemTemplate>
          <ItemStyle HorizontalAlign="Center" Width="30px" />
           <HeaderStyle HorizontalAlign="Center" />
          </asp:TemplateField>                                       
                <asp:TemplateField HeaderText="Effective Date">
                        <ItemTemplate>
                                 <asp:Label ID="lblEffectiveDate" runat="server" Text='<%# String.Format("{0:dd/MM/yyyy}", Eval("eff_date")) %>'></asp:Label>
                         </ItemTemplate>
                         <EditItemTemplate>
                             <table>
                                <tr>
                                    <td nowrap="nowrap">                                                                    
                                        <asp:TextBox ID="txtEffDate2" runat="server" MaxLength="10" Text='<%# String.Format("{0:dd/MM/yyyy}", Eval("eff_date")) %>'
                                          Width="70px" Style="text-align: center"></asp:TextBox>
                                    </td>
                                </tr>
                            </table>
                         </EditItemTemplate>
                </asp:TemplateField>
        </Columns>
        </asp:GridView>

Code BeHind:

protected void gvMaster_RowCommand(object sender, GridViewCommandEventArgs e)
    {
            if (e.CommandName.ToUpper().Equals("SELECT"))
                  { 
                  }
           else if (e.CommandName.ToUpper().Equals("EDIT"))
                {
                    string cmdNmEdit = e.CommandName;
                     object cmdSrcEdit = e.CommandSource;
                     GridViewRow gvMaster = (GridViewRow)(((Control)e.CommandSource).NamingContainer);
                     TextBox txtEffDate2 = gvMaster.FindControl("MyTextBoxId") as TextBox;
                     txtEffDate2.Text = DateTime.Now.ToString("DD/MM/yyyy");     //<------ Error This Line
                }
         }

如何在 GridView 的 OnRowCommand 中使用 FindControl?

提前谢谢。 ;)

默认情况下,CommandArgument包含行索引。这就是为什么这有效:

protected void gvMaster_RowCommand(object sender, GridViewCommandEventArgs e)
{
    int rowIndex = Convert.ToInt32(e.CommandArgument);
    GridView grid = (GridView) sender;
    GridViewRow row = grid.Rows[rowIndex];
    // now you can use row.FindControl
}

最新更新