Hi Everyone, I wonder if anyone can help me with this problem. I have set up a page called "Default.aspx" that contains a search text box called txtName and a button. I want to post the results of the text search on a another page called searchResults. The results are based on a search by potential students for courses. However when I try to access the page I get an error thrown up as such.
Could not find control 'txtName' in ControlParameter 'Course'.
The page will work perfectly if I post it back to itself but when I post to another page the above error appears. This leads me to suspect that the page is not remembering a value for txtName on the previous. I have tried various incarnations of retrieving the textbox value but to no avail.
I have imported
Code:
<%@ PreviousPageType VirtualPath="~/Default.aspx" %>
and used it with
Code:
public void Page_Load(object sender, EventArgs e)
{
if (PreviousPage != null && PreviousPage.IsCrossPagePostBack)
{
TextBox txtName = (TextBox)Page.PreviousPage.FindControl("txtName");
GridView1.Visible = true;
}
}
Nothing seems to work and this one error has gridlocked me at work for 2 days now. Anyways here is a full listing of the code.
default.aspx
Code:
<%@ Register TagPrefix="BIE" TagName="header" Src="header.ascx" %>
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
<link rel="stylesheet" href="stylecss.css" type="text/css" media="screen">
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="txtName" runat="server" AccessKey="4"></asp:TextBox>
<asp:Button ID="btnSearch" runat="server" Text="Go" PostBackUrl="searchResults.aspx"/>
<table style="width: 100%" height="100%" >
<tr>
<td colspan="2">
<BIE:HEADER id="header1" Runat="server"></BIE:HEADER>
</td>
</tr>
<tr>
<td colspan="2">
</td>
</tr>
<tr>
<td colspan="2">
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
searchResults.aspx
Code:
<%@ Import Namespace="System.Data.OleDb" %>
<%@ Import Namespace="System.Configuration" %>
<%@ Import Namespace="System.Data" %>
<%@ Page Language="C#" EnableEventValidation="False" %>
<%@ PreviousPageType VirtualPath="~/default.aspx" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
public void Page_Load(object sender, EventArgs e)
{
if (PreviousPage != null && PreviousPage.IsCrossPagePostBack)
{
TextBox txtName = (TextBox)Page.PreviousPage.FindControl("txtName");
GridView1.Visible = true;
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<asp:AccessDataSource ID="AccessDataSource1" runat="server" DataFile="<%$ connectionStrings: connString %>"
SelectCommand="SELECT [Course_ID], [Course], [Mode] FROM [Courses] WHERE [Course] LIKE '%' + @Course + '%'">
<SelectParameters>
<asp:ControlParameter ControlID="txtName" Name="Course" PropertyName="Text" Type="String" />
</SelectParameters>
</asp:AccessDataSource>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="Course" DataSourceID="AccessDataSource1" CssClass="courselist64">
<Columns>
<asp:HyperLinkField DataTextField="Course" DataNavigateUrlFields="Course_ID" DataNavigateUrlFormatString="Results.aspx?CourseID={0}" HeaderStyle-CssClass="courselist64" >
</asp:HyperLinkField>
<asp:BoundField DataField="Mode" HeaderText="Mode" SortExpression="Mode"/>
</Columns>
</asp:GridView>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</form>
</body>
</html>
I would appreciate any ideas. Thanks