By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,775 Members | 1,355 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,775 IT Pros & Developers. It's quick & easy.

asp.net, C#, constructor in new class

P: n/a
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!

May 12 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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.

"Ranginald" <da*******@gmail.com>
???????:11**********************@i39g2000cwa.googl egroups.com...
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!

May 12 '06 #2

P: n/a
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

May 12 '06 #3

P: n/a
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.

May 12 '06 #4

P: n/a
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" <da*******@gmail.com>
???????:11*********************@u72g2000cwu.google groups.com...
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]

May 12 '06 #5

P: n/a
Opps... forgotten to remove the following line...
index = getIndexZeroItem();
"Lau Lei Cheong" <le****@yehoo.com.hk> glsD:uj**************@TK2MSFTNGP03.phx.g bl...
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" <da*******@gmail.com>
???????:11*********************@u72g2000cwu.google groups.com...
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]


May 12 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.