Solved

Dynamically create HTML anchor tag with server-side click event

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

707 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now