[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

using datalist with codebehind

Posted on 2004-11-08
11
Medium Priority
?
1,353 Views
Last Modified: 2012-06-22
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) %>">
      
0
Comment
Question by:dprasad
  • 6
  • 5
11 Comments
 
LVL 20

Expert Comment

by:ihenry
ID: 12529716
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..
     .....
     .....
}
0
 
LVL 20

Expert Comment

by:ihenry
ID: 12529746

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

Author Comment

by:dprasad
ID: 12529771
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
      }
...
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:dprasad
ID: 12529781
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?)


0
 
LVL 20

Expert Comment

by:ihenry
ID: 12529806
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.
0
 
LVL 20

Expert Comment

by:ihenry
ID: 12529815

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

Author Comment

by:dprasad
ID: 12529867
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>
...

0
 
LVL 20

Expert Comment

by:ihenry
ID: 12529915
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.
0
 

Author Comment

by:dprasad
ID: 12530010
hmmm.. yeah that's what i've done.. I will keep fiddling with it. thanks!

Dinesh
0
 
LVL 20

Accepted Solution

by:
ihenry earned 2000 total points
ID: 12530089
So, do you get it works?
If you have the customerprofile class is in another assembly then add a reference to the assembly in your project.
0
 

Author Comment

by:dprasad
ID: 12530108
ahh.. got it now. think i misplaced some code between the solution. Thanks!

Dinesh
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question