2

Hi i am dynamically creating link buttons as shown below. the problem is how to add the link button in the place of "linktopage". currently the link button is added below the table.

for(i=0;i<100;i++)
{
LinkButton lnk = new LinkButton();
lnk.ID = "lnk" + i;
lnk.Text = "open profile";
lnk.Click += new System.EventHandler(lnk_click);
this.Page.Form.Controls.Add(lnk);

htmlstring += "<tr style='height:30px;'>" +
              "<td>" + firstname + "</td>" +
              "<td>" + surname + "</td>" +
              "<td>" + email + "</td>" +
              "<td>" + mobile + "</td>" +
              **"<td>" + linktopage + "</td>" +**
              "</tr>";
                  }   
5
  • where does the linktopage comes from? Commented May 24, 2011 at 11:20
  • i just added it as dummy placeholder. actually i want the linkbutton to be added there. for example it should be rendered as first name, surnmae,email,mobile,htmllink Commented May 24, 2011 at 11:26
  • so, your linktopage is an action page, like, executing a page, or executing a command? Do you want this to be synchronously or asynchronously? Commented May 24, 2011 at 11:31
  • actually i want to call a c# method which does some operation and redirects to another page. Commented May 24, 2011 at 11:34
  • then, that's the normal behavior of a POST, if you use the Command instead Click event you can even pass variables to your method Commented May 24, 2011 at 11:40

3 Answers 3

2

from your comments

in .aspx.cs

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
            Populate();
    }

    private void Populate()
    {
        List<MyTableData> list = new List<MyTableData>();

        for (int i = 0; i < 10; i++)
        {
            list.Add(
                new MyTableData()
                {
                    FirstName = "Firstname " + i.ToString(),
                    LastName = "Lastname " + i.ToString(),
                    Email = "Email " + i.ToString(),
                    Mobile = "Mobile " + i.ToString(),
                    CmdArgument = i.ToString()
                });
        }

        gv.DataSource = list;
        gv.DataBind();
    }

    protected void lnkBtn_Command(object sender, CommandEventArgs e)
    {
        string btnNumber = e.CommandArgument.ToString();

        // more code...

        lit.Text = "Button pressed <b>" + btnNumber + "</b>";
    }
}

public class MyTableData
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Email { get; set; }
    public string Mobile { get; set; }
    public string CmdArgument { get; set; }
}

in .aspx

<div>
    <asp:GridView ID="gv" runat="server" AutoGenerateColumns="False" CellPadding="5">
        <Columns>
            <asp:BoundField DataField="FirstName" HeaderText="First name" />
            <asp:BoundField DataField="LastName" HeaderText="Last name" />
            <asp:BoundField DataField="Email" HeaderText="Email" />
            <asp:BoundField DataField="Mobile" HeaderText="Mobile" />
            <asp:TemplateField HeaderText="">
                <ItemTemplate>
                    <asp:LinkButton ID="lnkBtn" runat="server" OnCommand="lnkBtn_Command" CommandArgument='<%# Eval("CmdArgument") %>'
                        Text='<%# Eval("CmdArgument", "Button {0}") %>' />
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>
</div>
<hr />
<asp:Literal ID="lit" runat="server" />

the result is

enter image description here

Source Code available

Sign up to request clarification or add additional context in comments.

1 Comment

@balexandre-thanks a lot. I will implement this and get back to you.
0

How about:

Replacing **"<td>" + linktopage + "</td>" +**, with <td><asp:Panel id="lnktoPage" /></td> And add it from the code, lnktoPage.Controls.Add(lnk);

Note that Panel renders as DIV on client side.

Or... you can create the html link dynamically:

string strID = "someID";
string strLink = @"<a id=""" + strID + @"" +
                @" onclick="" " + lnk_clickMethodName + @" "" " +
                @"href=""http://www.w3schools.com"">Visit W3Schools.com!</a>";

So, your final method would look something like:

for (int i = 0; i < 100; i++)
{
    string strID = "lnk" + i.ToString();
    string strLink = @"<a id=""" + strID + @"" +
                    @" onclick="" " + lnk_clickMethodName + @" "" " +
                    @"href=""http://www.w3schools.com"">Visit W3Schools.com!</a>";
    StringBuilder html = new StringBuilder();

    html.Append(@"<tr style='height:30px;'>");
    html.Append(@"<td>" + firstname + "</td>");
    html.Append(@"<td>" + surname + "</td>");
    html.Append(@"<td>" + email + "</td>");
    html.Append(@"<td>" + mobile + "</td>");
    html.Append(@"<td>" + strLink + "</td>");
    html.Append(@"</tr>");
}

1 Comment

i m getting this error Operator '+' cannot be applied to operands of type 'string' and 'void'
0

instead of link button, use href tag; it will be easy.....

1 Comment

i used this code <a href="<%#MetthodToCall(); %>">Call C# Method</a> but no luck

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.