[英]C# - Transfer the text of a hyperlink through a session inside a repeater
我想通過中繼器內的會話傳輸超鏈接的文本。 但我想不出這樣做的邏輯。 我的轉發器在該下拉菜單中生成鏈接,我想獲取鏈接中顯示的文本並將其傳遞到另一個頁面Sectors.aspx
。 這是我到目前為止所做的:
- - 編輯 - -
我希望當點擊鏈接時,鏈接中的文本被傳遞到另一個頁面。 例如,菜單有兩個鏈接,APPLE 和 BANANA。 當用戶單擊 APPLE 時,我希望下一頁Sectors.aspx
知道用戶單擊了 APPLE 而不是其他選項。
ASPX 頁面:
<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
<div class="dropdown">
<button class="dropbtn">Setor</button>
<div class="dropdown_content">
<asp:Repeater ID="sectors_menu" runat="server">
<ItemTemplate>
<asp:HyperLink id="hyperlink1" NavigateUrl="Sectors.aspx" Text='<%#((System.Data.DataRowView)Container.DataItem)["sector"] %>' runat="server"></asp:HyperLink>
</ItemTemplate>
</asp:Repeater>
</div>
</div>
</asp:Content>
背后的代碼
public partial class _Default : System.Web.UI.Page
{
MySqlConnection mysql_connection = new MySqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString);
string mysql_string;
MySqlDataAdapter mysql_data_adapter;
public void Page_Load(object sender, EventArgs e)
{
Sector_label();
}
public void Sector_label()
{
mysql_string = "SELECT * FROM employees GROUP BY sector";
mysql_data_adapter = new MySqlDataAdapter(mysql_string, mysql_connection);
DataTable data_table = new DataTable();
mysql_data_adapter.Fill(data_table);
DataView data_view = new DataView(data_table);
sectors_menu.DataSource = data_view;
sectors_menu.DataBind();
for (int count = 0; count < sectors_menu.Items.Count; count++)
{
var test = (HyperLink)sectors_menu.Items[count].FindControl("hyperlink1");
Session["session_hyperlink"] = test.Text;
// Debug.WriteLine(test.Text);
}
}
}
您可以將文本添加為NavigateUrl
的參數:
<ItemTemplate>
<asp:HyperLink id="hyperlink1" NavigateUrl='<%# "Sectors.aspx?sector=" + Server.UrlEncode(((System.Data.DataRowView)Container.DataItem)["sector"]).ToString() %>' Text='<%#((System.Data.DataRowView)Container.DataItem)["sector"] %>' runat="server"></asp:HyperLink>
</ItemTemplate>
然后, Sectors.aspx
可以通過“sector”參數檢索它:
protected void Page_Load(object sender, EventArgs e)
{
string sector = Request.Params["sector"];
}
不過,那個id="hyperlink1"
參數看起來很可疑。 您不希望所有超鏈接都具有相同的 ID。
只需用此代碼替換hyperlink
標簽,
<asp:HyperLink runat="server" Navigateurl='<%#"Sectors.aspx?mySector="+ Eval("sector") %>'
Text='<%#((System.Data.DataRowView)Container.DataItem)["sector"] %>' />
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.