11

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?

4
  • 1
    Is that the correct URL? looks like you are missing an & between RootFolder and Text parameters so it will not see the Text parmater and you will not be able to pull it back. Commented Aug 20, 2012 at 12:50
  • <asp:TextBox id="Name" runat="server" text="<%$Request.QueryString('Text')%>" ></asp:TextBox> Commented Aug 20, 2012 at 13:00
  • Do you know about page lifecycle? You can initialize your server control on PageLoad event. Commented Aug 20, 2012 at 13:24
  • nope, a rookie :P, Can you tell me how to write a page_load() into that aspx file? Commented Aug 20, 2012 at 13:35

5 Answers 5

21

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.

Sign up to request clarification or add additional context in comments.

3 Comments

but I wanna write one single line inside the aspx <textbox> tag, any good idea?
the Name.Text is in the <TextBox> ?
this sharepoint file .aspx is in the Layout folder, how can I find the cs file that controls this? can you help me'
5

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.

3 Comments

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.
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/…
Thanks for adding that information. Ofcourse methods started from the event "PageLoad" can change controls themself.
0

Actually, it would be

string value = Name.Text; 

Comments

0

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

7 Comments

thank you! but it didn't work, where can I call the function DataBind()? where can I put this code?
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.
how about the Name.Text = Request.QueryString["Text"];? how can I use this?
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.
so I just put Name.Text = Request.QueryString["Text"]; in the function Page_Load(), that will work right?
|
0

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.

3 Comments

that's coooooool! so what should I write in the <TextBox>?
@bowang nothing - that's the beauty in this.
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)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.