vb.net 单击链接时设置变量的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15115864/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Set value of variable upon link click
提问by Chris Stone
I currently have a listviewon 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:
我目前listview在 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_Nameof 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!
如果有人能帮我做到这一点,我将不胜感激!
回答by Blachshma
Just use a LinkButtoninstead of a Hyperlink... Catch the click event and do whatever you want...
只需使用LinkButton而不是超链接......捕捉点击事件并做任何你想做的......
For instance:
例如:
<asp:LinkButton ID="Cottage_NameLabel" runat="server" Text="whatever" onclick="Cottage_NameLabel_Click" />
Then in 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"];
...
}
回答by MikeSmithDev
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.
这比按钮或其他回发解决方案更可取,因为它不需要回发然后重定向。

