Wrox Home  
Search P2P Archive for: Go

  Return to Index  

aspx thread: code-behind for .ascx user controls


Message #1 by "Michael Gaertner" <mgaert@b...> on Thu, 4 Jan 2001 23:10:12 -0000
Are there any examples out there of a simple code behind page that can be

inherited from a user control/.ascx. I'm trying to provide several user

controls with my DNS string from config.web and/or Context.Cache. Classes

inheriting from Page are no good for .ascx pages. Classes inheriting from

UserControl seem right, but I can't find anything about implementing the

HttpContext object correctly for class derived from UserCotrol.

Thanks.



---

http://www.asptoday.com - the leading site for timely,

in-depth information for ASP developers everywhere.

---

You are currently subscribed to aspx as: $subst('Recip.EmailAddr')

To unsubscribe send a blank email to leave-aspx-$subst('Recip.MemberIDChar')@p2p.wrox.com

Message #2 by "Jacques Leclerc" <jleclerc2@h...> on Fri, 5 Jan 2001 07:40:31 -0500
Here's the reply posted by Susan Warren (Microsoft) on a different

newsgroup.

*****************************

Beta1 of VS.NET does not support WYSIWYG editing of ASCX files, code-behind

or otherwise.  (Sorry, we're working on this support but didn't get it in

for Beta1.  It will be there in Beta2.)  I can explain how to create and

consume a code-behind ASCX manually, however.



To create:

1. Make a new Web Form

2. optionally, do as much design as you wish using the WYSIWYG editor.

After the next two steps, you won't have this support.

3. close the file

4. rename to the .ASCX extension

5. reopen the .ASCX file and make these changes:

- Change the <%@ Page ... %> directive to <%@ Control ... %>

- remove the <html> tags, entire <head> section, <body> tags and

<form runat=server> tags (and their end tags)

6. open the code-behind class file (you'll need to Project > Show All Files

to see it) and make these changes:

- change System.Web.UI.Page to System.Web.UI.UserControl in the two

places it appears

- for C# classes only, change Page.Init to this.Init



To consume:

1. Add a Register directive to the page, pointing to the ASCX file (just

like a single file ASCX)

2. If you want to code against the user control in the codebehind class for

the ASPX page, you'll need to add an instance variable in the code behind.

For example, let's say I have a user control "mywidget.ascx" whose

code-behind class name is "mywidget", that I consume on WebForm1.aspx with

the id "m1".  Further, the mywidget class defines a public member "message".

Here's how I'd set the message member programmatically:



   Public Class WebForm2

Inherits System.Web.UI.Page



Protected m1 as mywidget



Sub Page_Load(sender as Object, e as EventArgs)

   m1.message="Hello World"

End Sub

   End Class



hth,

Susan



**********************************





"Michael Gaertner" <mgaert@b...> wrote in message

news:28945@a...

>

> Are there any examples out there of a simple code behind page that can be

> inherited from a user control/.ascx. I'm trying to provide several user

> controls with my DNS string from config.web and/or Context.Cache. Classes

> inheriting from Page are no good for .ascx pages. Classes inheriting from

> UserControl seem right, but I can't find anything about implementing the

> HttpContext object correctly for class derived from UserCotrol.

> Thanks.

>



---

http://www.asptoday.com - the leading site for timely,

in-depth information for ASP developers everywhere.

---

You are currently subscribed to aspx as: $subst('Recip.EmailAddr')

To unsubscribe send a blank email to leave-aspx-$subst('Recip.MemberIDChar')@p2p.wrox.com

Message #3 by Susan Warren <swarren@m...> on Fri, 5 Jan 2001 09:36:06 -0800
I think the other part of Michael's question is whether he can extend the

UserControl class (which is the base class for a user control) with some

functionality that will be shared across a number of user controls.  The

answer is "sure", and it's a pretty powerful technique.  It works for Pages

too!  Say I always want to have my config-file-based DSN available to my

user controls.  Here's how I'd make a derived class, and use it as the base

class for my user controls.  (I show both code-behind and single file

versions below):



-----------------

Custom base class

-----------------

public class DataUserControl : UserControl {



	public String MyDSN {



		get {

			return

(String)((Hashtable)Context.GetConfig("appsettings"))("dsn");

		}

	}

}  //end class DataUserControl

	



--------------------------------------------------------

User Control that uses this base class: single file ASCX

--------------------------------------------------------

<%@ Inherits="DataUserControl" %>

<%@ Imports Namespace="System.Data" %>

<%@ Imports Namespace="System.Data.SQL" %>



