asp.net, C#, constructor in new class

R

Ranginald

I am learning C# and asp.net and am trying to create a new utility
class.

The default code you get from vwd express is:

/// <summary>
/// Summary description for ddlCode
/// </summary>
public class someClass
{
public someClass()
{
//
// TODO: Add constructor logic here
//

}
}

If I have a method, for example txtBoxSubmit, that assigns a variable
(string txtData) from txtBox.text, where do I put that code in the
above scaffold? Can it have an empty constructor?

Thanks!
 
L

Lau Lei Cheong

You may have a constructor which accepts a string as parameter to make it
reusable on other places (compatible for exposing read-only property of the
string so others can't change it once the object is created), or have a
empty constructor and a property that let you change that value. It's
basically a choice of designing how you want to make it work.

Empty constructor is legal of course, just meaning this class won't do any
custom initialization when it's created.

And I don't prefer to have control names explicitly coded into classes other
than the webform itself, to preserve reusability and code cleaness.
 
R

Ranginald

On that note, I was hoping you or someone could assist me with the
following small problem:

I have a .ascx user control that declares a dropdownlist, ddlCategory,
and populates it from a sqlserver database. That works fine.

I am trying to create a common class for a method, runCategory, that
executes at the OnSelectedIndexChanged of the above user control
dropdown list. If I put this runCategory() in the control's own
codebehind file, along with the instantiation code, all is well..

BUT.......I want to use this situation as to learn how to create a
simple asp.net C# class. l know that I could just put this
ddlCategory() method into the .ascx code file without any trouble and
it will work but that defeats the learning purpose for me here.

I am doing nothing in design view and I want to have it all in code.

So when I try to put the OnSelectedIndexChanged functionality into its
own class, I get a "the name 'ddlCategory; does not exist in the
current context". I also get a "the name 'response' does not exist in
the current context..."

Could someone please explain why this behavior is occuring and how to
fix it?
============================
Here is the code (it's really simple):
/**** file = ddlCode.cs ********
/

/// <summary>
/// Summary description for ddlCode
/// </summary>
public class ddlCode
{
public ddlCode()
{
//
// TODO: Add constructor logic here
//

}

public void runCategory(object sender, EventArgs e)
{
string category;
category = ddlCategory.SelectedValue;
int index;
index = ddlCategory.SelectedIndex;
if (index != 0)
{
Response.Redirect("./sculpture2.aspx?catID=" + category);
}
}
I am not declaring a new namespace in the .ascx.cs file (if that helps
at all).


Thanks,
David
 
R

Ranginald

I acutally get the same message in if I combine it all into one file
(the ddlCategory error).
The partial class is public, the page_load is public, and runcategory
is public.

Thanks.
 
L

Lau Lei Cheong

So you want to create a generic static method for runCategory?

But unfortunately, static method (or external objects) don't have direct
access to the current webform, so some workarounds are needed.

Something like this (Disclaimer: I wrote this in .NET v1.1, although it
should work in v2.0 too):

public static void runCategory(object sender, EventArgs e)
{
string category;
// This will look for value of "ddlCategory" in the current
request stream
category = HttpContext.Current.Request.Form["ddlCategory"];
index = getIndexZeroItem();
if (!category.Equals(getIndexZeroItem()))
{
HttpContext.Current.Response.Redirect("./sculpture2.aspx?catID="
+ category);
}
}

public static string getIndexZeroItem()
{
// implements this yourself to retrieve the first item in
dropdownlist, you may
// cheat by pushing this value in Session when you create the
dropdownlist
}
 
L

Lau Lei Cheong

Opps... forgotten to remove the following line...
index = getIndexZeroItem();

Lau Lei Cheong said:
So you want to create a generic static method for runCategory?

But unfortunately, static method (or external objects) don't have direct
access to the current webform, so some workarounds are needed.

Something like this (Disclaimer: I wrote this in .NET v1.1, although it
should work in v2.0 too):

public static void runCategory(object sender, EventArgs e)
{
string category;
// This will look for value of "ddlCategory" in the current
request stream
category = HttpContext.Current.Request.Form["ddlCategory"];
index = getIndexZeroItem();
if (!category.Equals(getIndexZeroItem()))
{

HttpContext.Current.Response.Redirect("./sculpture2.aspx?catID=" +
category);
}
}

public static string getIndexZeroItem()
{
// implements this yourself to retrieve the first item in
dropdownlist, you may
// cheat by pushing this value in Session when you create the
dropdownlist
}


Ranginald said:
On that note, I was hoping you or someone could assist me with the
following small problem:

I have a .ascx user control that declares a dropdownlist, ddlCategory,
and populates it from a sqlserver database. That works fine.

I am trying to create a common class for a method, runCategory, that
executes at the OnSelectedIndexChanged of the above user control
dropdown list. If I put this runCategory() in the control's own
codebehind file, along with the instantiation code, all is well..

BUT.......I want to use this situation as to learn how to create a
simple asp.net C# class. l know that I could just put this
ddlCategory() method into the .ascx code file without any trouble and
it will work but that defeats the learning purpose for me here.

I am doing nothing in design view and I want to have it all in code.

So when I try to put the OnSelectedIndexChanged functionality into its
own class, I get a "the name 'ddlCategory; does not exist in the
current context". I also get a "the name 'response' does not exist in
the current context..."

Could someone please explain why this behavior is occuring and how to
fix it?
[Code removed]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top