By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,707 Members | 1,651 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,707 IT Pros & Developers. It's quick & easy.

Problem of Cast/Datagrid - Code Behind Bis (felix)

P: n/a
Hello,

First, thanks to felix for his answer.

But :-( , I'm feeling newbie :! I' don't understand all the situation.
The trouble :

Always in the road whith my Datagrid and my ItemTemplate with a Control
HyperLink.
I would like to fix many properties of this Control Hyperlink in the Code
Behind. I'm using the Datagrid OnItemCreated Event.

So, I can establish a lot of properties of the Control like that :

--- In the file .aspx, in the Datagrid Control :

OnItemCreated="myDatagrid_ItemCreated"
---- In the code behind file (.aspx .vb)
Sub myDatagrid_ItemCreated(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.DataGridItemEventArgs)

If e.Item.ItemType = ListItemType.Item Then

CType(e.Item.FindControl("lnkControl"),
HyperLink).Text="myText"

End If

-->> It's OK.

But I have always the problem with the property NavigateUrl of thisControl
Hyperlink (or CommandArgument property for an another control like
ImageButton).

Question : How can I access to the value of a cell in the DataGrid in the
OnItemCreated Event. I'm referring to thedata bound in the datagrid.

Example of try :
CType(e.Item.FindControl("lnkFamBreDetail"),
HyperLink).NavigateUrl="mapage.aspx?" & Value (of my column bound)
OR CType(e.Item.FindControl("lnkControl"),
Hyperlink).CommandArgument = DataBound
The DataGrid is bound with a DataView (Based on a dataset).

I ' ve try this Felix's Solution (thanks)) but it does not work or i 'm very
stupid :-(

If e.Item.ItemType = ListItemType.Item Then

Dim drvTemp As System.Data.DataRowView
drvTemp = CType(e.Item.DataItem, System.Data.DataRowView)
CType(e.Item.FindControl("lnkControl"),
Hyperlink).CommandArgument = CType(drvTemp.Row.Item("Column"),
System.String) --> Here I have an error. My Column is not recognized.

End if

how referred to the databound?

I'm lost in the deep datagrid ....
Thanks for you help

fabrice
(sorry for my poor english)
Nov 19 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Hi Fabrice,

I would suggest you to use ItemDataBound event rather than ItemCreated
event, because in the first event you can access underlying data source.

The following code snippet shows how to get data and reference of columns:

If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType =
ListItemType.AlternatingItem Then
Dim drv As DataRowView = CType(e.Item.DataItem, DataRowView)
Dim link As HyperLink =
Ctype(e.Item.FindControl(“lnkFamBreDetail”),Hy perLink)
Link.NavigateUrl="mapage.aspx?queryID=" + drv(“Column_name”).ToString
End If
HTH

Elton Wang

"Fabrice" wrote:
Hello,

First, thanks to felix for his answer.

But :-( , I'm feeling newbie :! I' don't understand all the situation.
The trouble :

Always in the road whith my Datagrid and my ItemTemplate with a Control
HyperLink.
I would like to fix many properties of this Control Hyperlink in the Code
Behind. I'm using the Datagrid OnItemCreated Event.

So, I can establish a lot of properties of the Control like that :

--- In the file .aspx, in the Datagrid Control :

OnItemCreated="myDatagrid_ItemCreated"
---- In the code behind file (.aspx .vb)
Sub myDatagrid_ItemCreated(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.DataGridItemEventArgs)

If e.Item.ItemType = ListItemType.Item Then

CType(e.Item.FindControl("lnkControl"),
HyperLink).Text="myText"

End If

-->> It's OK.

But I have always the problem with the property NavigateUrl of thisControl
Hyperlink (or CommandArgument property for an another control like
ImageButton).

Question : How can I access to the value of a cell in the DataGrid in the
OnItemCreated Event. I'm referring to thedata bound in the datagrid.

Example of try :
CType(e.Item.FindControl("lnkFamBreDetail"),
HyperLink).NavigateUrl="mapage.aspx?" & Value (of my column bound)
OR CType(e.Item.FindControl("lnkControl"),
Hyperlink).CommandArgument = DataBound
The DataGrid is bound with a DataView (Based on a dataset).

I ' ve try this Felix's Solution (thanks)) but it does not work or i 'm very
stupid :-(

If e.Item.ItemType = ListItemType.Item Then

Dim drvTemp As System.Data.DataRowView
drvTemp = CType(e.Item.DataItem, System.Data.DataRowView)
CType(e.Item.FindControl("lnkControl"),
Hyperlink).CommandArgument = CType(drvTemp.Row.Item("Column"),
System.String) --> Here I have an error. My Column is not recognized.

End if

how referred to the databound?

I'm lost in the deep datagrid ....
Thanks for you help

fabrice
(sorry for my poor english)

Nov 19 '05 #2

P: n/a
Hi.

It works fine with OnItemDataBound. Thanks a lot.
Have a nice week end.

fabrice.

"Elton W" <El****@discussions.microsoft.com> a crit dans le message de
news: 32**********************************@microsoft.com...
Hi Fabrice,

I would suggest you to use ItemDataBound event rather than ItemCreated
event, because in the first event you can access underlying data source.

The following code snippet shows how to get data and reference of columns:

If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType =
ListItemType.AlternatingItem Then
Dim drv As DataRowView = CType(e.Item.DataItem, DataRowView)
Dim link As HyperLink =
Ctype(e.Item.FindControl("lnkFamBreDetail"),HyperL ink)
Link.NavigateUrl="mapage.aspx?queryID=" + drv("Column_name").ToString
End If
HTH

Elton Wang

"Fabrice" wrote:
Hello,

First, thanks to felix for his answer.

But :-( , I'm feeling newbie :! I' don't understand all the situation.
The trouble :

Always in the road whith my Datagrid and my ItemTemplate with a Control
HyperLink.
I would like to fix many properties of this Control Hyperlink in the Code
Behind. I'm using the Datagrid OnItemCreated Event.

So, I can establish a lot of properties of the Control like that :

--- In the file .aspx, in the Datagrid Control :

OnItemCreated="myDatagrid_ItemCreated"
---- In the code behind file (.aspx .vb)
Sub myDatagrid_ItemCreated(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.DataGridItemEventArgs)

If e.Item.ItemType = ListItemType.Item Then

CType(e.Item.FindControl("lnkControl"),
HyperLink).Text="myText"

End If

-->> It's OK.

But I have always the problem with the property NavigateUrl of
thisControl
Hyperlink (or CommandArgument property for an another control like
ImageButton).

Question : How can I access to the value of a cell in the DataGrid in the
OnItemCreated Event. I'm referring to thedata bound in the datagrid.

Example of try :
CType(e.Item.FindControl("lnkFamBreDetail"),
HyperLink).NavigateUrl="mapage.aspx?" & Value (of my column bound)
OR CType(e.Item.FindControl("lnkControl"),
Hyperlink).CommandArgument = DataBound
The DataGrid is bound with a DataView (Based on a dataset).

I ' ve try this Felix's Solution (thanks)) but it does not work or i 'm
very
stupid :-(

If e.Item.ItemType = ListItemType.Item Then

Dim drvTemp As System.Data.DataRowView
drvTemp = CType(e.Item.DataItem, System.Data.DataRowView)
CType(e.Item.FindControl("lnkControl"),
Hyperlink).CommandArgument = CType(drvTemp.Row.Item("Column"),
System.String) --> Here I have an error. My Column is not recognized.

End if

how referred to the databound?

I'm lost in the deep datagrid ....
Thanks for you help

fabrice
(sorry for my poor english)

Nov 19 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.