|
|
 |
BOOK: Professional ASP.NET 3.5 : in C# and VB ISBN: 978-0-470-18757-9
 | This is the forum to discuss the Wrox book Professional ASP.NET 3.5: In C# and VB by Bill Evjen, Scott Hanselman, Devin Rader; ISBN: 9780470187579 |
|
Welcome to the p2p.wrox.com Forums.
You are currently viewing the BOOK: Professional ASP.NET 3.5 : in C# and VB ISBN: 978-0-470-18757-9 section of the Wrox p2p Programmer to Programmer discussion community. This is a community of more than 40,000 computer programmers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining our free Wrox p2p community you can post your own programming questions and respond to other programmers’ questions. Registered users also don't have to see the ads that are displayed to guests. Registration is fast, simple and absolutely free so please, join today!
Join today and post to win prizes! Post more to increase your chances of being Wrox’s top poster of the month.
|
 |

September 6th, 2009, 05:43 AM
|
|
Authorized User
|
|
Join Date: Apr 2009
Posts: 50
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Cap 1 pg 33 Oject reference not set to an instance of an object
Hello All,
I am getting an error "Object reference not set to an instance of an object"
As far as I can see everything is declared as a variable but there seems to be something not right about how the two pages are interacting because there is no red underlined errors in Visual Web Developer shoping that it can't see that the variable has been declared.
The error refers to TextBox1.
My page1.aspx file looks like this
Code:
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<script runat="server">
public TextBox pp_TextBox1
{
get
{
return TextBox1;
}
}
public Calendar pp_Calendar1
{
get
{
return Calendar1;
}
}
protected void Button1_Click (object sender, System.EventArgs e)
{
Label1.Text = "Hello " + TextBox1.Text + "<br />" +
"Date Selected: " + Calendar1.SelectedDate.ToShortDateString();
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>First Page</title>
</head>
<body>
<form id="form1" runat="server">
Enter your name:<br />
<asp:Textbox ID="TextBox1" Runat="server">
</asp:Textbox>
<p>
When do you want to fly?<br />
<asp:Calendar ID="Calendar1" Runat="server"></asp:Calendar></p>
<br />
<asp:Button ID="Button1" Runat="server" Text="Submit page to itself"
OnClick="Button1_Click" />
<asp:Button ID="Button2" Runat="server" Text="Submit page to Page2.aspx"
PostBackUrl="~/Page2.aspx" />
<p>
<asp:Label ID="Label1" Runat="server"></asp:Label></p>
</form>
</body>
</html>
My page2.aspx looks like this.
Code:
<%@ Page Language="C#" %>
<%@ PreviousPageType VirtualPath="~/Page1.aspx" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<script runat="server">
protected void Page_Load(object sender, System.EventArgs e)
{
TextBox pp_Textbox1;
Calendar pp_Calendar1;
pp_Textbox1 = (TextBox)PreviousPage.FindControl("TextBox1");
pp_Calendar1 = (Calendar)PreviousPage.FindControl("Calendar1");
if (PreviousPage != null && PreviousPage.IsCrossPagePostBack)
{
Label1.Text = "Hello " + PreviousPage.pp_TextBox1.Text + "<br />" + "Date Selected: " +
PreviousPage.pp_Calendar1.SelectedDate.ToShortDateString();
}
else
{
Response.Redirect("Page1.aspx");
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>Second Page</title>
</head>
<body>
<form id="form1" runat="server">
<asp:Label ID="Label1" Runat="server"></asp:Label>
</form>
</body>
</html>
My Page2.cs file looks like this
Help....
Cheers
Tomche
|

September 6th, 2009, 08:39 AM
|
 |
Wrox Author
Points: 33,554, Level: 80 |
|
|
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 10,228
Thanks: 7
Thanked 203 Times in 201 Posts
|
|
Quote:
|
My Page2.cs file looks like this
|
That's not a lot.... ;-)
Your other code looks fine, so maybe the problem is in that file?
However, Page2 looks like a file with in-line code, so how / where do you have this code file exactly?
Cheers,
Imar
Last edited by Imar : September 7th, 2009 at 04:07 PM.
|

September 9th, 2009, 10:04 AM
|
|
Authorized User
|
|
Join Date: Apr 2009
Posts: 50
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Imar,
I reckon the problem here is that I am trying to open Page2.aspx.
This code is only meant to reference the previous page so if you open it directly in Internet Explorer it doesn't have a previous page to reference therefore it produces an object reference error because there is no previous page for an object.
Doesn't one have to open Page1.aspx first?
Does this sound logical???
Cheers
Tomche
|

September 9th, 2009, 10:45 AM
|
 |
Wrox Author
Points: 33,554, Level: 80 |
|
|
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 10,228
Thanks: 7
Thanked 203 Times in 201 Posts
|
|
Take another look at this:
Code:
TextBox pp_Textbox1;
Calendar pp_Calendar1;
pp_Textbox1 = (TextBox)PreviousPage.FindControl("TextBox1");
pp_Calendar1 = (Calendar)PreviousPage.FindControl("Calendar1");
if (PreviousPage != null && PreviousPage.IsCrossPagePostBack)
{
Label1.Text = "Hello " + PreviousPage.pp_TextBox1.Text + "<br />" + "Date Selected: "
+ PreviousPage.pp_Calendar1.SelectedDate.ToShortDateString();
}
else
{
Response.Redirect("Page1.aspx");
}
You are protecting yourself from directly requesting Page2.apsx. If PreviousPage is null you're redirecting to Page1.aspx. However, before you do the null check, you're already trying to access the PreviousPage instance with this code:
Code:
pp_Textbox1 = (TextBox)PreviousPage.FindControl("TextBox1");
pp_Calendar1 = (Calendar)PreviousPage.FindControl("Calendar1");
Simply move that to inside the if check and you should be fine...
Hope this helps,
Imar
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
 |