ASP.NET - Cross-Page Posting

Asked By matt cupryk on 06-Jun-11 04:15 PM

Cross-Page Posting

a few seconds ago

I have the following:

ctrlSearch.ascx 

<asp:Button CssClass="button-search" ID="imgSearch" runat="server"
                  PostBackUrl="~/Secure/SearchResults.aspx" Font-Bold="True" />

 -----------------------------------------------------------------

<%@ Page Title="Search Results" Language="C#" MasterPageFile="~/MasterPages/OneColumn.master" AutoEventWireup="true"  
    CodeBehind="SearchResults.aspx.cs" Inherits="OmegaLove.Web.UI.Pages.SearchResults" %> 
<%@ Reference  VirtualPath="~/Controls/Search/ctrlSearch.ascx" %>
 
in the behind code of searchresults.aspx
 if ((Page.PreviousPage != null) && Page.PreviousPage.IsCrossPagePostBack) 
            { 
                UserControl ucSearch = PreviousPage.FindControl("ctrlSearch") as UserControl; 
                DropDownList ddlAge1 = new DropDownList(); 
                ddlAge1 = (DropDownList) ucSearch.FindControl("search_age_start"); 
                lblSentFromPreviousPage.Text = ddlAge1.SelectedValue; 
            }
Object reference not set to an instance of an object.  
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.  
 
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object. 
 
Source Error:  
 
 
Line 33:                 UserControl ucSearch = PreviousPage.FindControl("ctrlSearch") as UserControl; 
Line 34:                 DropDownList ddlAge1 = new DropDownList(); 
Line 35:                 ddlAge1 = (DropDownList) ucSearch.FindControl("search_age_start"); 
Line 36:                 lblSentFromPreviousPage.Text = ddlAge1.SelectedValue; 
Line 37:             } 
 


Peter Bromberg replied to matt cupryk on 06-Jun-11 04:37 PM
Unfortunately you haven't given us a line number from the exception stacktrace.

So it's either this line:

   UserControl ucSearch = PreviousPage.FindControl("ctrlSearch") as UserControl; 


or this line:
   ddlAge1 = (DropDownList) ucSearch.FindControl("search_age_start"); 

that aren't getting what you think they're supposed to be getting.
matt cupryk replied to Peter Bromberg on 06-Jun-11 04:43 PM
ddlAge1 = (DropDownList) ucSearch.FindControl("search_age_start"); 

is the line that fails.
Peter Bromberg replied to matt cupryk on 06-Jun-11 04:55 PM
Well does it throw an exception because ucSearch is null, or because it can't find "search_age_start") ?
You have to know the answer in order to correctly solve your problem.

For example, if ucSearch is nested in another control, then Page.FindControl may not see it. You might have to do 

Page.FindControl("containercontrol").FindControl("ucsearch")  etc.

matt cupryk replied to Peter Bromberg on 06-Jun-11 05:07 PM
Can u help me get started.

<%@ Master Language="C#" MasterPageFile="~/MasterPages/Root.Master" AutoEventWireup="true"

CodeBehind="TwoColumn.master.cs" Inherits="OmegaLove.Web.MasterPages.TwoColumn" %>

<%@ Register Src="../Controls/LeftMenu/ctrlMembers.ascx" TagName="ctrlMembers" TagPrefix="uc1" %>

<asp:Content ID="Content1" ContentPlaceHolderID="cph1" runat="server">

<div class="master-wrapper-side">

<asp:ContentPlaceHolder ID="cph2" runat="server">

<OmegaLove:ctrlSearch ID="ctrlSearch" runat="server" />

<div class="clear">

</div>

</asp:ContentPlaceHolder>

Peter Bromberg replied to matt cupryk on 06-Jun-11 05:20 PM
You'll have to experiment. But this will give you an idea:

ContentPlaceHolder cph = otherPage.Form.FindControl("ContentPlaceHolder1") as ContentPlaceHolder;

TextBox ctrlInput = (TextBox)cph.FindControl("mycontrol");
Ravi S replied to matt cupryk on 06-Jun-11 10:04 PM
HI

Cross page posting is submitting the form to a different page. This is usually required when you are creating a multi page form to collect information from the user on each page. When moving from the source to the target page, the values of controls in the source page can be accessed in the target page.
To use cross-page posting, you have to use the PostBackURL attribute to specify the page you want to post to.
 

refer the links for examples
http://www.dotnetcurry.com/ShowArticle.aspx?ID=71
http://csharpdotnetfreak.blogspot.com/2009/08/cross-page-posting-in-aspnet.html
Jitendra Faye replied to matt cupryk on 07-Jun-11 12:30 AM
You are getting error because of these lines-

         UserControl ucSearch = PreviousPage.FindControl("ctrlSearch") as UserControl; 
               
DropDownList ddlAge1 = new DropDownList(); 
                ddlAge1
= (DropDownList) ucSearch.FindControl("search_age_start"); 
         lblSentFromPreviousPage.Text = ddlAge1.SelectedValue; 

Change your code like this-

         UserControl ucSearch = PreviousPage.FindControl("ctrlSearch") as UserControl; 
               
DropDownList ddlAge1 = new DropDownList(); 
         
          if(ucSearch !=null)
           {
               ddlAge1
= (DropDownList) ucSearch.FindControl("search_age_start"); 
               if(ddlAge1 !=null)
             {
               lblSentFromPreviousPage.Text = ddlAge1.SelectedValue; 
              }

           } 

Try and let me know,
        
matt cupryk replied to matt cupryk on 07-Jun-11 12:01 PM
<%@ Page Title="Search Results" Language="C#" MasterPageFile="~/MasterPages/OneColumn.master" AutoEventWireup="true" 
  CodeBehind="SearchResults.aspx.cs" Inherits="OmegaLove.Web.UI.Pages.SearchResults" %>
<%@ Reference VirtualPath="../Controls/Search/ctrlSearch.ascx" %>
 
I tried the below code. I get null.

public partial class SearchResults : OmegaLoveBasePage
   {
     public static string ConnnectionString = ConfigurationManager.ConnectionStrings["omegaloveConnectionString"].ToString();
     private int counter = 0;
   
     protected void Page_Load(object sender, EventArgs e)
     {
       UserControl ucSearch = PreviousPage.FindControl("ctrlSearch") as UserControl;
       DropDownList ddlAge1 = new DropDownList();
       if (ucSearch != null)
       {

Asked By matt cupryk on 07-Jun-11 12:46 PM
Hi Kumar

I have TwoColumn master page -> search.ascx and search.ascx has a button that calls
searchresults.aspx.

This is my situation.
matt cupryk replied to Jitendra Faye on 07-Jun-11 02:44 PM
Gentleman,
please see
http://www.c-sharpcorner.com/UploadFile/Santhi.M/PassingValuesfrmUCtoASPX11212005050040AM/PassingValuesfrmUCtoASPX.aspx

I still have issues.