473,856 Members | 1,666 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

ASP.NET: Getting value back from web user control?

Hi All,

I have a relatively simple web user control I've included in a page
that simply presents 3 drop down lists and a submit button, that I use
as a date picker (ie one list for day, one for month, one for year).

I have the control registered on the page and it displays fine, but I
haven't managed to work out how to get the value(s) back from the
control on clicking the submit button (performs a postback). I want to
use the date value in the select statement of an SqlCommand.

Can anyone give me advice on how I get the value selected in the user
control back into the main page so I can use it from that point?

Any help appreciated!

Much warmth,

planetthoughtfu l
---
"Lost in thought"
http://www.planetthoughtful.org

Jun 1 '06 #1
4 7800
You can have a property in your control that exposes the values as a
date time.
Eg.
public DateTime SelectedDate {
get {
return new DateTime(dd1.Se lectedValue, dd2.SelectedVal ue,
dd3.SelectedVal ue,
0, 0, 0);
}
}

(Note casting is needed for selected values)

Then you can handle the Submit_click event in the MAIN page (not the
control), as long as you have made your button public (you could always
create your own event to get raised when the button gets clicked as
well).

In your code to handle the raised event, you simply call
myControl.Selec tedDate to get the date of what the user has selected.

Obviously you would need to code for validation and so on.

Hope it helps, Steven


planetthoughtfu l wrote:
Hi All,

I have a relatively simple web user control I've included in a page
that simply presents 3 drop down lists and a submit button, that I use
as a date picker (ie one list for day, one for month, one for year).

I have the control registered on the page and it displays fine, but I
haven't managed to work out how to get the value(s) back from the
control on clicking the submit button (performs a postback). I want to
use the date value in the select statement of an SqlCommand.

Can anyone give me advice on how I get the value selected in the user
control back into the main page so I can use it from that point?

Any help appreciated!

Much warmth,

planetthoughtfu l
---
"Lost in thought"
http://www.planetthoughtful.org


Jun 1 '06 #2
Hi,

Why do not you change the scope of the dropdowns to 'public' and the
access their fields like this MyUserControl.D ropDown....?

Regards,

Philip Hristov.

planetthoughtfu l написа:
Hi All,

I have a relatively simple web user control I've included in a page
that simply presents 3 drop down lists and a submit button, that I use
as a date picker (ie one list for day, one for month, one for year).

I have the control registered on the page and it displays fine, but I
haven't managed to work out how to get the value(s) back from the
control on clicking the submit button (performs a postback). I want to
use the date value in the select statement of an SqlCommand.

Can anyone give me advice on how I get the value selected in the user
control back into the main page so I can use it from that point?

Any help appreciated!

Much warmth,

planetthoughtfu l
---
"Lost in thought"
http://www.planetthoughtful.org


Jun 1 '06 #3

Steven Nagy wrote:
You can have a property in your control that exposes the values as a
date time.
Eg.
public DateTime SelectedDate {
get {
return new DateTime(dd1.Se lectedValue, dd2.SelectedVal ue,
dd3.SelectedVal ue,
0, 0, 0);
}
}

(Note casting is needed for selected values)

Then you can handle the Submit_click event in the MAIN page (not the
control), as long as you have made your button public (you could always
create your own event to get raised when the button gets clicked as
well).

In your code to handle the raised event, you simply call
myControl.Selec tedDate to get the date of what the user has selected.

Obviously you would need to code for validation and so on.

Hope it helps, Steven


It did indeed!

Thanks Steven - I'm still a little way off from creating my own events
(or even knowing where to start with that), but creating the public
property was the element I was missing on being able to use the values
from the drop down list controls.

Interestingly enough, my page seems to handle the click event for the
button being in the user control rather than in the main page. But,
just so I fully understand what you mean, can you (if you have time)
give me an example of the syntax I'd include in my main page to be able
to react to a button click in a user control?

Thanks again!

Much warmth,

planetthoughtfu l
---
"Lost in thought"
http://www.planetthoughtful.org

Jun 1 '06 #4
Sure. It works exactly the same. I'm writing this off my head right now
because I don't have a C# IDE in front of me, so it will be
generalised.

I am guessing you created your button click event handler method by
double clicking the button in designer mode. If you view the contents
of the region that holds the Windows Designer Generated Code, then you
can find where your button is declared and properties are set. You
should also see some syntax like this:
btnSubmit.Click += new ....

Thats assigning a delegate to handle your event. So really what you
need to do is copy this code and paste it in the Page_Load event of
your main form. Except that btnSubmit doesn't exist in that scope, so
you need to put something like "mycontrol.btnS ubmit" instead.

C# is good for autocreating your delegate. If you just typed:
mycontrol.btnSu bmit.Click +=
then it should autocomplete the code for you (it asks you to press TAB
I think to complete it). Then it also auto creates the delegate method
as well if you press tab again. So this way you don't even have to
concern yourself with the delegate's signature.

The only thing I am concerned about from here is the SCOPE of
btnSubmit. I think by default it is generated as "protected" so you may
need to change this in your control to public.

Of course the other way is to create your own events. Something like:
public event void SubmitButtonCli ckedHandler();
public SubmitButtonCli ckedHandler SubmitButtonCli cked;

