简体   繁体   English

如何从url获取参数

[英]How can I get the parameters from url

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? 如何解析参数'Text'并显示在文本框中?

<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? 有人可以帮帮我吗?

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. ASP提供了一些保护,仍然被认为是一种很好的做法。

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事件上初始化服务器控件值。

实际上,它会

string value = Name.Text;

You seem to be missing an & in your url between RootFolder and Text so change it to this - 您似乎在RootFolder和Text之间缺少一个&,因此将其更改为 -

/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"];

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 .aspx page 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 not good practice, just a way around when you can't do the best practice solution. 请注意,这不是一个好的做法,只是当你无法做出最佳实践解决方案时。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM