473,394 Members | 1,831 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Accessing input values across controls

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.

Apr 9 '07 #1
4 1452
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.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA
http://gregorybeamer.spaces.live.com

*********************************************
Think outside the box!
*********************************************
"Greg Stevens" <Gr***********@gmail.comwrote in message
news:E1**********************************@microsof t.com...
>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.
Apr 9 '07 #2
"Cowboy (Gregory A. Beamer)" wrote:
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.

Apr 9 '07 #3
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" <Gr***********@gmail.comwrote in message
news:E6**********************************@microsof t.com...
"Cowboy (Gregory A. Beamer)" wrote:
>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.

Apr 9 '07 #4
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" wrote:
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" <Gr***********@gmail.comwrote in message
news:E6**********************************@microsof t.com...
"Cowboy (Gregory A. Beamer)" wrote:
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.


Apr 9 '07 #5

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

Similar topics

3
by: Tom Meuzelaar | last post by:
Hello: I'm using VB6 in VS enterprise. I'd like to place an HTML form inside a VB container, have a user fill out the form information, click a submit button, and then have the program capture...
1
by: Neil Zanella | last post by:
Hello, I have a bunch of HTML input controls which I need to generate from within a for loop. I would like them to have id attributes set to values foo1, foo2, foo3, foo4, etc... ...
1
by: CS Wong | last post by:
Hi, I have a page form where form elements are created dynamically using Javascript instead of programatically at the code-behind level. I have problems accessing the dynamically-created...
3
by: Ali | last post by:
I have 3 html input tex in my asp.net form. Two of them are calling javascript client side to calculate the differnce of two dates and put the result into the third input text. i haven't include...
3
by: Dotnet Gruven | last post by:
I've built a WebForm with a Table added dynamically in Page_Load when IsPostBack is false. The table includes a couple of TextBoxes, RadioButtonLists and CheckboxLists. On postback, those...
4
by: raj_genius | last post by:
I hav two queries, whc are as follows: FIRSTLY: is it possible to access the controls(by name) of a parent form(MDI) from its child forms??if yes then how??plzz provide a coded example in VB if...
6
by: Chuck Anderson | last post by:
My knowledge of JavaScript is limited. I learn from example and then adapt those examples to suit my needs. I have stumped myself on this one. I have a form with checkboxes that I want to...
3
by: Nathan Sokalski | last post by:
I have a validator that I wrote by inheriting from BaseValidator. At certain points in the code, I need to access other controls on the page containing the validator. I have the IDs of these...
3
by: jeroen.bolle | last post by:
I'm designing a form where the user can decide how many options for a poll he wants to add. There's a "+" button to dynamically add textboxes with javascript, without a postback. How do I access...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...

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.