Search code examples
c#javascriptquery-stringcode-behind

html accessing variable value from c# codebehind


I'm trying to access a value from the code behind to create a query string. This is the simplest way I thought of, but if you guys have any recommendations please feel free to give your thoughts:

I have this link that would generate the querystring and would trigger a lightbox to show the page in front of the main page:

<a id="link" runat="server" href="thepage.aspx?id=<%strtest%>">Show the page with strtest</a>

Code behind:

public string strtest = "";
:
string strTestID = Request.QueryString["ID"].ToString();
:
strtest = strTestID ;

At the moment, it would just give me a blank lightbox. If I take out the lightbox and see the url generated, the url looks like this.

thepage.aspx?id=<%strtest%>

So like I said, I did the simplest way I can think of. I also thought of implementing javascript with this

string jsString= "changeLink('" + strtest  + "');";

But that would give me an error with my parameters which btw is weird as it works without it.

So yeah, any help is appreciated.


Solution

  • You are seeing this behavior because of the 'runat="server"' attribute on the anchor tag. Remove the attribute and the value will be displayed correctly. Also, you will want to change the code from <%strtest%> to <%= strtest%>