0

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!

russds
  • 845
  • 5
  • 25
  • 48
  • 1
    So you are requesting controls from one page (that does not presently exist at the time of the request) on a separate page? Assuming I am correct, why? – Mike H. Sep 18 '13 at 18:27
  • Well, I'm just trying to create a simple search feature. Enter some parameters (from date and to date) hit go button, and on the next page, show a grideview displaying the results based on the inputs from the previous page. – russds Sep 18 '13 at 18:30
  • Considered using a querystring? – Daniel Sep 18 '13 at 18:34
  • Thanks, yes, considered it. The issue I had was that there are actually lots of inputs (i just listed two as an example). So I was hoping to not have to add each one individually, but instead gather all Request vars from the target page. – russds Sep 18 '13 at 19:00

1 Answers1

2

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

Khan
  • 17,904
  • 5
  • 47
  • 59
  • 1
    Thanks, although not a direct answer, this I believe is what I was looking for, and appears to be the best way to do this. Thanks! – russds Sep 18 '13 at 20:35