Then in your control's btnSubmite_Clic k method, you can raise the event
"SubmitButtonCl icked" by calling it like you would call any method.
Then in your main form you attach a handler in Page_load like:
myControl.Submi tButtonClicked += ... press TAB twice!

Events are well documented in MSN.

And then, and then, and then, and then........No more "and then" !!!

Disclaimer: My concepts based around .NET1.1 but should work fine in
2.0

Steve
planetthoughtfu l wrote:
Steven Nagy wrote:
You can have a property in your control that exposes the values as a
date time.
Eg.
public DateTime SelectedDate {
get {
return new DateTime(dd1.Se lectedValue, dd2.SelectedVal ue,
dd3.SelectedVal ue,
0, 0, 0);
}
}

(Note casting is needed for selected values)

Then you can handle the Submit_click event in the MAIN page (not the
control), as long as you have made your button public (you could always
create your own event to get raised when the button gets clicked as
well).

In your code to handle the raised event, you simply call
myControl.Selec tedDate to get the date of what the user has selected.

Obviously you would need to code for validation and so on.

Hope it helps, Steven


It did indeed!

Thanks Steven - I'm still a little way off from creating my own events
(or even knowing where to start with that), but creating the public
property was the element I was missing on being able to use the values
from the drop down list controls.

Interestingly enough, my page seems to handle the click event for the
button being in the user control rather than in the main page. But,
just so I fully understand what you mean, can you (if you have time)
give me an example of the syntax I'd include in my main page to be able
to react to a button click in a user control?

Thanks again!

Much warmth,

planetthoughtfu l
---
"Lost in thought"
http://www.planetthoughtful.org


Jun 2 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
5506
by: tshad | last post by:
I posted this on the asp.net group, also. I wasn't sure whether this was an asp.net problem or a javascript problem. I have a page that was originally created from a program I found on the net that works well as an html page. It brings up a modal popup window that I have been trying to work out for days now and this was the closest I have been able to come. I added a little asp.net code and an asp.net button and cannot get it to
1
1781
by: .Net Newbie | last post by:
I am relatively new to .Net and have been coding an intranet site for my employer for a couple of months. I am currently stuck coding in a text-editor called EditPlus without access to the VS.Net IDE, and coding pages with the script directly in each page (not using the codebehind method) as most of my learning material has demonstrated. I started with the IBuySpy Portal (SDK version not VS Version) as my base portal and have coded...
8
3126
by: Brendan Reynolds | last post by:
I'm trying to integrate SQL Server Reporting Services reports into an ASP.NET app (SRS 2000, ASP.NET 1.1). I know how to do this using direct URL addressing, but this exposes in the query string parameters that should not be exposed. Each user is associated with a school, and should see only that school's data. When the user logs in, I retrieve the SchoolID associated with that user, and that SchoolID is used as a parameter in all stored...
5
5996
by: Nathan Sokalski | last post by:
I have a user control that contains three variables which are accessed through public properties. They are declared immediately below the "Web Form Designer Generated Code" section. Every time an event is fired by one of the controls contained in the User Control, these variable are reset. Here is my current code (I have a little more to add later, right now I am just concerned about the variables getting reset): Public Class DatePicker2...
1
1617
by: .NET Developer | last post by:
I'm having an issue that hopefully someone here can help me out with. First a quick explanation: I'm managing users of my site in a fairly custom way. (in other words I'm not using asp.net's built-in methods). I have my own database table for user info, and people can register on the site, and then log in. If they haven't logged in, they are considered a guest and I store their name as "guest" in a sessions table along with some other info...
13
1411
by: dm1608 | last post by:
I know all the hype right now from Microsoft is how much easier, faster, and less code ASP.NET 2.0 provides over previous versions. I'm puzzled by this as I could turn out an classic ASP webpage in a few hours to query a database, display a grid/table, and not have much to worry about. Doing the same tasks in ASP.NET 2.0 seems almost trivial, but is it really real-world? I have a classic ASP app that I wrote a few years back that took...
14
4871
by: Anoop | last post by:
Hi, I am new to this newsgroup and need help in the following questions. 1. I am workin' on a GUI application. Does C# provides Layout Managers the way Java does to design GUI? I know that it can be done using the designer but I intentionally don't want to use that. The one reason is that you cannot change the code generated by the designer. The other could be that you have more free hand and control to design your GUI. 2....
0
3675
by: shamirza | last post by:
When was .NET announced? Bill Gates delivered a keynote at Forum 2000, held June 22, 2000, outlining the .NET 'vision'. The July 2000 PDC had a number of sessions on .NET technology, and delegates were given CDs containing a pre-release version of the .NET framework/SDK and Visual Studio.NET. When was the first version of .NET released? The final version of the 1.0 SDK and runtime was made publicly available around 6pm PST on...
0
3850
by: shamirza | last post by:
What is view state and use of it? The current property settings of an ASP.NET page and those of any ASP.NET server controls contained within the page. ASP.NET can detect when a form is requested for the first time versus when the form is posted (sent to the server), which allows you to program accordingly. What are user controls and custom controls? Custom controls: A control authored by a user or a third-party software vendor that...
0
9920
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
9764
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11063
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10790
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9535
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7932
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7098
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5960
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4176
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.