0

我正在尝试在 gridview 的 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>

后面的代码:

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?

提前致谢。;)

4

1 回答 1

1

默认情况下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
}
于 2015-06-17T12:00:00.133 回答