C# 如何从url获取参数

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

How can I get the parameters from url

c#asp.netsharepoint

提问by bowang

I'm writing an aspx to let users check the filename and create a file with that name

我正在编写一个 aspx 来让用户检查文件名并使用该名称创建一个文件

the url is

网址是

/sites/usitp/_layouts/CreateWebPage.aspx?List=%7b74AB081E-59FB-45A5-876D-
                             284607DA03C6%7d&RootFolder=%3bText=%27SD_RMDS%27

how can I parse the parameter 'Text' and show in the textbox?

如何解析参数“文本”并显示在文本框中?

<div>
    <asp:TextBox id="Name" runat="server" />
</div>

the aspx text box is this, I tried

aspx 文本框是这个,我试过了

<asp:TextBox id="Name" runat="server" text=<%$Request.QueryString['Text']%>></asp:TextBox>>

but it didn't work

但它没有用

anyone can help me out?

任何人都可以帮助我吗?

回答by Remy

Actually, it would be

其实会是

string value = Name.Text;

回答by Kirill Bestemyanov

If you want get text value from Querystring you need to use:

如果您想从 Querystring 获取文本值,您需要使用:

var text = (string)Request.QueryString["Text"];

Then you can bind it to Text property of TextBox Name:

然后你可以将它绑定到 TextBox Name 的 Text 属性:

 Name.Text = text;

Update:You can initialize you server controls values only on PageLoad event.

更新:您只能在 PageLoad 事件上初始化您的服务器控件值。

回答by Th 00 m? s

To get the value for the http get Parameter:

要获取 http get 参数的值:

string testParameter = Request.QueryString["Text"];

then set the Textbox Text

然后设置文本框文本

Name.Text = testParameter

Also its strongly suggested to not take Content directly from the url as malicious content could be injected that way into your page. ASP offers some protection from this, still its considered a good practice.

此外,强烈建议不要直接从 url 获取内容,因为恶意内容可能会以这种方式注入您的页面。ASP 提供了一些保护,但它仍然被认为是一种很好的做法。

回答by Kevin Main

You seem to be missing an & in your url between RootFolder and Text so change it to this -

您似乎在 RootFolder 和 Text 之间的 url 中缺少 &,因此将其更改为 -

/sites/usitp/_layouts/CreateWebPage.aspx?List=%7b74AB081E-59FB-45A5-876D-284607DA03C6%7d&amp;RootFolder=%3b&Text=%27SD_RMDS%27

In terms of binding your are almost right, this should do it -

在绑定方面你几乎是正确的,这应该这样做 -

<asp:TextBox id="Name" runat="server" text='<%#Request.QueryString["Text"]%>'></asp:TextBox>

However, if you run this now it will not work as you will need to call DataBind() in your PageLoad like this

但是,如果您现在运行它,它将无法工作,因为您需要像这样在 PageLoad 中调用 DataBind()

protected void Page_Load(object sender, EventArgs e)
{
    DataBind();
}

This should do as you want although it is probably easier just to do this directly in your PageLoad like this -

这应该按照您的意愿进行,尽管像这样直接在您的 PageLoad 中执行此操作可能更容易 -

Name.Text = Request.QueryString["Text"];

回答by Shadow Wizard is Ear For You

If you don't have access to the code behind (common limitation in SharePoint) then you can use JavaScript "hack" to populate the textbox with the URL value.

如果您无权访问背后的代码(SharePoint 中的常见限制),那么您可以使用 JavaScript“hack”来使用 URL 值填充文本框。

To achieve this, place this code in the very bottom of the .aspxpage with the textbox:

为此,请将此代码放置在.aspx带有文本框的页面最底部:

<script type="text/javascript">
    var strTextBoxId = "<%=Name.ClientID%>";
    var oTextBox = document.getElementById(strTextBoxId);
    if (oTextBox) {
        oTextBox.value = "<%=Request.QueryString["Text"].Replace("\"", "\\"")%>";
    }
    else {
        //debug
        alert("element with ID '" + strTextBoxId + "' does not exist");
    }
</script>

Note this is notgood practice, just a way around when you can't do the best practice solution.

请注意,这不是好的做法,只是当您无法执行最佳实践解决方案时的一种解决方法。