Accessing input values across controls

G

Guest

I have an ASP.NET page with a form that contains two user controls:

<%@ Register TagPrefix="x" TagName="c1" Src="ctl1.ascx" %>
<%@ Register TagPrefix="x" TagName="c2" Src="ctl2.ascx" %>
<body>
<form runat="Server">
<x:c1 id="con1" runat="Server">
<x:c2 id="con2" runat="Server">
<form>
</body>

In one control (con1) I have a dropdownlist. In the other control (con2), I
have a set of textboxes that I would like to pre-populate with values that
depend on what items is selected in the dropdownlist in the first control.

How does the code for con2 access the value of the dropdownlist in con1?

Thanks for any help or suggestions.
 
C

Cowboy \(Gregory A. Beamer\)

You have three options:

1. Auto postback the DropDownList

This puts everything in your control, although it does cost a postback

2. Ajax

This makes it appear as if you are not round tripping, as only part of the
page is updated. This is the method I would most likely use, especially if
the dropdownlist had a large number of values

3. JavaScript

Embed the values in an array and do a lookup and update. With ASP.NET, it is
best to emit JavaScript and pull the control's client side name from code,
rather than hard code, as it is more maintainable.
 
G

Guest

Cowboy (Gregory A. Beamer) said:
You have three options:
1. Auto postback the DropDownList
[...]

I am using autopostback. My question is actually even more basic than this:
Once the form has posted back, how do I access the value of the dropdownlist
in control 1 from the code-behind page of control 2?

For example, suppose the dropdownlist is called MyDD. In the code-behind
page for control 2, I have tried accessing:

MyDD
Page.MyDD
Page.Con1.MyDD

and none of these seem to work.
 
E

Eric

Hi Greg,

I would have the page that is hosting the two controls handle this
interaction. You could have control 1 expose the currently-selected value
(in the dropdownlist) as a property (let's call the property
"SelectedMenuItemValue") and also fire an event when the DropDownList's
SelectedIndexChanged event fires (let's call the event MyMenuChanged).
Then, when this event handler is fired in the "main" page, the main page
could call some sort of custom RefreshMenu() method in control 2 that
accepts the value selected in control 1 as an argument.

The event in the page consuming the controls might look something like this
(example is in VB):

-----

Private Sub Con1_MyMenuChanged(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Con1.MyMenuChanged

Con2.RefreshMenu(Con1.SelectedMenuItemValue)

End Sub

-----

Does this help?


Regards,

Eric



Greg Stevens said:
Cowboy (Gregory A. Beamer) said:
You have three options:
1. Auto postback the DropDownList
[...]

I am using autopostback. My question is actually even more basic than
this:
Once the form has posted back, how do I access the value of the
dropdownlist
in control 1 from the code-behind page of control 2?

For example, suppose the dropdownlist is called MyDD. In the code-behind
page for control 2, I have tried accessing:

MyDD
Page.MyDD
Page.Con1.MyDD

and none of these seem to work.
 
G

Guest

Thank you. That did answer my question, although I've discovered a simpler
approach, using the Page.FindControl() method.

From within control 2, I can use

Dim MyControl as UserControl = Page.FindControl("con1")
Dim MyList as DropDownList = MyControl.FindControl("ddl1")

And then I can access the drop down list properties from there.



Eric said:
Hi Greg,

I would have the page that is hosting the two controls handle this
interaction. You could have control 1 expose the currently-selected value
(in the dropdownlist) as a property (let's call the property
"SelectedMenuItemValue") and also fire an event when the DropDownList's
SelectedIndexChanged event fires (let's call the event MyMenuChanged).
Then, when this event handler is fired in the "main" page, the main page
could call some sort of custom RefreshMenu() method in control 2 that
accepts the value selected in control 1 as an argument.

The event in the page consuming the controls might look something like this
(example is in VB):

-----

Private Sub Con1_MyMenuChanged(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Con1.MyMenuChanged

Con2.RefreshMenu(Con1.SelectedMenuItemValue)

End Sub

-----

Does this help?


Regards,

Eric



Greg Stevens said:
Cowboy (Gregory A. Beamer) said:
You have three options:
1. Auto postback the DropDownList
[...]

I am using autopostback. My question is actually even more basic than
this:
Once the form has posted back, how do I access the value of the
dropdownlist
in control 1 from the code-behind page of control 2?

For example, suppose the dropdownlist is called MyDD. In the code-behind
page for control 2, I have tried accessing:

MyDD
Page.MyDD
Page.Con1.MyDD

and none of these seem to work.
 

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

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top