0

I have defined a variable in C# as the item selected in a drop down.

string parametername = ddlCarrier.SelectedItem.Text;

I now want to pass this variable in my URL to the next page. How do I do this in the href tag?

<asp:LinkButton href="Table.aspx?parameter=<%parametername%>"  ID="btnSubmit" runat="server">Click Here</asp:LinkButton>
2
  • this is what you need to do stackoverflow.com/questions/10172567/linkbutton-href-url Commented Jun 2, 2016 at 20:54
  • parametername is rather misleading as a variable name, as it's actually the parameter itself, not the name. The parameter name is actually just "parameter". It's really the parameter VALUE. Also, you may have to URL encode the string, if you're going to pass it via that url, depending on the nature of the values in the drop down. Commented Jun 2, 2016 at 21:01

2 Answers 2

2

Purely Server-Side Approach

Instead of a LinkButton, you might want to consider using a HyperLink or <a> tag as you aren't going to be doing anything with your code-behind:

<asp:HyperLink ID="btnSubmit" runat="server" NavigateUrl="Table.aspx" Text="Navigate"></asp:HyperLink>

Then you can use the NavigateUrl property, which you might want to consider setting within your code-behind :

// This will set up your Navigation URL as expected
btnSubmit.NavigateUrl = String.Format("Table.aspx?parameter={0}",ddlCarrier.SelectedItem.Text);

If you use this approach, you may want to explicitly set that a PostBack occurs when your DropDownList changes so that this value will consistently be correct :

<asp:DropDownList ID="dllCarrier" runat="server" AutoPostBack="True" ...>

Client-Side Approach

However, if you are expecting to be able to change this to reflect the current value of your Carrier DropDownList without a PostBack, then you'll likely need to resort to Javascript to populate the value prior to actually navigating :

<!-- Set your base URL within the method and append the selected value when clicked -->
<asp:Button ID="Example" runat="server" OnClientClick="ClientSideNavigate('Table.aspx'); return false;" Text="Navigate"></asp:Button>
<script>
    function ClientSideNavigate(url) {
        // Get the selected element
        var e = document.getElementById('<%= ddlCarrier.ClientID %>');
        // Navigate
        window.location.href = url + '?parameter=' + e.options[e.selectedIndex].value;
    }
</script>

Or you could just avoid ASP.NET Controls altogether and just use an <button> tag :

<button onclick="ClientSideNavigate('Table.aspx'); return false;">Navigate</button>
<script>
    function ClientSideNavigate(url) {
        // Get the selected element
        var e = document.getElementById('<%= ddlCarrier.ClientID %>');
        // Navigate
        window.location.href = url + '?parameter=' + e.options[e.selectedIndex].value;
    }
</script>
Sign up to request clarification or add additional context in comments.

7 Comments

Where in C# do I place this code 'btnSubmit.NavigateUrl = String' Do I place it within protected void btnSubmit_Click(object sender, EventArgs e)?
It depends on which approach you want to use. The first approach would be placed within your Page_Load method of your ASPX page and the second approach (client-side) would be all within your ASPX page.
For the first approach I am not getting .NavigateURL as a method. (Sorry if wrong terminology)
Also, which approach would you suggest?
I've revised this to use a HyperLink Control instead, which is going to be more appropriate for this type of scenario. If you explicitly need it to be a Button, I'll be glad to provide that approach as well.
|
0

You need to handle TextChanged or SelectedIndexChanged event for ddlCarrier and properly set href property of btnSubmit to include ddlCarrier.Text.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.