您好我正在动态创建链接按钮,如下所示。问题是如何在“linktopage
”的位置添加链接按钮。目前,链接按钮添加在表格下方。
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>";
}
答案 0 :(得分:2)
来自您的评论
.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; }
}
.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" />
结果是
答案 1 :(得分:0)
怎么样:
用**"<td>" + linktopage + "</td>" +**
替换<td><asp:Panel id="lnktoPage" /></td>
并从代码中添加lnktoPage.Controls.Add(lnk);
请注意,Panel
在客户端呈现为DIV
。
或者......你可以动态创建html链接:
string strID = "someID";
string strLink = @"<a id=""" + strID + @"" +
@" onclick="" " + lnk_clickMethodName + @" "" " +
@"href=""http://www.w3schools.com"">Visit W3Schools.com!</a>";
所以,你的最终方法看起来像是:
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>");
}
答案 2 :(得分:0)
代替链接按钮,使用href
标记;这很容易......