kdeutsch
asked on
Item bata bound event causing an error
I am trying to create a databound event for my hyperlink when it is blank from the database.
but when I impliment it it causes errors on the datagrid, but when I put same code in db it works, it also works when I comment out the ItemDatabound event, but what happens is some items are not links on the datagrid but just blank.
but when I impliment it it causes errors on the datagrid, but when I put same code in db it works, it also works when I comment out the ItemDatabound event, but what happens is some items are not links on the datagrid but just blank.
Private Sub myDataGrid_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles myDataGrid.ItemDataBound
Dim Fixpage As String = e.Item.Cells(4).Text
Dim hl As HyperLink = e.Item.Cells(4).FindControl("hlFix")
If Fixpage = " " Or Fixpage = "" Then
hl.NavigateUrl = "http://sharepoint/JFHQ/JSTAFF/J1/J1ARMY/Mob/Mob%20Tracker%20Fix%20Pages/Fix%20Page%20Library.aspx"
End If
End Sub
<asp:TemplateColumn HeaderText="Fix Page">
<ItemTemplate>
<asp:Hyperlink id="hlFix" runat="server" Text="Fix Page" NavigateUrl='<%# DataBinder.Eval (Container.DataItem,"strFilterPage") %>' Target="_blank"></asp:Hyperlink>
</ItemTemplate>
</asp:TemplateColumn>
ASKER
Sorry this is the error,
When I comment out these lines it works good,
If Fixpage = " " Or Fixpage = "" Then
hl.NavigateUrl = "http://sharepoint/JFHQ/JSTAFF/J1/J1ARMY/Mob/Mob%20Tracker%20Fix%20Pages/Fix%20Page%20Library.aspx"
End If
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceExcept ion: Object reference not set to an instance of an object.
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
Stack Trace:
[NullReferenceException: Object reference not set to an instance of an object.]
MobTrackerDotNet.StandardR eports.myD ataGrid_It emDataBoun d(Object sender, DataGridItemEventArgs e) in R:\Inetpub\wwwroot\MobTrac kerDotNet\ Reports\St andardRepo rts.aspx.v b:568
System.Web.UI.WebControls. DataGrid.O nItemDataB ound(DataG ridItemEve ntArgs e) +110
System.Web.UI.WebControls. DataGrid.C reateItem( Int32 itemIndex, Int32 dataSourceIndex, ListItemType itemType, Boolean dataBind, Object dataItem, DataGridColumn[] columns, TableRowCollection rows, PagedDataSource pagedDataSource) +181
System.Web.UI.WebControls. DataGrid.C reateContr olHierarch y(Boolean useDataSource) +1485
System.Web.UI.WebControls. BaseDataLi st.OnDataB inding(Eve ntArgs e) +49
System.Web.UI.WebControls. BaseDataLi st.DataBin d() +23
MobTrackerDotNet.StandardR eports.Fil lDataGrid( ) in R:\Inetpub\wwwroot\MobTrac kerDotNet\ Reports\St andardRepo rts.aspx.v b:165
MobTrackerDotNet.StandardR eports.ddl Deployment _SelectedI ndexChange d(Object sender, EventArgs e) in R:\Inetpub\wwwroot\MobTrac kerDotNet\ Reports\St andardRepo rts.aspx.v b:93
System.Web.UI.WebControls. ListContro l.OnSelect edIndexCha nged(Event Args e) +108
System.Web.UI.WebControls. DropDownLi st.System. Web.UI.IPo stBackData Handler.Ra isePostDat aChangedEv ent() +26
System.Web.UI.Page.RaiseCh angedEvent s() +115
System.Web.UI.Page.Process RequestMai n() +1099
When I comment out these lines it works good,
If Fixpage = " " Or Fixpage = "" Then
hl.NavigateUrl = "http://sharepoint/JFHQ/JSTAFF/J1/J1ARMY/Mob/Mob%20Tracker%20Fix%20Pages/Fix%20Page%20Library.aspx"
End If
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceExcept
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
Stack Trace:
[NullReferenceException: Object reference not set to an instance of an object.]
MobTrackerDotNet.StandardR
System.Web.UI.WebControls.
System.Web.UI.WebControls.
System.Web.UI.WebControls.
System.Web.UI.WebControls.
System.Web.UI.WebControls.
MobTrackerDotNet.StandardR
MobTrackerDotNet.StandardR
System.Web.UI.WebControls.
System.Web.UI.WebControls.
System.Web.UI.Page.RaiseCh
System.Web.UI.Page.Process
ok add a check before that...
if hl IsNot nothing then
if hl IsNot nothing then
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi,
HL isNot Nothing gives me Blue errors, still working in 1.1.
Tried this but gives me errors as well
If hl = "" Then
If Fixpage = " " Or Fixpage = "" Then
hl.NavigateUrl = "http://sharepoint/JFHQ/JSTAFF/J1/J1ARMY/Mob/Mob%20Tracker%20Fix%20Pages/Fix%20Page%20Library.aspx"
End If
End If
HL isNot Nothing gives me Blue errors, still working in 1.1.
Tried this but gives me errors as well
If hl = "" Then
If Fixpage = " " Or Fixpage = "" Then
hl.NavigateUrl = "http://sharepoint/JFHQ/JSTAFF/J1/J1ARMY/Mob/Mob%20Tracker%20Fix%20Pages/Fix%20Page%20Library.aspx"
End If
End If
ASKER
Oh,
the erro for that is Operator is not defined for types system.web.uic.webcontrols .hyperlink & string
the erro for that is Operator is not defined for types system.web.uic.webcontrols
ASKER
All,
the realization just hit me that checking agianst the text in the cell will never work because it will never be blank, it will always have Fix page in it, but what is blank is the data behind the link. How do I access this data????
the realization just hit me that checking agianst the text in the cell will never work because it will never be blank, it will always have Fix page in it, but what is blank is the data behind the link. How do I access this data????
In your ItemDataBound event handler, you will first need to check if it is a Header row or a Item row, because the event handler loops through all rows in the DataGrid, starting from the Header row.
ASKER
Ok, I am really getting confused witht this now, it does not give the error above when I put in the alternating item it seemed to disapppear, but some elements in cell 4 of the datagrid do not appear with a blue link becuase they have no database element attached to them. I put in the check for header footer but I don't see how thats solving the problem of checking for data behind the link.
If (e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingIt em) Then
Dim Fixpage As String = e.Item.Cells(4).Text
Dim hl As HyperLink = e.Item.Cells(4).FindContro l("hlFix")
If (e.Item.ItemType = ListItemType.Header Or e.Item.ItemType = ListItemType.Footer) Then
If e.Item.Cells(4).Text = " " Then
hl.NavigateUrl = "http://sharepoint/JFHQ/JSTAFF/J1/J1ARMY/Mob/Mob%20Tracker%20Fix%20Pages/Fix%20Page%20Library.aspx"
End If
End If
End If
If (e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingIt
Dim Fixpage As String = e.Item.Cells(4).Text
Dim hl As HyperLink = e.Item.Cells(4).FindContro
If (e.Item.ItemType = ListItemType.Header Or e.Item.ItemType = ListItemType.Footer) Then
If e.Item.Cells(4).Text = " " Then
hl.NavigateUrl = "http://sharepoint/JFHQ/JSTAFF/J1/J1ARMY/Mob/Mob%20Tracker%20Fix%20Pages/Fix%20Page%20Library.aspx"
End If
End If
End If
Are you trying to change the url only for header and footer? If not, then remove the checking for Header and Footer.
If (e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem) Then
Dim Fixpage As String = e.Item.Cells(4).Text
Dim hl As HyperLink = e.Item.Cells(4).FindControl("hlFix")
If (Not hl Is Nothing) Then
If e.Item.Cells(4).Text = " " Then
hl.NavigateUrl = "http://sharepoint/JFHQ/JSTAFF/J1/J1ARMY/Mob/Mob%20Tracker%20Fix%20Pages/Fix%20Page%20Library.aspx"
End If
End If
End If
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Got it to work by going directly to the datarow. Thanks for everyones help. I had to spread points on this because I would be remiss if I did not acknowledge help from others.
May be because NavigateUrl='<%# DataBinder.Eval (Container.DataItem,"strFi
Can you try:
.aspx
NavigateUrl='<%# GetUrl(Eval("strFilterPage
.aspx.vb
Public Function GetUrl(ByVal strFilterPage As Object) As String
Dim url As String = DirectCast(strFilterPage, String)
If strFilterPage Is Nothing or strFilterPage = String.EmptyThen
url = "http://sharepoint/JFHQ/JSTAFF/J1/J1ARMY/Mob/Mob%20Tracker%20Fix%20Pages/Fix%20Page%20Library.aspx"
End If
Return url
End Function
Note: There could be some other way....