Gridview_RowDataBound中的错误 [英] Error in Gridview_RowDataBound

查看:98
本文介绍了Gridview_RowDataBound中的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

This is my gridview row bound code

Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound

If e.Row.RowType = DataControlRowType.DataRow Then
Dim lcl_date As String = "d-MMM-yyyy"
If Session("lcl_date") IsNot Nothing Then
lcl_date = Session("lcl_date")
End If
Dim podate As DateTime = DataBinder.Eval(e.Row.DataItem, "podate")
Dim lbl5 As Label = CType(e.Row.FindControl("Label5"), Label)
Dim txpodate As TextBox = CType(e.Row.FindControl("txt_podate"), TextBox)
lbl5.Text = podate.ToString(lcl_date)
txpodate.text = podate.ToString(lcl_date)
End If

End Sub




It was working fine if i do not add

Dim txtpodate as textbox and txtpodate.text

and corresponding asp.net code is as

<asp:TemplateField HeaderText="PO Date">

                               <ItemTemplate>
                                   <asp:Label ID="Label5" runat="server" Text='<%# Bind("podate") %>'></asp:Label>
                               </ItemTemplate>
                               <EditItemTemplate>
                                   <asp:TextBox ID="txt_podate" runat="server" Text='<%# Bind("podate") %>'></asp:TextBox>
                               </EditItemTemplate>
                           </asp:TemplateField>
but after adding this line in the gridview row databound, it displaying error as Object reference not set to instance of the object

推荐答案

错误即将发生,因为您从获取控件EditItemTemplate

您可以从EditItemTemplate中找到Control,如下所示。



Error is coming because you are fetching control from EditItemTemplate.
You can find Control from EditItemTemplate as below.

If ((e.Row.RowState & DataControlRowState.Edit)) Then  
  Dim txpodate As TextBox = CType(e.Row.FindControl("txt_podate"), TextBox)
  Response.Write(TextBox1.Text)
End If  


这篇关于Gridview_RowDataBound中的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