setting hyperlink properties from behind code

lounge56

Newcomer
Joined
Feb 5, 2004
Messages
14
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!
 
You have to go thru each Datagriditem and find the control, you can do this either by doing a For each...Next or in your itemdatabound event of your datagrid.
 
Thank you kahlua001! Controls(integer) and Items(integer) require specific index number. I am not quite sure how to get a Count of the controls within the data grid. When I try to get a count, I get only 1:

griProfile.Controls.Count


Any direction would be helpful, or would you happen to have an example around that you could post?
 
Back
Top