Reputation: 365
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?
Upvotes: 11
Views: 56575
Reputation: 66389
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.
Upvotes: 0
Reputation: 3826
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.
Upvotes: 21
Reputation: 11964
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.
Upvotes: 5
Reputation: 2344
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&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"];
Upvotes: 0