創建按鈕如果你不想使用網絡的數據綁定控件像Repeater
您可以創建LinkButton
小號動態。請記住,在最新作之前重新創建它們在具有相同ID的回傳在Page_Load
:
protected void Page_Init(object sender, EventArgs e)
{
createTagButtons();
}
private void createTagButtons()
{
var tblTags = new DataTable();
using (var con = new SqlConnection(connectionString))
using (var da = new SqlDataAdapter("SELECT TagID, TagName FROM dbo.Tags ORDER BY TagName", con))
{
da.Fill(tblTags);
}
foreach (DataRow row in tblTags.Rows)
{
int tagID = row.Field<int>("TagID");
string tagName = row.Field<string>("TagName");
LinkButton tagButton = new LinkButton();
tagButton.ID = "tagButton_" + tagID;
tagButton.CommandArgument = tagName;
tagButton.Click += TagLinkClicked;
this.TagPanel.Controls.Add(tagButton);
}
}
private void TagLinkClicked(Object sender, EventArgs e)
{
LinkButton tagLink = (LinkButton)sender;
string url = string.Format("Tags.aspx?name={0}", tagLink.CommandArgument);
Response.Redirect(url);
}
在ASPX你可以使用一個Panel
:
<asp:Panel ID="TagPanel" runat="server"></asp:Panel>
你試圖克雷婭察一個按鈕,每個標籤在你的代碼?您可以拆分字符串併爲每個字符串創建一個新按鈕。 – Constanta