C# 单击asp.net按钮时如何在url后添加查询字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16404841/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 00:56:48  来源:igfitidea点击:

How to add a querystring after url when asp.net button is clicked

c#asp.netpostbackquery-string

提问by HanYi Zhang

I have a asp.net page that has a button to click. When click on it, I wish to have a querystring such as ?id=1 added after the normal url. How can I do that from server side c# code?

我有一个 asp.net 页面,有一个按钮可以点击。单击它时,我希望在普通 url 之后添加一个查询字符串,例如 ?id=1。我怎样才能从服务器端 C# 代码做到这一点?

采纳答案by Jasmine

Three ways... server-side redirect, LinkButton, and client-side button or link.

三种方式...服务器端重定向、LinkBut​​ton 和客户端按钮或链接。

You can have your button event handler redirect to a location with a querystring...

您可以将按钮事件处理程序重定向到带有查询字符串的位置...

 Response.Redirect("myPage.aspx?id=" + myId.toString(), true);

You can render the button as a LinkButton and set the URL...

您可以将按钮呈现为 LinkBut​​ton 并设置 URL...

 LinkButton myLinkButton = new LinkButton("myPage.aspx?id=" + myId.toString(), true);

Or, you can render the button as a client side link - this is what I do when using Repeater controls...

或者,您可以将按钮呈现为客户端链接 - 这就是我在使用中继器控件时所做的......

 <a href='myPage.aspx?id=<%# Eval("myID") %>'>Link</a>

I prefer the last method, particularly when I need a whole bunch of links.

我更喜欢最后一种方法,特别是当我需要一大堆链接时。

BTW, this is application of KISS - all you need is a regular old link, you don't need to jump through server-side hoops to create a link with a querystring in it. Using regular client-side HTML whenever possible is how to keep ASP.Net simple. I don't see enough of that technique in the wild.

顺便说一句,这是 KISS 的应用程序 - 您只需要一个常规的旧链接,您不需要跳过服务器端箍来创建带有查询字符串的链接。尽可能使用常规的客户端 HTML 是保持 ASP.Net 简单的方法。我在野外看不到足够的这种技术。

回答by DeveloperBuddy

There are various way to add querystring in url. You can use following code If you want to add value on server side:

有多种方法可以在 url 中添加查询字符串。如果要在服务器端添加值,可以使用以下代码:

protected void Button_Click(object sender, EventArgs e)
{
   Int32 id = 1;
   // Or your logic to generate id
   string url = String.Format("anypage.aspx?id={0}",id.ToString());

}

回答by Vishal Gupta

How to build a query string for a URL in C#?

如何在 C# 中为 URL 构建查询字符串?

Or you can use this code.

或者您可以使用此代码。

string url = Request.Url.GetLeftPart(UriPartial.Path);
url += (Request.QueryString.ToString() == "" ) ? "?pagenum=1" : "?" + Request.QueryString.ToString() + "&pagenum=1";

回答by user0474975

I realize this is an old question, but I had the exact same problem when we wanted to create a new URL string so Google Analytics could "count" when a form was submitted.

我意识到这是一个老问题,但是当我们想要创建一个新的 URL 字符串以便 Google Analytics 可以在提交表单时“计数”时,我遇到了完全相同的问题。

I was using an ASP:Button to submit the form and saving to a database and displaying a thank-you message on postback. Therefore, the URL pre- and post-submit was the same, and I had to modify it in some way.

我使用 ASP:Button 提交表单并保存到数据库并在回发时显示感谢消息。因此,提交前和提交后的 URL 是相同的,我不得不以某种方式对其进行修改。

I used the following code and it worked for me:

我使用了以下代码,它对我有用:

C#, in Page_Load:

C#,在 Page_Load 中:

btnSubmit.PostBackUrl = "Page.aspx?id=" + id.ToString() + "&newquerystring=newvalue";

where Page.aspx is the page with the button that I want to postback to (the same page), ID is a dynamic ID I'm using to grab content from our CMS, and obviously the new querystring item.

其中 Page.aspx 是带有我想要回发到的按钮的页面(同一页面),ID 是我用来从我们的 CMS 中获取内容的动态 ID,显然是新的查询字符串项。

I hope this helps.

我希望这有帮助。