Hello! I have a User Control (.ascx) in VB.NET. The .ascx file has a data grid with a couple of hyperlinks in the the TemplateColumn (they are hyperlinks with different types of functionality for my application):
'******************** Begin .ascx file code *******************
<asp:datagrid id="griProfile" BorderWidth="1" Width="900px" cellpadding="2" cellspacing="0" runat="server" AutoGenerateColumns="False" AlternatingItemStyle-BackColor="#cccccc" ItemStyle-VerticalAlign="Top" HeaderStyle-HorizontalAlign="Center">
<Columns>
<asp:BoundColumn DataField="SurveyID" HeaderText="#"></asp:BoundColumn>
<asp:BoundColumn DataField="SurveyName" HeaderText="Name"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="Controls">
<ItemTemplate>
<asp:hyperlink id="hypProfEdit" ImageUrl="../../images/edit.gif" runat="server"></asp:hyperlink>
<asp:hyperlink id="hypProfPublish" ImageUrl="../../images/publish.gif" runat="server"></asp:hyperlink>
<asp:hyperlink id="hypProfReports" ImageUrl="../../images/reports.gif" runat="server" ></asp:hyperlink>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:datagrid>
'******************** End .ascx file code *******************
In the behind code, I try to set the "NavigateUrl" property for each hyperlink. When I run the application, I get the following error right when the Debugger reads the first line where I try to set the "NavigateURL" property to on of the hyperlinks:
"System.NullReferenceException: Object reference not set to an instance of an object."
'******************** Begin behind code sample **************
The behind code sample is the following:
'data grid
Protected WithEvents griProfile As System.Web.UI.WebControls.DataGrid
'hyperlinks
Protected WithEvents hypProfEdit As Esperantus.WebControls.HyperLink
Protected WithEvents hypProfPublish As Esperantus.WebControls.HyperLink
Protected WithEvents hypProfReports As Esperantus.WebControls.HyperLink
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim objSurveyDB As New SurveyDB()
Dim ds As DataSet
Dim inUserName As String
Dim inUserCode As Integer
Dim inCodArea As Integer
Dim inCodCargo As Integer
Dim inProfileCode As Integer
'these are just values for stored procedure which returns info for
'data grid...please disregard
inUserCode = 156
inCodCargo = 34
inCodArea = 42
inProfileCode = 156
'data grid
ds = ConvertDataReaderToDataSet(objSurveyDB.GetSurveys(1, inUserCode, inCodCargo, inCodArea, inProfileCode))
griProfile.DataSource() = ds
'NOTE: NEXT LINE IS WHERE I GET MENTIONED ERROR
'NOTE2: I made hyperlinks static for this sample to facilitate demonstration
'of code. They are dynamic in reality, but that does not make a difference
'here because I still get error with static hyperlinks displayed.
hypProfEdit.NavigateUrl = "~/EDefault.aspx?tid=1"
hypProfPublish.NavigateUrl = "~/EDefault.aspx?tid=2"
hypProfReports.NavigateUrl = "~/EDefault.aspx?tid=3"
End Sub
'******************** End behind code sample ****************
I have tried:
1. creating an instance of the same class and deriving the properties from this new instance. I get the same error I mentioned above.
2. I have tried deriving the hyperlink properties from Me (i.e: Me.Property = "..."). I get the same error I mentioned above.
I tried to present the source code in a very organized manner. Any help would be greatly appreciated.
Thank you!
'******************** Begin .ascx file code *******************
<asp:datagrid id="griProfile" BorderWidth="1" Width="900px" cellpadding="2" cellspacing="0" runat="server" AutoGenerateColumns="False" AlternatingItemStyle-BackColor="#cccccc" ItemStyle-VerticalAlign="Top" HeaderStyle-HorizontalAlign="Center">
<Columns>
<asp:BoundColumn DataField="SurveyID" HeaderText="#"></asp:BoundColumn>
<asp:BoundColumn DataField="SurveyName" HeaderText="Name"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="Controls">
<ItemTemplate>
<asp:hyperlink id="hypProfEdit" ImageUrl="../../images/edit.gif" runat="server"></asp:hyperlink>
<asp:hyperlink id="hypProfPublish" ImageUrl="../../images/publish.gif" runat="server"></asp:hyperlink>
<asp:hyperlink id="hypProfReports" ImageUrl="../../images/reports.gif" runat="server" ></asp:hyperlink>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:datagrid>
'******************** End .ascx file code *******************
In the behind code, I try to set the "NavigateUrl" property for each hyperlink. When I run the application, I get the following error right when the Debugger reads the first line where I try to set the "NavigateURL" property to on of the hyperlinks:
"System.NullReferenceException: Object reference not set to an instance of an object."
'******************** Begin behind code sample **************
The behind code sample is the following:
'data grid
Protected WithEvents griProfile As System.Web.UI.WebControls.DataGrid
'hyperlinks
Protected WithEvents hypProfEdit As Esperantus.WebControls.HyperLink
Protected WithEvents hypProfPublish As Esperantus.WebControls.HyperLink
Protected WithEvents hypProfReports As Esperantus.WebControls.HyperLink
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim objSurveyDB As New SurveyDB()
Dim ds As DataSet
Dim inUserName As String
Dim inUserCode As Integer
Dim inCodArea As Integer
Dim inCodCargo As Integer
Dim inProfileCode As Integer
'these are just values for stored procedure which returns info for
'data grid...please disregard
inUserCode = 156
inCodCargo = 34
inCodArea = 42
inProfileCode = 156
'data grid
ds = ConvertDataReaderToDataSet(objSurveyDB.GetSurveys(1, inUserCode, inCodCargo, inCodArea, inProfileCode))
griProfile.DataSource() = ds
'NOTE: NEXT LINE IS WHERE I GET MENTIONED ERROR
'NOTE2: I made hyperlinks static for this sample to facilitate demonstration
'of code. They are dynamic in reality, but that does not make a difference
'here because I still get error with static hyperlinks displayed.
hypProfEdit.NavigateUrl = "~/EDefault.aspx?tid=1"
hypProfPublish.NavigateUrl = "~/EDefault.aspx?tid=2"
hypProfReports.NavigateUrl = "~/EDefault.aspx?tid=3"
End Sub
'******************** End behind code sample ****************
I have tried:
1. creating an instance of the same class and deriving the properties from this new instance. I get the same error I mentioned above.
2. I have tried deriving the hyperlink properties from Me (i.e: Me.Property = "..."). I get the same error I mentioned above.
I tried to present the source code in a very organized manner. Any help would be greatly appreciated.
Thank you!