Saturday, March 24, 2012

Pass variable into a user control

Please help, I've been working on this for hours and can't seem to find an answer online.

I have a main page (index.aspx) that uses a usercontrol (DropDownSearch.ascx). Inside the usercontrol are 3 dropdownlists with a code behind file that fills them with data from a query. The query is based on a variable which is sent by the main (index.aspx) file. Inside the code behind for the main page (index.aspx) I try to send the variable over to the codebehind of the user control, but it keeps failing.

Here is the user control info in index.aspx:
<%@dotnet.itags.org.Register TagPrefix="MyControl" Tagname="DropDownSearch" src="http://pics.10026.com/?src=dropdownsearch.ascx"%>
<MyControl:DropDownSearch ID="SearchMenus" Runat="server"></MyControl:DropDownSearch
Here is the code for the index.aspx.cs:

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace MainPage
{
/// <summary>
/// Summary description for index.
/// </summary>
public class index : System.Web.UI.Page
{
protected System.Web.UI.WebControls.RadioButton Functional;
protected System.Web.UI.WebControls.RadioButton Pwb;
protected System.Web.UI.WebControls.RadioButton Decorative;
protected System.Web.UI.WebControls.RadioButton Microelectronics;
public string SqlCat1, SqlCat2;
protected SearchMenus DropDownSearch;

private void Page_Load(object sender, System.EventArgs e)
{

}

protected void CategorySelected(object sender, System.EventArgs e)
{

if (Functional.Checked)
{
SearchMenus.SqlCat1 = "1";
SearchMenus.SqlCat2 = "1";
}

if (Decorative.Checked)
{
SearchMenus.SqlCat1 = "2";
SearchMenus.SqlCat2 = "2";
}

if (Pwb.Checked)
{
SearchMenus.SqlCat1 = "3";
SearchMenus.SqlCat2 = "3";
}

if (Microelectronics.Checked)
{
SearchMenus.SqlCat1 = "4";
SearchMenus.SqlCat2 = "4";
}

}
}
}

Here is the user control code behind (dropdownsearch.ascx.cs):
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace Enthone
{
/// <summary>
/// Summary description for tier.
/// </summary>
public class dropdownsearch : System.Web.UI.UserControl
{
SqlConnection SearchConnection;
protected DataSet MyDataSet = new DataSet();
public DropDownList TradeNames, Applications, Processes;
public string SqlCat1, SqlCat2;

private void Page_Load(object sender, System.EventArgs e)
{
SearchConnection = new SqlConnection("Server=localhost;uid=sa;password=;database=Enthone");
string MySqlStatement = "sp_DropDownsSql " +SqlCat1+", "+SqlCat2;
SqlDataAdapter MyAdapter = new SqlDataAdapter(MySqlStatement, SearchConnection);
MyAdapter.Fill(MyDataSet);

TradeNames.DataSource = MyDataSet.Tables["Table"];
TradeNames.DataBind();

Applications.DataSource = MyDataSet.Tables["Table1"];
Applications.DataBind();

Processes.DataSource = MyDataSet.Tables["Table2"];
Processes.DataBind();

SearchConnection.Close();
}

override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}

private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}

}
}

ThanksHow is it failing? Do you get an exception? (If so, what is the complete exception text?) Incorrect results? What's going wrong?

Don
When I go to build it from vs.net I get this error in the index.aspx.cs file, "c:\inetpub\wwwroot\MyApp\index.aspx.cs(25): The type or namespace name 'SearchMenus' could not be found (are you missing a using directive or an assembly reference?)
"
Okay, that means that it can't find the SearchMenus namespace. Where is that located? In another project? Is it open in your VS.NET solution file? You'll need to add a reference to it in Solution Explorer, by right-clicking on References in the project with Index.aspx and adding a reference to the project or DLL.

There are lots of ways to configure these things in VS.NET though, so explain where SearchMenus is located and we should be able to figure out what's wrong.

Don
O.K. I got the whole thing working, but would definitly appreciatea look at my code to make sure I'm handling it the right way...

User Control Info on index.aspx:


<%@.Register TagPrefix="MyControl" Tagname="DropDownSearch" src="http://pics.10026.com/?src=dropdownsearch.ascx"%
<MyControl:DropDownSearch ID="SearchMenus" Runat="server"></MyControl:DropDownSearch>

Code Behind of index.aspx:


using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace MyNamespace
{
/// <summary>
/// Summary description for index.
/// </summary>
public class index : System.Web.UI.Page
{
protected System.Web.UI.WebControls.RadioButton Functional;
protected System.Web.UI.WebControls.RadioButton Pwb;
protected System.Web.UI.WebControls.RadioButton Decorative;
protected System.Web.UI.WebControls.RadioButton Microelectronics;
public string SqlCat1, SqlCat2;
protected MyNamespace.dropdownsearch SearchMenus;

private void InitializeComponent()
{

}

private void Page_Load(object sender, System.EventArgs e)
{
}

protected void CategorySelected(object sender, System.EventArgs e)
{

if (Functional.Checked)
{
SearchMenus.SqlCat1 = "1";
SearchMenus.SqlCat2 = "1";
SearchMenus.DbCall();
}

if (Decorative.Checked)
{
SearchMenus.SqlCat1 = "2";
SearchMenus.SqlCat2 = "2";
SearchMenus.DbCall();
}

if (Pwb.Checked)
{
SearchMenus.SqlCat1 = "3";
SearchMenus.SqlCat2 = "3";
SearchMenus.DbCall();
}

if (Microelectronics.Checked)
{
SearchMenus.SqlCat1 = "4";
SearchMenus.SqlCat2 = "4";
SearchMenus.DbCall();
}

}
}
}


Code behind of User Control:

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace MyNamespace
{
/// <summary>
/// Summary description for tier.
/// </summary>
public class dropdownsearch : System.Web.UI.UserControl
{
SqlConnection SearchConnection;
protected DataSet MyDataSet = new DataSet();
public DropDownList TradeNames, Applications, Processes;
public Label MyLabel;
private string _SqlCat1;
private string _SqlCat2;

public string SqlCat1
{

get
{
return _SqlCat1;
}
set
{
_SqlCat1 = value;
}
}

public string SqlCat2
{

get
{
return _SqlCat2;
}
set
{
_SqlCat2 = value;
}
}

private void Page_Init(object sender, System.EventArgs e){

}

private void Page_Load(object sender, System.EventArgs e)
{
}

public void DbCall()
{
SearchConnection = new SqlConnection("Server=localhost;uid=sa;password=;database=db");
string MySqlStatement = "sp_DropDownsSql " +SqlCat1+", "+SqlCat2;
SqlDataAdapter MyAdapter = new SqlDataAdapter(MySqlStatement, SearchConnection);
MyAdapter.Fill(MyDataSet);

TradeNames.DataSource = MyDataSet.Tables["Table"];
TradeNames.DataBind();

Applications.DataSource = MyDataSet.Tables["Table1"];
Applications.DataBind();

Processes.DataSource = MyDataSet.Tables["Table2"];
Processes.DataBind();

SearchConnection.Close();
}

override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}

private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}

}
}

Thanks

0 comments:

Post a Comment