dprasad
asked on
using datalist with codebehind
i'm trying to convert my application to use codebehind files instead of just single files. ii need to set the datasource for the list to a method call, the method also needs the customerid variable. Here's the code
<asp:DataList id="MyList" runat="server" RepeatColumns="2" DataSource="<%#customerpro file.GetPr ofile(cust omerId) %>">
<ItemTemplate>
<table border="0" width="300">
<tr>
<td width="25">
</td>
</a>
</td>
</tr>
<br />
<br />
<b>Firstname: </b> <%# DataBinder.Eval(Container. DataItem, "firstname") %>
<br />
<b>Email: </b> <%# DataBinder.Eval(Container. DataItem, "emailaddress") %>
<br />
<b>password: </b> <%# DataBinder.Eval(Container. DataItem, "password") %>
<br />
<b>Last Name: </b> <%# DataBinder.Eval(Container. DataItem, "lastname") %>
<br />
<b>Company:</b> <%# DataBinder.Eval(Container. DataItem, "company") %>
<br />
<b>Url: </b> <%# DataBinder.Eval(Container. DataItem, "url") %>
<br />
<b>Phone: </b> <%# DataBinder.Eval(Container. DataItem, "phonenumber") %>
<br />
<b>Address: </b> <%# DataBinder.Eval(Container. DataItem, "address") %>
<br />
<b>State: </b> <%# DataBinder.Eval(Container. DataItem, "state") %> <%# DataBinder.Eval(Container. DataItem, "state") %>
<br />
<b>Zipcode: </b> <%# DataBinder.Eval(Container. DataItem, "zipcode") %>
</table>
</ItemTemplate>
</asp:DataList>
CODEBEHIND
...
private void Page_Load(object sender, System.EventArgs e)
{
string customerid;
int customerId = Int32.Parse(Request.Params ["Customer ID"]);
Session["customerid"] = customerId;
customerid = Session["customerid"].ToSt ring();
// Obtain products and databind to an asp:datalist control
IBuySpy.CustomersDB customerprofile = new IBuySpy.CustomersDB();
//MyList.DataSource = customerprofile.GetProfile (customerI d);
//MyList.DataBind();
}
...
I commented the last two lines, which were how the single file solution worked. Any ideas on how to get this to work? I get this error since obviously it can't see the customerid:
Compiler Error Message: CS0103: The name 'customerId' does not exist in the class or namespace 'ASP.Userprofile_aspx'
Line 13: <asp:DataList id="MyList" runat="server" RepeatColumns="2" DataSource="<%#customerpro file.GetPr ofile(cust omerId) %>">
<asp:DataList id="MyList" runat="server" RepeatColumns="2" DataSource="<%#customerpro
<ItemTemplate>
<table border="0" width="300">
<tr>
<td width="25">
</td>
</a>
</td>
</tr>
<br />
<br />
<b>Firstname: </b> <%# DataBinder.Eval(Container.
<br />
<b>Email: </b> <%# DataBinder.Eval(Container.
<br />
<b>password: </b> <%# DataBinder.Eval(Container.
<br />
<b>Last Name: </b> <%# DataBinder.Eval(Container.
<br />
<b>Company:</b> <%# DataBinder.Eval(Container.
<br />
<b>Url: </b> <%# DataBinder.Eval(Container.
<br />
<b>Phone: </b> <%# DataBinder.Eval(Container.
<br />
<b>Address: </b> <%# DataBinder.Eval(Container.
<br />
<b>State: </b> <%# DataBinder.Eval(Container.
<br />
<b>Zipcode: </b> <%# DataBinder.Eval(Container.
</table>
</ItemTemplate>
</asp:DataList>
CODEBEHIND
...
private void Page_Load(object sender, System.EventArgs e)
{
string customerid;
int customerId = Int32.Parse(Request.Params
Session["customerid"] = customerId;
customerid = Session["customerid"].ToSt
// Obtain products and databind to an asp:datalist control
IBuySpy.CustomersDB customerprofile = new IBuySpy.CustomersDB();
//MyList.DataSource = customerprofile.GetProfile
//MyList.DataBind();
}
...
I commented the last two lines, which were how the single file solution worked. Any ideas on how to get this to work? I get this error since obviously it can't see the customerid:
Compiler Error Message: CS0103: The name 'customerId' does not exist in the class or namespace 'ASP.Userprofile_aspx'
Line 13: <asp:DataList id="MyList" runat="server" RepeatColumns="2" DataSource="<%#customerpro
Or try another way like so
public class Userprofile : System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e)
{
string customerid;
int customerId = Int32.Parse(Request.Params
Session["customerid"] = customerId;
customerid = Session["customerid"].ToSt
// Obtain products and databind to an asp:datalist control
IBuySpy.CustomersDB customerprofile = new IBuySpy.CustomersDB();
MyList.DataSource = customerprofile.GetProfile
MyList.DataBind();
}
}
And inside aspx declare the datalist server control tag without datasource attribute
<asp:DataList id="MyList" runat="server" RepeatColumns="2">
That should solve your problem faster.
ASKER
ok, I tried that but am now getting this error:
Compiler Error Message: CS0246: The type or namespace name 'customerprofile' could not be found (are you missing a using directive or an assembly reference?)
here is what my codebehind is now:
...
public class Userprofile : System.Web.UI.Page
{
protected int customerId;
private void Page_Load(object sender, System.EventArgs e)
{
int customerId = Int32.Parse(Request.Params ["Customer ID"]);
// Obtain products and databind to an asp:datalist control
IBuySpy.CustomersDB customerprofile = new IBuySpy.CustomersDB();
//MyList.DataSource = customerprofile.GetProfile (customerI d);
//MyList.DataBind();
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.P age_Load);
}
#endregion
}
...
Compiler Error Message: CS0246: The type or namespace name 'customerprofile' could not be found (are you missing a using directive or an assembly reference?)
here is what my codebehind is now:
...
public class Userprofile : System.Web.UI.Page
{
protected int customerId;
private void Page_Load(object sender, System.EventArgs e)
{
int customerId = Int32.Parse(Request.Params
// Obtain products and databind to an asp:datalist control
IBuySpy.CustomersDB customerprofile = new IBuySpy.CustomersDB();
//MyList.DataSource = customerprofile.GetProfile
//MyList.DataBind();
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.P
}
#endregion
}
...
ASKER
alright, i tried the second suggestion.. this was my original problem actually, I get this compliation error:
E:\Inetpub\wwwroot\StoreCS VS\Userpro file.aspx. cs(32): The type or namespace name 'MyList' could not be found (are you missing a using directive or an assembly reference?)
E:\Inetpub\wwwroot\StoreCS
The first problem, import full qualified customerprofile class in the Userprofile.aspx.cs. Something similar like the following
using IBuySpy;
And also in the Userprofile.aspx
<%@ Import Namespace="IBuySpy" %>
The "IBuySpy" is the namespace name where the CustomersDB resides.
using IBuySpy;
And also in the Userprofile.aspx
<%@ Import Namespace="IBuySpy" %>
The "IBuySpy" is the namespace name where the CustomersDB resides.
The second problem, In VS.NET IDE, close the two file Userprofile.aspx and Userprofile.aspx.cs. And then recompile, always recompile after you make any change in your code.
ASKER
hmm, I tried the first suggesion and I still get the The type or namespace name 'customerprofile' could not be found runtime error.
I did try the second suggestion and close the two userprofile files and rebuild. got the same error. Here's my .cs file
using IBuySpy;
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
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 IBuySpy
{
/// <summary>
/// Summary description for Userprofile.
/// </summary>
public class Userprofile : System.Web.UI.Page
{
protected System.Web.UI.WebControls. DataList MyList;
protected int customerId;
private void Page_Load(object sender, System.EventArgs e)
{
customerId = Int32.Parse(Request.Params ["Customer ID"]);
Session["customerid"] = customerId;
//customerid = Session["customerid"].ToSt ring();
// Obtain products and databind to an asp:datalist control
IBuySpy.CustomersDB customerprofile = new IBuySpy.CustomersDB();
//MyList.DataSource = customerprofile.GetProfile (customerI d);
//MyList.DataBind();
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.P age_Load);
}
#endregion
}
}
ASP
<asp:DataList id="MyList" runat="server" RepeatColumns="2" DataSource="<%#customerpro file.GetPr ofile(cust omerId)%>" >
<ItemTemplate>
<table border="0" width="300">
<tr>
<td width="25">
</td>
</a> </td>
</tr>
<br />
<br />
<b>Firstname: </b>
<%# DataBinder.Eval(Container. DataItem, "firstname") %>
<br />
<b>Email: </b>
<%# DataBinder.Eval(Container. DataItem, "emailaddress") %>
<br />
<b>password: </b>
<%# DataBinder.Eval(Container. DataItem, "password") %>
<br />
<b>Last Name: </b>
<%# DataBinder.Eval(Container. DataItem, "lastname") %>
<br />
<b>Company:</b>
<%# DataBinder.Eval(Container. DataItem, "company") %>
<br />
<b>Url: </b>
<%# DataBinder.Eval(Container. DataItem, "url") %>
<br />
<b>Phone: </b>
<%# DataBinder.Eval(Container. DataItem, "phonenumber") %>
<br />
<b>Address: </b>
<%# DataBinder.Eval(Container. DataItem, "address") %>
<br />
<b>State: </b>
<%# DataBinder.Eval(Container. DataItem, "state") %>
<%# DataBinder.Eval(Container. DataItem, "state") %>
<br />
<b>Zipcode: </b>
<%# DataBinder.Eval(Container. DataItem, "zipcode") %>
</table>
</ItemTemplate>
</asp:DataList>
...
I did try the second suggestion and close the two userprofile files and rebuild. got the same error. Here's my .cs file
using IBuySpy;
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
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 IBuySpy
{
/// <summary>
/// Summary description for Userprofile.
/// </summary>
public class Userprofile : System.Web.UI.Page
{
protected System.Web.UI.WebControls.
protected int customerId;
private void Page_Load(object sender, System.EventArgs e)
{
customerId = Int32.Parse(Request.Params
Session["customerid"] = customerId;
//customerid = Session["customerid"].ToSt
// Obtain products and databind to an asp:datalist control
IBuySpy.CustomersDB customerprofile = new IBuySpy.CustomersDB();
//MyList.DataSource = customerprofile.GetProfile
//MyList.DataBind();
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.P
}
#endregion
}
}
ASP
<asp:DataList id="MyList" runat="server" RepeatColumns="2" DataSource="<%#customerpro
<ItemTemplate>
<table border="0" width="300">
<tr>
<td width="25">
</td>
</a> </td>
</tr>
<br />
<br />
<b>Firstname: </b>
<%# DataBinder.Eval(Container.
<br />
<b>Email: </b>
<%# DataBinder.Eval(Container.
<br />
<b>password: </b>
<%# DataBinder.Eval(Container.
<br />
<b>Last Name: </b>
<%# DataBinder.Eval(Container.
<br />
<b>Company:</b>
<%# DataBinder.Eval(Container.
<br />
<b>Url: </b>
<%# DataBinder.Eval(Container.
<br />
<b>Phone: </b>
<%# DataBinder.Eval(Container.
<br />
<b>Address: </b>
<%# DataBinder.Eval(Container.
<br />
<b>State: </b>
<%# DataBinder.Eval(Container.
<%# DataBinder.Eval(Container.
<br />
<b>Zipcode: </b>
<%# DataBinder.Eval(Container.
</table>
</ItemTemplate>
</asp:DataList>
...
Assuming the customerprofile class looks like this
namespace theNamespace
{
public class customerprofile
{
....
....
}
....
....
}
in the Userprofile.aspx.cs import the namespace, like so
<%@ Import Namespace="theNamespace" %>
<HTML>
<HEAD>
<link rel="stylesheet" type="text/css" href="IBuySpy.css">
</HEAD>
<body background="images/sitebkg rdnogray.g if" leftmargin="0" topmargin="0" rightmargin="0" bottommargin="0" marginheight="0" marginwidth="0">
using theNamespace; <<== need this
namespace IBuySpy
{
/// <summary>
/// Summary description for Userprofile.
/// </summary>
public class Userprofile : System.Web.UI.Page
{
}
}
And in the Userprofile.aspx
<%@ Import Namespace="theNamespace" %> <<== need this
<HTML>
<HEAD>
<link rel="stylesheet" type="text/css" href="IBuySpy.css">
</HEAD>
<body background="images/sitebkg rdnogray.g if" leftmargin="0" topmargin="0" rightmargin="0" bottommargin="0" marginheight="0" marginwidth="0">
Pay attention to letter case-sensivity.
namespace theNamespace
{
public class customerprofile
{
....
....
}
....
....
}
in the Userprofile.aspx.cs import the namespace, like so
<%@ Import Namespace="theNamespace" %>
<HTML>
<HEAD>
<link rel="stylesheet" type="text/css" href="IBuySpy.css">
</HEAD>
<body background="images/sitebkg
using theNamespace; <<== need this
namespace IBuySpy
{
/// <summary>
/// Summary description for Userprofile.
/// </summary>
public class Userprofile : System.Web.UI.Page
{
}
}
And in the Userprofile.aspx
<%@ Import Namespace="theNamespace" %> <<== need this
<HTML>
<HEAD>
<link rel="stylesheet" type="text/css" href="IBuySpy.css">
</HEAD>
<body background="images/sitebkg
Pay attention to letter case-sensivity.
ASKER
hmmm.. yeah that's what i've done.. I will keep fiddling with it. thanks!
Dinesh
Dinesh
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
ahh.. got it now. think i misplaced some code between the solution. Thanks!
Dinesh
Dinesh
the problem because of this line
<asp:DataList id="MyList" runat="server" RepeatColumns="2" DataSource="<%#customerpro
the customerId variable needs to be declared as protected on top of the class (module class scope) in order to be visible within aspx.
public class Userprofile : System.Web.UI.Page
{
protected int customerId; // place the var here..
.....
.....
}