Link to home
Start Free TrialLog in
Avatar of dprasad
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="<%#customerprofile.GetProfile(customerId) %>">
                                        <ItemTemplate>
                                            <table border="0" width="300">
                                                <tr>
                                                    <td width="25">
                                                        &nbsp;
                                                    </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["CustomerID"]);
                  Session["customerid"] = customerId;
                  customerid = Session["customerid"].ToString();

                  // Obtain products and databind to an asp:datalist control
                  IBuySpy.CustomersDB customerprofile = new IBuySpy.CustomersDB();

                  //MyList.DataSource = customerprofile.GetProfile(customerId);
                  //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="<%#customerprofile.GetProfile(customerId) %>">
      
Avatar of ihenry
ihenry

hello dprasad,

the problem because of this line
<asp:DataList id="MyList" runat="server" RepeatColumns="2" DataSource="<%#customerprofile.GetProfile(customerId) %>">

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..
     .....
     .....
}

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["CustomerID"]);
            Session["customerid"] = customerId;
            customerid = Session["customerid"].ToString();

            // Obtain products and databind to an asp:datalist control
            IBuySpy.CustomersDB customerprofile = new IBuySpy.CustomersDB();

            MyList.DataSource = customerprofile.GetProfile(customerId);
            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.
Avatar of dprasad

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["CustomerID"]);
                  
                  // Obtain products and databind to an asp:datalist control
                  IBuySpy.CustomersDB customerprofile = new IBuySpy.CustomersDB();

                  //MyList.DataSource = customerprofile.GetProfile(customerId);
                  //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.Page_Load);
            }
            #endregion
      }
...
Avatar of dprasad

ASKER

alright, i tried the second suggestion.. this was my original problem actually, I get this compliation error:

E:\Inetpub\wwwroot\StoreCSVS\Userprofile.aspx.cs(32): The type or namespace name 'MyList' could not be found (are you missing a using directive or an assembly reference?)


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.

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.
Avatar of dprasad

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["CustomerID"]);
                  Session["customerid"] = customerId;
                  //customerid = Session["customerid"].ToString();

                  // Obtain products and databind to an asp:datalist control
                  IBuySpy.CustomersDB customerprofile = new IBuySpy.CustomersDB();

                  //MyList.DataSource = customerprofile.GetProfile(customerId);
                  //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.Page_Load);

            }
            #endregion
      }
}

ASP

<asp:DataList id="MyList" runat="server" RepeatColumns="2" DataSource="<%#customerprofile.GetProfile(customerId)%>">
                  <ItemTemplate>
                        <table border="0" width="300">
                              <tr>
                                    <td width="25">
                                          &nbsp;
                                    </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>
...

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/sitebkgrdnogray.gif" 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/sitebkgrdnogray.gif" leftmargin="0" topmargin="0" rightmargin="0" bottommargin="0" marginheight="0" marginwidth="0">

Pay attention to letter case-sensivity.
Avatar of dprasad

ASKER

hmmm.. yeah that's what i've done.. I will keep fiddling with it. thanks!

Dinesh
ASKER CERTIFIED SOLUTION
Avatar of ihenry
ihenry

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dprasad

ASKER

ahh.. got it now. think i misplaced some code between the solution. Thanks!

Dinesh