链接点击时设置变量值

时间:2022-07-18 00:14:10

I currently have a listview on an ASP.NET webpage that displays "cottage" records from an Access database. The name of each cottage is displayed as a hyperlink so when clicked brings you to another webpage:

我目前在ASP.NET网页上有一个列表视图,它显示Access数据库中的“山寨”记录。每个小屋的名称都显示为超链接,因此点击后会转到另一个网页:

<li style="">Name:
     <asp:Hyperlink ID="Cottage_NameLabel" NavigateURL="~/Cottage.aspx"
         runat="server" Text='<%# Eval("Cottage_Name") %>' />
     <br />

This works perfectly fine when selecting a hyperlink. What I want the system to do is to set the value of a publically declared variable (set in a module) to the Cottage_Name of the selected hyperlink. So say if i clicked on a hyperlink that said "cottage1", the public variable is set to "cottage1" and then the navigate URL opens the next webpage.

选择超链接时,这非常适用。我希望系统做的是将公开声明的变量(在模块中设置)的值设置为所选超链接的Cottage_Name。所以说如果我点击一个名为“cottage1”的超链接,公共变量设置为“cottage1”,然后导航URL打开下一个网页。

Would really appreciate it if anyone could help me do this!

如果有人能帮我这么做,真的很感激!

2 个解决方案

#1


2  

Just use a LinkButton instead of a Hyperlink... Catch the click event and do whatever you want...

只需使用LinkBut​​ton而不是超链接...抓住点击事件并做任何你想做的事情......

For instance:

例如:

<asp:LinkButton  ID="Cottage_NameLabel" runat="server"  Text="whatever" onclick="Cottage_NameLabel_Click" />

Then in CodeBehind:

然后在CodeBehind:

    protected void Cottage_NameLabel_Click(object sender, EventArgs e)
    {
        LinkButton btn = (LinkButton)sender;
        Session["MyCottageName"] = btn.Text;
        Response.Redirect("Cottage.aspx");
    }

In your Cottage.Aspx page you can check the value of the Session variable like this:

在Cottage.Aspx页面中,您可以检查Session变量的值,如下所示:

protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["MyCottageName"] != null)
        {
            string name = (String)Session["MyCottageName"];
            ... 
        }

#2


0  

You can pass the name as a querystring variable to the page. If you go this route, you need to make sure you URL encode the cottage name:

您可以将名称作为查询字符串变量传递给页面。如果你走这条路线,你需要确保你对山寨名称进行URL编码:

<a href='/Cottage.aspx?name=<%# Server.UrlEncode(DataBinder.Eval(Container.DataItem, "Cottage_Name")) %>'><%# Eval("Cottage_Name") %></a>

And then on cottage.aspx you can get the cottage name:

然后在cottage.aspx上你可以获得小屋名称:

Dim cottageName As String = Request.QueryString("name")

This would be preferable to a button or other postback solution as it removes the need for a postback and then a redirect.

这比按钮或其他回发解决方案更可取,因为它不需要回发,然后重定向。

#1


2  

Just use a LinkButton instead of a Hyperlink... Catch the click event and do whatever you want...

只需使用LinkBut​​ton而不是超链接...抓住点击事件并做任何你想做的事情......

For instance:

例如:

<asp:LinkButton  ID="Cottage_NameLabel" runat="server"  Text="whatever" onclick="Cottage_NameLabel_Click" />

Then in CodeBehind:

然后在CodeBehind:

    protected void Cottage_NameLabel_Click(object sender, EventArgs e)
    {
        LinkButton btn = (LinkButton)sender;
        Session["MyCottageName"] = btn.Text;
        Response.Redirect("Cottage.aspx");
    }

In your Cottage.Aspx page you can check the value of the Session variable like this:

在Cottage.Aspx页面中,您可以检查Session变量的值,如下所示:

protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["MyCottageName"] != null)
        {
            string name = (String)Session["MyCottageName"];
            ... 
        }

#2


0  

You can pass the name as a querystring variable to the page. If you go this route, you need to make sure you URL encode the cottage name:

您可以将名称作为查询字符串变量传递给页面。如果你走这条路线,你需要确保你对山寨名称进行URL编码:

<a href='/Cottage.aspx?name=<%# Server.UrlEncode(DataBinder.Eval(Container.DataItem, "Cottage_Name")) %>'><%# Eval("Cottage_Name") %></a>

And then on cottage.aspx you can get the cottage name:

然后在cottage.aspx上你可以获得小屋名称:

Dim cottageName As String = Request.QueryString("name")

This would be preferable to a button or other postback solution as it removes the need for a postback and then a redirect.

这比按钮或其他回发解决方案更可取,因为它不需要回发,然后重定向。