sam
sam

Reputation: 21

link button inside html table cell asp.net

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>";
                  }   

Upvotes: 2

Views: 12064

Answers (3)

vasmay
vasmay

Reputation: 1429

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

Upvotes: 0

balexandre
balexandre

Reputation: 75073

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

Upvotes: 2

Kamran Khan
Kamran Khan

Reputation: 9986

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>");
}

Upvotes: 0

Related Questions