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

  • Thread starter Thread starter planetthoughtful
  • Start date Start date
P

planetthoughtful

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,

planetthoughtful
 
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.SelectedValue, dd2.SelectedValue,
dd3.SelectedValue,
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.SelectedDate 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
 
Hi,

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

Regards,

Philip Hristov.

planetthoughtful напиÑа:
 
Steven said:
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.SelectedValue, dd2.SelectedValue,
dd3.SelectedValue,
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.SelectedDate 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,

planetthoughtful
 
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.btnSubmit" instead.

C# is good for autocreating your delegate. If you just typed:
mycontrol.btnSubmit.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 SubmitButtonClickedHandler();
public SubmitButtonClickedHandler SubmitButtonClicked;

Then in your control's btnSubmite_Click method, you can raise the event
"SubmitButtonClicked" by calling it like you would call any method.
Then in your main form you attach a handler in Page_load like:
myControl.SubmitButtonClicked += ... 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
 
Back
Top