Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

do i need a custom control? 1

Status
Not open for further replies.

skitty123

Technical User
May 4, 2004
56
US
I have a couple of asp .net page which request a user to input a start month and year
I want to create drop dopwn boxes for month with value Jan through Dec and for year: 2004, 2005, 2006 ... etc

I have never created custom controls ( only user controls) Will crateing a custom control with these 2 drop downs be useful in this case? The idea is that I can use these on any page whjere I need user to input a date
But I want to be able to store the selected values and pass it back to the server to process further.

If no , any alternatives, if yes any pointers to an example?

thx
 
I would do this in a simple Web User Control (composite control), where you start with an ascx and add the actual dropdownlists while designing, and create a code-behind class as ususal. I think this would be much simpler than trying to create the dropdownlists dynamically in the Render method, especially since you want the viewstate for postback.



Greetings,
Dragonwell
 
If you only need the control on a couple pages, then the user control would be the way to go. You can also expose properties of the user control to the parent page.

For example, let's say the user control class name is MyProject.UserControl1.

In your UserControl1 class, you want a Month property. So, declare a public int (or whatever datatype). Then, you want to add that user control to an .aspx page (Page1.aspx). The control's added... Now what? How do you get the property value of the user control?

Here's kinda how the classes would look (these are hand-coded, so please excuse any syntax errors):

Page1.aspx
Code:
private int GetMonth()
{
    return ((MyProject.UserControl1)this.UserControl1).Month;
}

UserControl1.ascx
Code:
public int Month;

public UserControl1()
{
    this.Month = 1; // Set it to January
}

There are, of course, better ways to handle this (this method will require autopostback to be set on the dropdowns... If you want to avoid postbacks, then you'll be looking at some pretty ugly javascript to get the values from the usercontrol.

Hope this is pretty much what you're looking for and that I'm not waaay off base (haven't had my post-lunch coffee yet ;)).

-----------------------------------------------
"The night sky over the planet Krikkit is the least interesting sight in the entire universe."
-Hitch Hiker's Guide To The Galaxy
 
Thanks!!!
I was not sure how to get a value back from a user control, but thats to your post I do now.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top