Forum Moderators: open

Message Too Old, No Replies

Using variable in .aspx.cs in .aspx file

         

katebp

11:24 am on Jun 19, 2009 (gmt 0)

10+ Year Member



This should be such a simple thing, but I just don't seem to be able to do it.

I have a bit of code in my master page cs file that assigned a value to "pageid".

In the master page I am loading in a handler for CSS:


<link href="/dotnettemplate/style/style.ashx?pageid=" rel="stylesheet" type="text/css" />

I have tried editing it to:


<link href="/dotnettemplate/style/style.ashx?pageid=<%=pageid%>" rel="stylesheet" type="text/css" />

(like I would do for classic ASP), but it doesn't convert it.

The code in the master.cs file that assigns the value to the pageid is below:


protected void Page_Load(object sender, EventArgs e)
{
// get pageid
string PagePath = "~" + Request.ServerVariables["URL"];
string PageType = Request.QueryString["filetype"];
//
XElement el = XElement.Load(Server.MapPath("~/xml/dotnettemplate.sitemap"));
//
var node = (from sitemap in el.Descendants()
where sitemap.Attribute("url").Value == PagePath
select sitemap.Attribute("pageid").Value).SingleOrDefault();
//
string pageid = string.Format("{0}", node);
Response.Write(pageid);
}

It def works (the response.write executes fine) and even if I change it to string pageid = "1"; it still doesn't work.

Any ideas?

Thanks, Kx

marcel

12:08 pm on Jun 19, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm not sure why exactly your code doesn't or won't work, I usually go about achieving this in a different way. Here is my preferred method:

replace

<link href="/dotnettemplate/style/style.ashx?pageid=<%=pageid%>" rel="stylesheet" type="text/css" />

with a literal control
<asp:Literal ID="litStylesheet" runat="server" />

and then replace

Response.Write(pageid);

with this, which will fill the text property of the literal control
litStylesheet.Text = String.Format("<link href='/dotnettemplate/style/style.ashx?pageid={0}' rel='stylesheet' type='text/css' />", pageid);

Also, for a simple string conversion, you do not need to use a String.Format, the following will suffice:

string pageid = node.ToString();

or better yet, convert the LINQ query:
string pageid = (from sitemap in el.Descendants()
where sitemap.Attribute("url").Value == PagePath
select sitemap.Attribute("pageid").Value).SingleOrDefault().ToString();

katebp

1:01 pm on Jun 19, 2009 (gmt 0)

10+ Year Member



That's brilliant Marcel, thanks for that.

katebp

3:57 pm on Jun 19, 2009 (gmt 0)

10+ Year Member



Aha - I found out why the first bit of code wasn't working. I had set the variable inside the Page_Load event - once I moved

public string pageid;

above the Page_Load event (even though that was where the value was assigned) I could use it in the .aspx file.

Thanks for your help tho, I am going to use the method you suggested for loading in the style.ashx

marcel

7:56 pm on Jun 19, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



oh, I should have thought of that... but I prefer not to use public variable if possible :)