Forum Moderators: open

Message Too Old, No Replies

Code Behind with Dreamweaver MX

Is it possible to easily use Code Behind with Dreamweaver?

         

Barbacena

2:19 pm on Jul 25, 2005 (gmt 0)

10+ Year Member



Hi there, as it is the case with most web applications, one has to constantly reuse parts of the code you write for certain functionality throughout the site. As I’m using Dreamweaver with ASP.NET, I was wondering if it is possible to make use of the Code Behind technology with the software and if so are there things that I should be aware when making use of the Visual Studio technology.

Barbacena

5:35 pm on Jul 25, 2005 (gmt 0)

10+ Year Member



I Have been trying this Code Behind thing with Dreamweaver with the following files:

The Aspx file (choose.aspx)

<%@ page Language="C#" inherits="ChoiceForm.MyChoice" src="choices.aspx.cs" AutoEventWireup="false"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title><asp:Literal runat="server" id="lbTitle"></asp:Literal></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link href="../css/style.css" rel="stylesheet" type="text/css" />
</head>
<body>

<h2>Make your choice</h2>
<form runat="server">
<asp:DropDownList id="ddlChoice" runat="server" AutoPostBack="true" Rows="4" OnSelectedIndexChanged="subListChange" cssClass="ListBox">

</asp:DropDownList>
</form>
<h3><asp:Label id="lblXMLFileName" runat="server" /></h3>
<h2>The server is at: <asp:Label id="lblSettings" runat="server" /></h2>
<asp:DataGrid id="dgResult" runat="server" CssClass="Grid"/><br />
</body>
</html>

Now the code behind file

using System;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.IO;
using System.Xml;
using System.Xml.Xsl;
using System.Xml.XPath;

namespace ChoiceForm {

public class MyChoice : Page
{

public string strRoot = "choices/";
public string strDir;
public DropDownList ddlChoice;
protected Label lblXMLFileName;
protected Label lblSettings;
protected DataGrid dgResult;

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

if (!IsPostBack){

//Connection argument
SqlConnection objConnection = new SqlConnection(ConfigurationSettings.AppSettings["ConnString"]);

//SQL statement argument
string strSQLforListBox = "SELECT " + "choice_ID, choice " + "FROM " + "Choices ORDER BY choice_ID";
SqlCommand objCommand = new SqlCommand(strSQLforListBox, objConnection);

objConnection.Open();
ddlChoice.DataSource = objCommand.ExecuteReader();
ddlChoice.DataTextField = "choice";
ddlChoice.DataValueField = "choice_ID";
ddlChoice.DataBind();
objConnection.Close();
}
}
public void subListChange(object s, System.EventArgs e){

string strSelected = ddlChoice.SelectedItem.Value;
switch (strSelected){
case "1":
strDir = "Option1/";
break;
case "2":
strDir = "Option2/";
break;
case "3":
strDir = "Option3/";
break;
}

lblXMLFileName.Text = strRoot + strDir + "ChoiceFile.xml";

DataSet objDataSet = new DataSet();
objDataSet.ReadXml(Server.MapPath(strRoot + strDir + "ChoiceFile.xml"));

dgResult.DataSource = objDataSet.Tables[0].DefaultView;
dgResult.DataBind();
}

}
}

Basically what I have here is a page with a dropdown list from which you can chose specific theme settings.

The themes(choices) names are stored in the Database and should appear in the dropdown list whilst their settings are XML files of the same name(ChoiceFile.xml) which sit in different folders (option1, option2, option3 etc.) which can be found in turn in the strRoot(choices/) directory.

When an user makes a choice, the function subListChange looks for which folder that corresponds to the user choice, then loads the contents of ChoiceFile.xml into the dgResult grid. So if the user has chosen Theme1 the dgResult will display the contents “choices/option1/ChoiceFile.xml” and if he chooses Theme 2 the “choices/option2/ChoiceFile.xml”.

This should work smoothly, however for some reason the ddlChoice Dropdownlist loads as empty. The objConnection is correct and everything worked fine until I turned the code into a “Code Behind” class, so I don’t understand what I could be doing wrong.

Would anyone be able to enlighten me on this issue?

Regards

sharbel

12:38 pm on Jul 26, 2005 (gmt 0)

10+ Year Member



Did you compile the code behind file and put the resulting .dll file in the /bin directory of your application?

Barbacena

10:32 am on Jul 29, 2005 (gmt 0)

10+ Year Member



Bizarrely, I didn’t need to do that but I just place both files in the same folder and it worked.

Go figure.

But thanks for replying.

Just one theoretical question, can you have more than one codebehind directives in 1 aspx page?

If you need to use more than one class for a page, how do you call them all into the page?

Easy_Coder

11:04 am on Jul 29, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Barbacena, I don't think you can do that (multiple code behinds) but your main code behind can spin up any number of custom objects that you cut.

Barbacena

3:46 pm on Jul 29, 2005 (gmt 0)

