简体   繁体   中英

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

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

<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:

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.

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

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

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

 Name.Text = text;

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

实际上,它会

string value = Name.Text;

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

/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

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 -

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.

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

<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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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