locked
Accessing control inside 2 DataLists RRS feed

  • Question

  • User-1304029052 posted

    I'm trying to access a hyperlink control from code behind that sits inside two custom server controls that are inherited from a datalist

    example:

    Control 1
    ....Control 2
    .......Hyperlink

    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

    Friday, April 17, 2009 11:23 AM

Answers

  • User-2106054853 posted

    Hi,

    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?

    <asp:HyperLink ID="HyperLink1" runat="server"

    onprerender="HyperLink1_PreRender">HyperLink</asp:HyperLink>

     

    protected void HyperLink1_PreRender(object sender, EventArgs e)

    {

    //sender is the HyperLink

    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, April 21, 2009 2:55 AM
  • User-1304029052 posted

    Hi

     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.

    Thanks

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, April 21, 2009 4:27 PM

All replies

  • User-1465674731 posted

     Hi,

    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)
                    return;
    // 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
    }
    }
      
    Friday, April 17, 2009 12:53 PM
  • User-1304029052 posted

    Hi

    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.

     eg.

    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.

    Friday, April 17, 2009 2:43 PM
  • User-2106054853 posted

    Hi,

    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?

    <asp:HyperLink ID="HyperLink1" runat="server"

    onprerender="HyperLink1_PreRender">HyperLink</asp:HyperLink>

     

    protected void HyperLink1_PreRender(object sender, EventArgs e)

    {

    //sender is the HyperLink

    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, April 21, 2009 2:55 AM
  • User-1304029052 posted
    It will perform a server sided call to a method that I will define
    Tuesday, April 21, 2009 12:01 PM
  • User-1304029052 posted

    Hi

     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.

    Thanks

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, April 21, 2009 4:27 PM