Forum Moderators: open
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
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?
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>
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
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";
}
}
}
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.
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.