[RESOLVED] creating hyperlinks dynamically.

I want to create hyperlink dynamically whenever i click the button below is my code

int j = 1;
        for (int i = 0; i < j; j++)
            //HyperLink links = new HyperLink();
            HyperLink hplink = new HyperLink();
            hplink.NavigateUrl = "http://www.asp.net";
            hplink.Text = "Click Here" + "<br /><br />";

but i am unable to get the hyperlinks dynamically because the loop is going infinite.

for (int i = 0; i < j; j++) 

--> for (int i = 0; i < j; i++) 

Replace j++ with i++, since i is your looping variable. 

Ya when i have done what you have said but i am getting only one hyperlink when i press the button,i want to createhyperlinks whenever i click the button, i mean how many times i click the button that many times i have to get hyperlink. 

Here is a way to do that

1;) in your aspx markup :

    <form id="form1" runat="server">
    <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
    <br />
    <asp:Literal runat="server" ID="LinksPlaceHolder" />

2;) in the code-behind :

    protected void Button1_Click(object sender, EventArgs e)
            LinksPlaceHolder.Text += "<a href=\"http://www.asp.net/\">go to the asp.net website</a><br/></br>";

if this  is not what you want, explain exactly what your are trying to do

This video shows one way of doing something like that: http://www.asp.net/ajax/videos/how-to-dynamically-add-controls-to-a-web-page 

There is a major problem with creating a new hyperlink each time a button is pressed.  Dynamic controls have to be redrawn each time the page loads.  In order to redraw a different hyperlink each time a button is pressed you will need to have a lists of hyperlinks in a DataTable or HybridDictionary that create the hyperlinks each time the button click causes a postback.  Your redraw loop is close but it should use a For Each and loop through the collection of hyperlink data and draw a control for each one during the Page Init method.  In the button click simply add the new hyperlink data to the collection.  Dynamic controls are pain and should be avoided whenever possible. 

Are you saying about tables rows and columns and adding the control to column and generating whenever the button is clicked?? but i want to achieve with a for loop is it possible?? 

For example the DataTable would contain columns CONTROL_ID, URL, TEXT.  Each time the button is clicked you create a new row from the DataTable fill in the fields and add the row to the data and store it in the ViewState.  Each time the page load you read the DataTable with your for each Row loop and add your hyperlinks to the placeholder.  This will make sure the drawing is constant each time and you only have to debug the data is getting updated and added properly.   

I think a little bit of code can explain much than big sentences.

Invalid loop for (int i = 0; i < j; j++)  

Write loop 

>> for (int i = 0; i < j; i++)  


Http is stateless, Dynamically created control can not maintain itself after postback.

If you dynamically add a HyperLink Control to the page during button click, be aware that every PostBack is a new Request .They need to be recreated each time the page loads.

Check this two links, they give you some example and explanation:



Hope this can help you.

Here you have increamented j++, that should be i++. so in your case conditon not get false at any time so that is goes in infinate looks

change as per below code

int j = 1;
        for (int i = 0; i < j; i++)
            //HyperLink links = new HyperLink();
            HyperLink hplink = new HyperLink();
            hplink.NavigateUrl = "http://www.asp.net";
            hplink.Text = "Click Here" + "<br /><br />";


Please mark as answer if this is helps


thoughts on "[RESOLVED] creating hyperlinks dynamically."

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>