Last post Apr 21, 2009 04:27 PM by paulcreedy
Apr 17, 2009 11:23 AM|paulcreedy|LINK
I'm trying to access a hyperlink control from code behind that sits inside two custom server controls that are inherited from a datalist
I need to set the hyperlink navurl property from code behind.
I can get at control 2 using ItemDataBound, but how do I get at the hyperlink inside Control 2
Apr 17, 2009 12:53 PM|nizam133|LINK
I will suggest you to use link button inside ur datalist and then use set CommandName property of link button to something e.g. "NavigateToURL" , now in code behind file in Data List Item command click event you use following code
protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
if (e.CommandName == "NavigateURL")
LinkButton lnkbtn = (LinkButton)e.Item.FindControl("lnkBtnID");
if (lnkbtn == null)
// here you can use write code to navigate to where ever you want
//if u want your navigation url dynamically change as per the selected item in the list then
//u can also do that when you are binding the some proper value to the link button or u can bind link button
//PostBackURL property to your desired location
Apr 17, 2009 02:43 PM|paulcreedy|LINK
The problem I have is that the control whether a hyperlink or button is inside the inner server control; although I will need it to be a hyperlink.
I can get at a hyperlink control inside the first control (a datalist), using findcontrol but there is another inner control and the hyperlink is inside that.
Datalist 1 (uses a <itemtemplate> to hold content)
The content it is holding inside its itemtemplate is another datalist which also uses an <itemtemplate>
The hyperlink in question therefore is inside the itemtemplate of the second inner datalist.
The code behind page only exposes the events of the first datalist (the outer one), and not the inner datalist.
Apr 21, 2009 02:55 AM|Allen Chen – MSFT|LINK
Could you tell me what do you want to do with that hyperlink? Can you use the PreRender event to get the reference of it?
Apr 21, 2009 12:01 PM|paulcreedy|LINK
Apr 21, 2009 04:27 PM|paulcreedy|LINK
I've sort of got around the problem now by taking the inner datalist control out and placing it at the top level. This means that I can now get the hyperlink inside it from code behind. Whilst not the direct answer to what I was looking for your last suggestion
caused me to think in a different way which accomplished the same end result.