We help IT Professionals succeed at work.

Pass Query String Variables from Form

I am creating an aspx page in sharepoint which needs to link to other pages that accept Query String variables. Those pages are ready to go.

I need to create a list of links which accept values from a form text box and pass to the next page.

For eg..

<script language="javascript" type="text/javascript">
private void menuredirect(object sender, System.EventArgs e)
{
    // Value sent using HttpResponse
    Response.Redirect("menus.aspx?sid="+RadioList.Value+"&date="+datetxt.Text);
}
</script>

<asp:TextBox runat="server"  id="datetxt"></asp:TextBox>

<asp:RadioButtonList runat="server" id="RadioList">
      <asp:ListItem Value="1019629">Alumni Cafe</asp:ListItem>
</asp:RadioButtonList>

<asp:Button runat="server" Text="Button" id="Button1" OnClientClick="menuredirect()"/>

This is not working. I am not sure what I am doing wrong. When I click it does not redirect to the next page.
Comment
Watch Question

Commented:
There are a few things wrong with your javascript there.  Firstly, you're making your function call incorrectly.  Notice that in your OnClientClick you call menuredirect() however, menuredirect has a signature that requires two parameters.  The parameters are not necessary in javascript, so start by removing those.

Your next problem is the way you're referencing the RadioButtonList.  Firstly, the javascript won't know what it is since you're using the ASP.NET ID rather than the ClientID.  Next, a RadioButtonList is rendered as a span with several radiobuttons inside of it.  As such the RadioList.Value will return "undefined".  You would have to modify your javascript to loop through each child element of RadioList (this will contain both objects of type input (radio buttons) and span (the labels) until you find the radiobutton with its "checked" property set to true and use the value of that for the redirect.

In the end you will need something like this:

<script language="javascript" type="text/javascript">
private void menuredirect()
{
    var rbList = document.getElementById('RadioList');
    for (i = 0; i < rbList.childNodes.length; i++)
    {
        if (rbList.childNodes[i].checked == true)
        {
             Response.Redirect("menus.aspx?sid="+rbList.childNodes[i].value+"&date="+datetxt.Text);
             // You may also want to add a "break;" here to stop it from continuing to loop through the rest.
        }
    }
}
</script>

<asp:TextBox runat="server"  id="datetxt"></asp:TextBox>

<asp:RadioButtonList runat="server" id="RadioList">
      <asp:ListItem Value="1019629">Alumni Cafe</asp:ListItem>
</asp:RadioButtonList>

<asp:Button runat="server" Text="Button" id="Button1" OnClientClick="menuredirect()"/>

Author

Commented:
Hey, Bane. Thanks for the input. I have corrected my code, however the result is still the same. When I click the button it looks like it redirects me to the same page. I see an error box on the bottom left corner of IE but it goes away.  Any thoughts? I am doing this within a sharepoint aspx page.
Commented:
Sorry, that was careless of me.  I missed the datetxt.Text part of your code and did a small mistake on the code to retrieve a javascript handle to the control.  Please revise this to be:

<script language="javascript" type="text/javascript">
private void menuredirect()
{
    var rbList = document.getElementById('<% =RadioList.ClientID %>');
    var datetxt = document.getElementById('<% =datetxt.ClientID %>');

    for (i = 0; i < rbList.childNodes.length; i++)
    {
        if (rbList.childNodes[i].checked == true)
        {
             Response.Redirect("menus.aspx?sid="+rbList.childNodes[i].value+"&date="+datetxt.value);
             // You may also want to add a "break;" here to stop it from continuing to loop through the rest.
        }
    }
}
</script>

If you still receive errors, it is also possible to debug javascript in IE (provided you have something installed to do this such as Visual Studio) by first (in IE) going to Tools -> Internet Options.  In the popup choose the Advanced tab and then under "Browsing" uncheck the 2nd and 3rd item ("Disable script debugging).  Once you do that, you can add a "debugger;" statement to the beginning of your javascript:

private void menuredirect()
{
    debugger;
    var rbList = document.getElementById('<% =RadioList.ClientID %>');

This will allow you to step through the code line by line and use the IDE to view the variables and any possible errors.
I'm pretty sure SharePoint doesn't let you use <% %>

Author

Commented:
yes it doesnt. and thats the problem. how do i bind my link to a form control?

Commented:
My apologies, I've never used Sharepoint.  Although I'm not sure this is the best way to do it in SharePoint, you could view the source of the rendered page and get the ID of the controls you require.

You would then use
var ctrl = document.getElementById('controlID');

Explore More ContentExplore courses, solutions, and other research materials related to this topic.