Question

I have an ASP.Net page with a GridView. In one of the GridView cells there's a HyperLink control and its NavigateURL property is set like so:

NavigateUrl='<%# "~/telecom/SmartPhoneInventory.aspx?IMEI=" +  Eval("IMEI") %>'

There's a RadioButtonList (rblDeviceType) on this page (not in the GridView) with four values. I want to add another querystring to the HyperLink's NavigateURL so that:

NavigateUrl='<%# "~/telecom/SmartPhoneInventory.aspx?IMEI=" +  Eval("IMEI") + "&devicetype=" + rblDeviceType.SelectedValue %>'

This is of course not correct syntax. Is there a way to do this?

Was it helpful?

Solution

Try this:

In your html

<a href='<%= string.Format("~/telecom/SmartPhoneInventory.aspx?IMEI={0}&devicetype=", this.someValue, rblDeviceType.SelectedValue) %>'>
        Hello World
    </a>

or in your html:

<asp:HyperLink runat="server"
        NavigateUrl='' ID="demoLink">
            Hello World
        </asp:HyperLink>

and then in your codebehind:

demoLink.NavigateUrl= string.Format("~/telecom/SmartPhoneInventory.aspx?IMEI={0}&devicetype=",this.someValue,rblDeviceType.SelectedValue)

Regarding

'someValue'

Which you present as Eval("IMEI") in your sample code since your code is not part of the Grid you will need to get this from either a control directly, session, viewstate or server side variable. Your code sample does not allow me to understand where is the original source of this value.

Try this in your code behind:

public partial class _Default : Page
{
    public string someValue = "Hello World";

Using string.Format and <%= instead of <%#

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top