asp.netpostbackurl

using postbackurl to pass variables without referencing ctl00$MainContent


I'm hoping there's a cleaner way of doing this. My source page markup has some simple inputs and a submit button:

<asp:TextBox runat="server" ID="TBPostDateFrom" placeholder="From" />
<asp:TextBox runat="server" ID="TBPostDateTo" placeholder="Present" />
...
<asp:Button ID="BtnDetailedResults" PostBackUrl="~/Auth/ResultsDetail.aspx" runat="server" Text="View Detailed Results" />

On my target page, I'm trying to reference those controls and use them as datasource select parameters. So far the only way I've found to do that is to use the long asp generated names "ctl00$MainContent$TBPostDateFrom" and "ctl00$MainContent$TBPostDateTo":

SDSDetailedResults.SelectParameters.Add("PDFrom", Request.Form["ctl00$MainContent$TBPostDateFrom"]);
SDSDetailedResults.SelectParameters.Add("PDTo", Request.Form["ctl00$MainContent$TBPostDateTo"]);

Is there a way I can reference those controls without using the long ct100$...? Or a way to reference the controls directly? I'm guessing if sometime down the road I change my master page, or content controls, these references would get messed up.

I've tried adding using adding the ClientIDMode=Static to the inputs like:

<asp:TextBox runat="server" ID="TBPostDateFrom" placeholder="From" ClientIDMode="Static" />

But that appears to only change the ID. On my target page, I'm still unable to reference it without using the ct100$....

I've also tried using the Page.PreviousPage method, but the objects end up empty:

if (Page.PreviousPage != null)
        {
            //post date
            TextBox PostDateFrom = (TextBox)Page.PreviousPage.FindControl("TBPostDateFrom");
            TextBox PostDateTo = (TextBox)Page.PreviousPage.FindControl("TBPostDateTo");

            //at this point both PostDateFrom and PostDateTo are empty, if I do this:
            SDSDetailedResults.SelectParameters.Add("PostDateFrom", PostDateFrom.Text);
            SDSDetailedResults.SelectParameters.Add("PostDateTo", PostDateTo.Text);
            //  I get an IIS error saying the object references dont' exist, or are null
            }
        }

Thanks in advance, any help or guidance is much appreciated!


Solution

  • For a search page, I would recommend using the QueryString to pass information to your later page, rather than trying to reference the controls from the previous page.

    This will be especially useful if you want to use this functionality from different technologies. You won't have to worry about where the request came from.

    SearchPage.aspx:

    //Button Click:
    var page = "ResultsDetail.aspx";
    var url = String.Format("{0}?TBPostDateFrom={1}&TBPostDateTo={2}", page, TBPostDateFrom.Text, TBPostDateTo.Text);
    
    Response.Redirect(url);
    

    ResultDetails.aspx:

    var from = DateTime.Parse(Request.QueryString["TBPostDateFrom"]);
    var to = DateTime.Parse(Request.QueryString["TBPostDateTo"]);
    
    //Do search based on parameters
    

    For more information: MSDN - How to: Pass Values Between ASP.NET Web Pages