Solved

Dynamically create HTML anchor tag with server-side click event

Posted on 2006-11-02
4
12,446 Views
Last Modified: 2008-07-16
Hi experts,

I am dynamically creating a navigation menu with tabs for the different pages/topics of a survey.  I'm doing this by concatenating the HTML code at runtime and setting it as the innerhtml property of a DIV tag on the page.  I need to make the anchor tags trigger a server-side click event rather than set the HREF property.  This is because I'm using the page's viewstate to store the user's answers as they progress through the survey rather than make a trip to the SQL database for each tab.

Can anyone tell me if this is possible, or if I'm overlooking something obvious?  If more information/code samples are needed, please let me know.  Thanks in advance!

P.S. If this seems overly complicated, I agree.  But I have requirements for compatibility with css, etc.


        Protected WithEvents header As System.Web.UI.HtmlControls.HtmlGenericControl
        ...

        Dim strMenuHtml As New StringBuilder("<ul>")

        ' Loop through each page in the form and add a menu item
        For i As Integer = 1 To objForm.PageCount - 1
            strMenuHtml.Append("<li ")
            If i = intPage Then
                strMenuHtml.Append("id=""current"" ")
            Else
                strMenuHtml.Append("id=""menuOption" & i & """ ")
            End If
            strMenuHtml.Append("runat=""server"" command=""" & i & """><a href=""Default.aspx?Form=" & intForm & "&Page=" & i & """>" & GetPageCaption(i) & "</a></li>")
        Next

        strMenuHtml.Append("</ul>")

        ' Insert the HTML code into the menu DIV tag
        header.InnerHtml = strMenuHtml.ToString


0
Comment
Question by:esdTeamsters
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 27

Assisted Solution

by:Sammy
Sammy earned 200 total points
ID: 17864277
EsdTeamster,
You are thinking asp not asp.net in your code
what you need to do is place a placeholder in your div and add the controls to the place holder as you build them
take a look here
http://aspnet.4guysfromrolla.com/articles/081402-1.aspx
it should be a good place to start


Good Luck
0
 
LVL 10

Expert Comment

by:anv
ID: 17864296
Use __doPostBack from Javascript

Check this article for more http://www.codeproject.com/aspnet/ClientServerInteraction.asp

anv
0
 
LVL 5

Accepted Solution

by:
dynamicrevolutions earned 300 total points
ID: 17865007
   Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Dim i As Integer
       
        Dim ul As New HtmlGenericControl
        ul.TagName = "ul"
       
        Dim li As HtmlGenericControl
       
        Dim a As LinkButton
       
        For i = 0 To 10
           
            li = New HtmlGenericControl
            li.TagName = "li"
            li.ID = "menuOption" & i
           
            a = New LinkButton
            a.Text = "button" & i
            AddHandler a.Click, AddressOf someEvent
           
            li.Controls.Add(a)
           
            ul.Controls.Add(li)
           
        Next
       
        header.Controls.Add(ul)


    End Sub

    Private Sub someEvent(ByVal o As Object, ByVal e As EventArgs)
        Dim a As LinkButton = o
        Response.Write(a.Text)
    End Sub



like sammy1971  said, u're thinking too much old school asp/php/whatever
think OOP, Objects bla bla
0
 

Author Comment

by:esdTeamsters
ID: 17869071
Thanks for all the advice!  I haven't done much with HtmlGenericControl in the past, so I wasn't sure it was possible to add HTML controls in this way.  The sample code posted by dynamicrevolutions was a huge help!
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question