3

I am creating LINKbuttons Dynamically:

Lnk1

Lnk2

..

I want to Dynamically but a text(Or any HTML Element) between them for example:

Lnk1
Seperator
Lnk2
Sepeperator

Here is my code:

 for (int i=0;i<10;i++)
            {
    form1.Controls.Add(lnks[i]);
    Response.Write("Seperator<br>");
}

But in output I get the following picture:

enter image description here

Please let me know how to add one text exactly after one linkButton.

S Nash
  • 2,363
  • 3
  • 34
  • 64

2 Answers2

7

Take a look at this approach. You're on the right track, but you need to think of the form itself. The form has a collection property called Controls. If you instantiate controls in your for loop, you can effectively add each control sequentially. First, create your LinkButton instance and assign it's properties. Then, create whatever sort of separator control you would like to use. I used an HtmlGenericControl in this example which allows us to assign a text property (of Separator). Notice I started my loop at 1 instead of zero to match your example ... hopefully this helps.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        for (int i = 1; i < 11; i++)
        {
            LinkButton linkButton = new LinkButton();
            linkButton.Text = "Lnk" + i;
            linkButton.Click += linkButton_Click;
            form1.Controls.Add(linkButton);

            HtmlGenericControl p = new HtmlGenericControl("p");
            p.InnerText = "Separator";
            form1.Controls.Add(p);
        }
    }

    void linkButton_Click(object sender, EventArgs e)
    {
        Response.Redirect("http://www.stackoverflow.com");
    }
}
darlirium
  • 370
  • 4
  • 9
  • nice info +1. I hanged the accepted answer to yours. Since HtmlGenericControl seem to be for fit for my needs. I need to add tags and change their styles. Literalcontrol object does not have a style property. – S Nash Feb 06 '15 at 15:10
  • To be fair below answer was helpful and should be down voted. So please up vote it. It does not let me upvote. – S Nash Feb 06 '15 at 15:11
  • @SNash. I hear ya. I didn't down vote the other answer. (I'm not really into that personally. Most people are trying to be helpful and are not overly concerned with reputation points.) I did go ahead and up vote it because you found it helpful. – darlirium Feb 06 '15 at 16:27
1

Try using this as a ref. its working code in my past project.

for (int i = 0; i <= rows - 1; i++)
            {
                HyperLink MyLink = new HyperLink();

                //Set the Hyperlink Text and ID properties.
                MyLink.Text = "YOUR TEXT";
                MyLink.ID = "Link Id";
                // Add a spacer in the form of an HTML <br /> element.
                Panel1.Controls.Add(new LiteralControl("<br />"));
                Panel1.Controls.Add(MyLink);
            }
  • no your code does not work. Still any tag(Literal control) appears before LinkButton. So they do not appear by order. – S Nash Feb 06 '15 at 14:35
  • OK I found the reason. I was setting the LinkButton position to absolute. Also using LiteralControl is really helpful here. If you add a comment I will upvote you and will accept your answer since comparing your code to mine helped me to find the reason. – S Nash Feb 06 '15 at 14:55
  • Also please remove the unneeded info from your answer: like `MyLink.ID = admission.Rows[i][0].ToString(); username = admission.Rows[i][3].ToString(); string Refno = HttpUtility.UrlEncode(Encrypt(admission.Rows[i][0].ToString()));` – S Nash Feb 06 '15 at 14:57