English 中文(简体)
在 asp.net 控件中显示查询字符串
原标题:Display query string in asp.net controls

我正用 VB 后端撰写 ASP 网络应用程序。 我想做的是生成一个 URL 并在页面上显示它。 例如, 如果我在窗体上有一个标签和按钮, 标签是空白的。 当单击以下代码时, 标签是空的 :

 Protected Sub btnGenerate_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnGenerate.Click
    label1.Text = "Hello"
End Sub

我最想要的是一个能指给我的ASP页面 标签上写着"你好"字眼的骨灰盒。这有可能吗?

最佳回答

通过查询字符串中的文本,例如,假设页面的相对路径是 / pagename.aspx,您可以按下面的示例通过查询字符串:

/pagename.aspx?text=hello

在 page_Load 事件中, c# 写入以下代码

//You don t have to check the url all the time , so just check it if page is not posting back (first time after user visits the page and ignore all other same page post backs. Label can maintain its control state (text value) after every postback, so assign it only once to increase performance
if (!IsPostBack)
{
    //Check if query string is provided or not , if it is not provided take some default text, I am taking empty string as default text.
    string givenText = (Request.QueryString["text"] == null)?"":Request.QueryString["text"];
    label1.Text = givenText;
}

也可以为通过查询字符串和默认文本提供的文本创建属性。

问题回答

你可以做以下工作:

{siteaddress}/aspxpage.aspx?label=hello

然后在您 Aspx 页面中做一些类似的事情:

<asp:label runat="server" id="yourLabelId" text= <%=Request.QueryString("label")%>  />

或在《页眉》中:

yourLabelId.Text = Request.QueryString("label")

我建议,在将数据写进网页之前,先核实数据。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签