How can I get the parameters from url

48,613

Solution 1

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.

Solution 2

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.

Share:
48,613
bowang
Author by

bowang

Updated on August 21, 2020

Comments

  • bowang
    bowang over 3 years

    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?

  • bowang
    bowang over 11 years
    but I wanna write one single line inside the aspx <textbox> tag, any good idea?
  • bowang
    bowang over 11 years
    thank you! but it didn't work, where can I call the function DataBind()? where can I put this code?
  • bowang
    bowang over 11 years
    the Name.Text is in the <TextBox> ?
  • Kevin Main
    Kevin Main over 11 years
    Your TextBox is in your filename.aspx page right? So you should have a code behind file called filename.aspx.cs - in there you by default get a Page_Load function (if not you will have to add this) - add the DataBind() call inside the PageLoad.
  • bowang
    bowang over 11 years
    how about the Name.Text = Request.QueryString["Text"];? how can I use this?
  • Kevin Main
    Kevin Main over 11 years
    To do it that way (the recommended way) - ignore everything else above and simply add that one line into the Page_Load method - build it and should now work.
  • bowang
    bowang over 11 years
    this sharepoint file .aspx is in the Layout folder, how can I find the cs file that controls this? can you help me'
  • Th 00 mÄ s
    Th 00 mÄ s over 11 years
    Could your clarify on "You can initialize you server controls values only on PageLoad event.". I guess i know what you mean but its properly not clear to the OP.
  • bowang
    bowang over 11 years
    so I just put Name.Text = Request.QueryString["Text"]; in the function Page_Load(), that will work right?
  • Kevin Main
    Kevin Main over 11 years
    Yes, it is that simple - it is pretty much ASP.NET 101 really. I would suggest you read a few tutorials, watch some videos and maybe get yourself a good book before going much further.
  • Kirill Bestemyanov
    Kirill Bestemyanov over 11 years
    Page lifecycle contains next events: PreInit, Init, PreLoad, Load, Control (postback) events, LoadComplete, PreRender, SaveStateComplete, Render, Unload. All events are being catched for specific operations. PageLoad Event is a time when page is stable, it has been initialized and its state has been reconstructed. So it is time to setup values of controls from sources other than control postback events. msdn.microsoft.com/en-us/library/…
  • bowang
    bowang over 11 years
    that's coooooool! so what should I write in the <TextBox>?
  • Th 00 mÄ s
    Th 00 mÄ s over 11 years
    Thanks for adding that information. Ofcourse methods started from the event "PageLoad" can change controls themself.
  • Shadow The Kid Wizard
    Shadow The Kid Wizard over 11 years
    @bowang nothing - that's the beauty in this.
  • bowang
    bowang over 11 years
    oops, I just add the script under the buttom and a run time error happened, something wrong with the js file?(I just added them at the end and did nothing more, even not editing the tag)
  • bowang
    bowang over 11 years
    sharepoint doesn't allow me to see the .cs code behind it, is there any other way I can do?