Welcome to WebmasterWorld Guest from 54.198.92.22

Forum Moderators: ocean10000

Message Too Old, No Replies

Variable from usercontrol works in .ascx but not .ascx.cs - any ideas?

     
3:53 pm on Aug 21, 2009 (gmt 0)

New User

5+ Year Member

joined:June 17, 2009
posts: 28
votes: 0


Hello everyone,

I have an annoying bug, I have built one usercontrol called BlogPost.ascx and one called BlogPostWithComments.ascx which loads the BlogPost one and then adds some extra features to the output.

If I refer to a variable in the .ascx file (i.e. BlogPost1.BlogId) it works fine, but if I try to use this in the class file, despite it coming up in the Visual Studion intellisense it returns a null value.

BlogPostWithComments.ascx


<%@ Control Language="C#" AutoEventWireup="true" CodeFile="BlogPostWithComments.ascx.cs" Inherits="managed_content_usercontrols_BlogPostWithComments" %>
<%@ Register src="BlogPost.ascx" tagname="BlogPost" tagprefix="uc1" %>
<uc1:BlogPost ID="BlogPost1" runat="server" />
<%=BlogPost1.BlogId %>

BlogPostWithComments.ascx.cs


using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using System.IO;
using System.Web;
using System.Web.Caching;
using System.Xml;
using System.Xml.Xsl;
using System.Xml.XPath;
//
public partial class managed_content_usercontrols_BlogPostWithComments : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
Response.Write("[" + BlogPost1.BlogId + "," + BlogPost1.PostAuthor + ",");
}
}

Does anyone have any idea why? The <%=BlogPost1.BlogId%> outputs fine.

Thanks.

6:42 pm on Aug 21, 2009 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Feb 1, 2005
posts:732
votes: 0


sorry, I am not at a development PC right now, so I can't test anything. Could you try the following?

protected void Page_PreRender(object sender, EventArgs e)
{
Response.Write("[" + BlogPost1.BlogId + "," + BlogPost1.PostAuthor + ",");
}
7:22 pm on Aug 21, 2009 (gmt 0)

New User

5+ Year Member

joined:Aug 21, 2009
posts: 15
votes: 0


I've not worked too much with User Controls like this. But, that won't stop me from tossing my $0.02 in ...

What happens when you try to response.write BlogPost1.BlogId.ToString() and BlogPost1.PostAuthor.ToString() ?

12:48 am on Aug 22, 2009 (gmt 0)

Administrator

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month

joined:Jan 14, 2004
posts:864
votes: 3


I am assuming BlogPost1.BlogId is an int or some other unquie value which is not a string value. I am guessing the same thing for the PostAuthor property as well that it is an object and not a string.

<%=BlogPost1.BlogId %> will automaticly convert this to
Response.Write(BlogPost1.BlogId.ToString());

Example Below
<%@ Control Language="C#" AutoEventWireup="true" %>
<table border="1">
<tr><td><%=BlogID%></td><td><%=PostAuthor%></td></tr>
</table>
<script runat="Server" language="C#">

private Guid _BlogID = Guid.NewGuid();
public Guid BlogID
{
get
{
return _BlogID;
}
set
{
_BlogID = value;
}

}
private object _PostAuthor = new object();
public object PostAuthor
{
get
{
return _PostAuthor;
}
set
{
_PostAuthor = value;
}
}

protected void Page_Load(object sender, EventArgs e)
{
Response.Write("[" + this.BlogID.ToString() + "," + this.PostAuthor.ToString() + "]");
}
</script>

7:30 am on Aug 26, 2009 (gmt 0)

New User

5+ Year Member

joined:June 17, 2009
posts:28
votes: 0


Thanks everyone, sorry for the delay in replying - I got stuck on another project.

I've tried Marcel's suggestion and bizarrely while the text in the Page_PreRender doesn't work the text in the Page_Load does.

So adding the code above generates the text:
[,,[173,Mr Blogger,

Ocean10000, I'm having a look at your suggestion now.

7:55 am on Aug 26, 2009 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Feb 1, 2005
posts:732
votes: 0


Page_PreRender comes after Page_Load in the Page Lifecycle [msdn.microsoft.com], so the result you have shown is what I would have expected after adding the PreRender code.

What's most likely happening is that you are trying to get the values from BlogPost in the Page_Load event before the UserControl BlogPost itself has loaded. By moving the code to PreRender, BlogPost has already loaded and you are inserting the values just before it is being rendered to HTML.

9:32 am on Aug 26, 2009 (gmt 0)

New User

5+ Year Member

joined:June 17, 2009
posts:28
votes: 0


Ah - so simple! I shall have to swot up on the Page Lifecycle.

Thanks Marcel.