<script language="C#" runat="server">



        protected void Page_Load(object sender, EventArgs e) {



            if (!IsPostBack) {

	            DataGrid1.DataSource 

MyDatabaseMethod().Tables[0].DefaultView;

	            DataGrid1.DataBind();

            }

        }



	  DataSet MyDatabaseMethod() {



		    SQLConnection connection = new SQLConnection(MyDSN);

//MyDSN is set in the base class

		    SQLDataSetCommand command = new

SQLDataSetCommand("SELECT * FROM Products", connection);

		    

		    DataSet dataset = new DataSet();

		    command.FillDataSet(dataset, "dataset");

		    

		    return dataset;

	  }



</script>



<asp:DataGrid id=DataGrid1 runat="server" />





--------------------------------------------------------

User Control that uses this base class: ASCX/code-behind

--------------------------------------------------------

<%@ Control language="c#" Codebehind="UserControl1.cs"

Inherits="UserControl1" %>



<asp:DataGrid id=DataGrid1 runat="server" />





---------------------------------------------------------

User Control that uses this base class: code-behind class

---------------------------------------------------------

namespace r1cs

{

    using System;

    using System.Data;

    using System.Data.SQL;

    using System.Web.UI.WebControls;



    public class UserControl1 : DataUserControl

    {

	  protected DataGrid DataGrid1;



        protected void Page_Load(object sender, EventArgs e) {



            if (!IsPostBack) {

	            DataGrid1.DataSource 

MyDatabaseMethod().Tables[0].DefaultView;

	            DataGrid1.DataBind();

            }

        }



	  DataSet MyDatabaseMethod() {



		    SQLConnection connection = new SQLConnection(MyDSN);

//MyDSN is set in the base class

		    SQLDataSetCommand command = new

SQLDataSetCommand("SELECT * FROM Products", connection);

		    

		    DataSet dataset = new DataSet();

		    command.FillDataSet(dataset, "dataset");

		    

		    return dataset;

	  }

    }

}





hth,

Susan





-----Original Message-----

From: Jacques Leclerc [mailto:jleclerc2@h...]

Sent: Friday, January 05, 2001 4:41 AM

To: ASP+

Subject: [aspx] Re: code-behind for .ascx user controls





Here's the reply posted by Susan Warren (Microsoft) on a different

newsgroup.

*****************************

Beta1 of VS.NET does not support WYSIWYG editing of ASCX files, code-behind

or otherwise.  (Sorry, we're working on this support but didn't get it in

for Beta1.  It will be there in Beta2.)  I can explain how to create and

consume a code-behind ASCX manually, however.



To create:

1. Make a new Web Form

2. optionally, do as much design as you wish using the WYSIWYG editor.

After the next two steps, you won't have this support.

3. close the file

4. rename to the .ASCX extension

5. reopen the .ASCX file and make these changes:

- Change the <%@ Page ... %> directive to <%@ Control ... %>

- remove the <html> tags, entire <head> section, <body> tags and

<form runat=server> tags (and their end tags)

6. open the code-behind class file (you'll need to Project > Show All Files

to see it) and make these changes:

- change System.Web.UI.Page to System.Web.UI.UserControl in the two

places it appears

- for C# classes only, change Page.Init to this.Init



To consume:

1. Add a Register directive to the page, pointing to the ASCX file (just

like a single file ASCX)

2. If you want to code against the user control in the codebehind class for

the ASPX page, you'll need to add an instance variable in the code behind.

For example, let's say I have a user control "mywidget.ascx" whose

code-behind class name is "mywidget", that I consume on WebForm1.aspx with

the id "m1".  Further, the mywidget class defines a public member "message".

Here's how I'd set the message member programmatically:



   Public Class WebForm2

Inherits System.Web.UI.Page



Protected m1 as mywidget



Sub Page_Load(sender as Object, e as EventArgs)

   m1.message="Hello World"

End Sub

   End Class



hth,

Susan



**********************************





"Michael Gaertner" <mgaert@b...> wrote in message

news:28945@a...

>

> Are there any examples out there of a simple code behind page that can be

> inherited from a user control/.ascx. I'm trying to provide several user

> controls with my DNS string from config.web and/or Context.Cache. Classes

> inheriting from Page are no good for .ascx pages. Classes inheriting from

> UserControl seem right, but I can't find anything about implementing the

> HttpContext object correctly for class derived from UserCotrol.

> Thanks.

>



---

http://www.asptoday.com - the leading site for timely,

in-depth information for ASP developers everywhere.

---

You are currently subscribed to aspx as: $subst('Recip.EmailAddr')

To unsubscribe send a blank email to leave-aspx-$subst('Recip.MemberIDChar')@p2p.wrox.com


  Return to Index