Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Dynamically create HTML anchor tag with server-side click event

Posted on 2006-11-02
4
Medium Priority
?
12,740 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 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

971 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