简体   繁体   中英

C# - Transfer the text of a hyperlink through a session inside a repeater

I want to transfer the text of a hyperlink through a session inside a repeater. But I can not think of the logic to do this. My repeater generates links within that dropdown menu and I want to get the text that appears in the link and pass it to another page Sectors.aspx . This is what I have done so far:

--- edit ---

I want that when the link is clicked, the text that is in the link is passed to the other page. For example, the menu has two links, APPLE and BANANA. When the user clicks APPLE, I want the next page Sectors.aspx to know that the user clicked APPLE and not the other options.

ASPX Page:

<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>

Code Behind

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

You could add the text as a parameter of the 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>

Then, Sectors.aspx can retrieve it through the "sector" parameter:

protected void Page_Load(object sender, EventArgs e)
{
    string sector = Request.Params["sector"];
}

That id="hyperlink1" parameter looks fishy, though. You don't want all your hyperlinks to have the same ID.

Just replace hyperlink tag with this code ,

   <asp:HyperLink runat="server" Navigateurl='<%#"Sectors.aspx?mySector="+ Eval("sector") %>'   
                    Text='<%#((System.Data.DataRowView)Container.DataItem)["sector"] %>' />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM