Solved

Dynamically create HTML anchor tag with server-side click event

Posted on 2006-11-02
4
12,287 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
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

775 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