0

I want to add some asp.net linkbutton controls to a literal on a c# web form application like code below:

StringBuilder sb = new StringBuilder("<ul>");
sb.Append("<li>");
sb.AppendFormat("<asp:LinkButton runat='server' class='add-row' ID='BtnAdd' OnClick='BtnAdd_Click' CommandArgument='test'>{0}</asp:LinkButton>", "Text on the link");
sb.Append("</li></ul>");
this.Literal.Text = sb.ToString();`

and also i want to fire event of click and get CommandArgument's value of that.

private void BtnAdd_Click(object sender, EventArgs e)
{
   //get the value of CommandArgument
}

I tried these and the linkbutton added to the literal successfully. but the linkbutton didn't convert to <a> tag and i found that with the firebug like this and no any events couldn't fire: enter image description here

I want make a tree with leafs as linkbuttons using <ul><li> tags overall. It would be very helpful if someone could explain solution for this problem.

शेखर
  • 17,412
  • 13
  • 61
  • 117
Reza Amini
  • 476
  • 1
  • 5
  • 20
  • Server-side control cannot be added as a text, or a child, of Literal control. Whatever you add to Literal control is treated as literal string, that is why it is called Literal. You need to find alternative way to do whatever you want to do – Andrei Oct 24 '16 at 10:17

5 Answers5

2

The correct approach here is via the WebControl api and not from the template. Your approach may work for HTML content only. What you need to do is handle a page lifecycle event like OnInit, get a reference to your container control (this.MyPanel), and dynamically add items to that control's children property (this.MyPanel.Children.Add(new....))

The suggested Repeater solution is not suited for tree structure data sources, works for a list though...

1

As suggested by Andrei using linkbuttons in this way will not work.

I have used a similar solution in the past by creating a query parameter for each a tag in the literal and redirecting to either the current page or a different page and extracting the query parameter from there.

I would however advise you to first try and see if there is not a different way to accomplish what you are trying to do as the literal solution ends up very cluttered and code heavy.

ThatChris
  • 752
  • 1
  • 4
  • 18
1

I think a Repeater would better suit your purpose.

<ul>
    <asp:Repeater ID="Repeater1" runat="server">
        <ItemTemplate>
            <li><asp:LinkButton ID="BtnAdd" runat="server" CssClass="add-row" OnClick="BtnAdd_Click">Text on the link</asp:LinkButton></li>
        </ItemTemplate>
    </asp:Repeater>
</ul>

And if you want to use CommandArgument you need an OnCommand, not a OnClick

<asp:LinkButton ID="BtnAdd" runat="server" CssClass="add-row" OnCommand="BtnAdd_Command" CommandArgument='<%# Eval("value") %>'>

protected void BtnAdd_Command(object sender, CommandEventArgs e)
{
     string commandArgument = e.CommandArgument.ToString();
}
VDWWD
  • 35,079
  • 22
  • 62
  • 79
1

If you have single <asp:LinkButton ..> then you can set it's property at run time.
Like

BtnAdd.Id="";

or if you multiple then you can use AddControl method below is link

How to add controls dynamically when click button in asp.net?
How to add controls dynamically to ASP.NET form?

Community
  • 1
  • 1
शेखर
  • 17,412
  • 13
  • 61
  • 117
-1

If you have to use asp: LinkButton just for one time then do it in this way.

<asp:Literal runat="server" ID="DocLink"></asp:Literal>

<asp:LinkButton runat="server" ID="LinkButton1" OnCommand='LinkButton1_Click'></asp:LinkButton>

and in .cs file

LinkButton1.Text = CurrentListItemAbsoluteUrl;

LinkButton1.CommandArgument = CurrentListItemAbsoluteUrl;

public void LinkButton1_Click(Object sender, CommandEventArgs e)
{
    e. CommandEventArgs //To get your parameter 
Josef
  • 2,869
  • 2
  • 22
  • 23