PostBackUrl and Retrieving Values from User Controls

O

osh

All,

I am having trouble using the PostBackUrl feature in .NET 2.0. I have
several User Controls that are on a MasterPage. The User Controls
contain the form fields for a basic search on available homes and
properties in a given area. I put these form fields into User Controls
because they will be repeated in different ways throughout the site and
it would be nice to only update the fields from one place to make a
site wide change.

The problem I am running into is that when I click my search button
which has a PostBackUrl field defined to the search results page, on
the search results page, I cant get a reference to the controls. I keep
getting a NullReference exception. For example:

====== search.aspx ======
<%@ Page Language="C#" MasterPageFile="MyMasterPage.master"
AutoEventWireup="true" CodeFile="search.aspx.cs" Inherits="search" %>

<%@ Register Src="~/path/formFieldMinPrice.ascx"
TagName="formFieldMinPrice" TagPrefix="uc1" %>
....
<uc1:formFieldMinPrice ID="ctrlMinPrice" runat="server" />
....
<asp:Button id="btnSubmit" runat="server"
PostBackUrl="~/path/results.aspx" />

====== formFieldMinPrice.ascx ======
<%@ Control Language="C#" AutoEventWireup="true"
CodeFile="formFieldMinPrice.ascx.cs" Inherits="formFieldMinPrice" %>

<asp:DropDownList ID="ddlMinimumPrice" runat="server" Width="80px">
<asp:ListItem Value="0">No Min</asp:ListItem>
<asp:ListItem>$1</asp:ListItem>
<asp:ListItem>$2</asp:ListItem>
<asp:ListItem>$3</asp:ListItem>
<asp:ListItem>$4</asp:ListItem>
<asp:ListItem>$5</asp:ListItem>
..... (more list items)
</asp:DropDownList>

====== results.aspx ======
if (PreviousPage != null && PreviousPage.IsValid)
{
DropDownList ddlMinPrice =
PreviousPage.FindControl("ctrlMinPrice").FindControl("ddlMinimumPrice")
as DropDownList;

... (more code)
}

On the above line in the results.aspx page, I get my
"System.NullReferenceException: Object reference not set to an instance
of an object." I have also tried:

====== results.aspx ======
if (PreviousPage != null && PreviousPage.IsValid)
{
UserControl ucMinPrice = PreviousPage.FindControl("ctrlMinPrice")
as UserControl;
DropDownList ddlMinPrice =
ucMinPrice.FindControl("ddlMinimumPrice") as DropDownList;
... (more code)
}

In which case the same error occurs on the line that I declare the
DropDownList on. If I remove the DropDownList from a User Control,
adjust the code accordingly, and then try it - it works just fine. If
this is not even possible to do, then I can settle with having to
update the drop down list all across the site but I would hope this is
possible. Any insight anyone can give to this or any other suggestions
would be greatly appreciated. Thanks in advance!
 
D

davidgallucci

It is because there is no previous page when doing the search from the
current page. All you need to do is change your call from
PreviousPage.Master to Page.Master to find your control.

Hope this helps,

Dave Gallucci
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,023
Latest member
websitedesig25

Latest Threads

Top