Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Dynamically create HTML anchor tag with server-side click event

Posted on 2006-11-02
4
Medium Priority
?
12,651 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 Ageil
Sammy Ageil earned 800 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 1200 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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 recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

719 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