10+ Year Member



That’s what I thought. So basically you have to create class libraries I assume, but I have to have a look into that as Dreamweaver isn’t very C# friendly.

thx

Easy_Coder

3:55 pm on Jul 29, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yeah that's right...

mainpage.aspx talks to mainpage.aspx.cs

mainpage.aspx.cs can reference and use:
- dataportal object
- choices object
- error logging object
- an external web service
- just about anything you want...

Barbacena

5:27 pm on Jul 29, 2005 (gmt 0)

10+ Year Member



Apart from the web services bit, nothing on your list rings bells to me LOL but I guess that someone for someone who comes from an “includes” background this whole codebehind thing is still quite alien to me. For the moment I’m just trying to figure what’s the best way to make a component available in every page of the application with slight variations (by basically componentizing the class in different parts). I also read about .NET templates, but I'm just going to take one thing at the time :)

Easy_Coder

6:18 pm on Jul 29, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hang in there, you'll get there... you're underway and learning already.

Easy_Coder

6:36 pm on Jul 29, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



let's see of we can get your code working...

you need to make sure that the code that your expecting to run on the server side is sitting in between your form tags...

this...
</form>
<h3><asp:Label id="lblXMLFileName" runat="server" /></h3>
<h2>The server is at: <asp:Label id="lblSettings" runat="server" /></h2>
<asp:DataGrid id="dgResult" runat="server" CssClass="Grid"/><br />
</body>
</html>

should be...
<h3><asp:Label id="lblXMLFileName" runat="server" /></h3>
<h2>The server is at: <asp:Label id="lblSettings" runat="server" /></h2>
<asp:DataGrid id="dgResult" runat="server" CssClass="Grid"/><br />
</form>
</body>
</html>

Easy_Coder

6:38 pm on Jul 29, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



what's the xml file look like?

Barbacena

2:27 pm on Aug 2, 2005 (gmt 0)

10+ Year Member



Thanks a lot EasyCoder, but as I said above the code worked when I put both CS and ASPX file in a separate folder together and changed some the protection level of certain members of the class. But I put the form tags in the right places anyway as you suggest as it is good to have better code practice, thx.

What I’m finding frustrating is to come up with a way of using more than one class in an ASPX page because with code behind it seems that you can only call one CS file and one class at the time (unless you actually put the class directly on your page which is a bit useless) if you want to reuse it in others. I’ve started to learn from a C# and .NET book now but there is so much for me to cover before I even get to the class library bit and my project will need to be ready way before then(as it's usually the case LOL). I jumped to the chapter about class libraries(even if I didn't understand all of it) but there is still only an example with "1 Cs file and 1 Aspx" file. So if I have a class that needs to be used in every page as other classes for each of the pages' individual functionality, I can’t do that. With includes you were able to call in as much functionality into one page as you wanted and even if it was messy, you still only had 1 file to edit, if you had to change anything.

I know that things are supposed to change with .NET 2.0 but for now it seems that there is the need for too much coding even for doing basic things.

Regards

Easy_Coder

5:12 pm on Aug 2, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Oops, i missed that part where you got it working!

Yeah so with the code behind your not limited to calling just one class. You can so something like this all day long...

Say you have a custom object:
class MyCustomObject
{
public string foo()
{
return "bar";
}
}

You can call that from your code behind easily:
private void Page_Load(object sender, System.EventArgs e)
{
MyCustomObject mco = new MyCustomObect()
string mcoRetVal = mco.foo();
}

That custom object can exist in it's own class or right in your code behind as a seperate class.

namespace ChoiceForm {
public class MyChoice : Page
{
// your current stuff
}

// new custom class
class MyCustomObject
{
public string foo()
{
return "bar";
}
}
}

Barbacena

9:56 am on Aug 3, 2005 (gmt 0)

10+ Year Member



That’s great EasyCoder, I knew you could use more than one class for as long as they are in the same CS file and the class is called from within that CS file, but I didn’t know that they could refer to one another if they are in “separate” CS codebehind files.

Do I need any special directives for 1 CS file to be able to refer to another or can they simply pick up each other’s existence if they are in the same folder or compiled in the bin directory?

I was also reading about controls, they seem to be a better alternative to Codebehind as you don’t appear to be limited to one “Register” directive on your page, but in which circumstances would be better to use a bespoke control or codebehind?

Thanks.

Easy_Coder

10:54 am on Aug 4, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Do I need any special directives for 1 CS file to be able to refer to another

Yeah just drop the dll in your /bin/ like you said and then add a project reference to it. When you want to instantiate the object just do something like this...

MyCustomObject mco = new MyCustomObect()
string mcoRetVal = mco.foo();

I'm a Visual Studio.net user so I can't tell you how to add a dll reference to your project in Dreamweaver. In VS we right click on references which is located in the Project Tree and from there we add either a Web Service Reference or a COM, project or .Net DLL reference.