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!

4

1 回答 1

2

对于搜索页面,我建议使用QueryString将信息传递到您以后的页面,而不是尝试引用前一页中的控件。

如果您想从不同的技术中使用此功能,这将特别有用。您不必担心请求来自何处。

搜索页面.aspx:

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

Response.Redirect(url);

结果详细信息.aspx:

var from = DateTime.Parse(Request.QueryString["TBPostDateFrom"]);
var to = DateTime.Parse(Request.QueryString["TBPostDateTo"]);

//Do search based on parameters

有关详细信息:MSDN - 如何:在 ASP.NET 网页之间传递值

于 2013-09-18T18:38:36.733 回答