472,805 Members | 807 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Accessing the instances of the web user control

Hi,

I have created a dropdownlist as a web user control. I am using its
multiple instances on the webpage. How do I access the selectedValue of
each instance? All the instances have different IDs.

Thanks

Vivek
Nov 17 '05 #1
3 2739
Hi Vivek,

The proper way to access control members is through a public interface that
encapsulates access. Add a property to your User Control that delegates to
the DropDownList, something like this:

public string SelectedValue
{
get
{
return DropDownList1.SelectedValue;
}
set
{
DropDownList1.SelectedValue = value;
}
}

Then on your Web Form, add a reference to each control, naming the variable
the same as the id you used for each user control on the page, something
like this:

protected WebUserControl1 WebUserControl11;
WebUserControl is the name of the User Control type you created.
WebUserControl11 is the id of a control of type WebUserControl1 on the Web
Form.

You can then access the DropDownList control on the user control through the
public property, via your Web Form, something like this:

string selectedVal = WebUserControl11.SelectedValue;

Joe
--
http://www.csharp-station.com

"Vivek Sharma" <jo*@bloggs.com> wrote in message
news:uM*************@TK2MSFTNGP12.phx.gbl...
Hi,

I have created a dropdownlist as a web user control. I am using its
multiple instances on the webpage. How do I access the selectedValue of
each instance? All the instances have different IDs.

Thanks

Vivek

Nov 17 '05 #2
Thanks Joe,

I am not sure where am I failing ...

This is what I tried

My web user control filename: uctlRating.ascx contains dropdownlist
As mentioned I created a public interface.

I declared the contol at the top of the HTML
<%@ Register TagPrefix="ucRating" TagName="Rating" src="...">

In HTML I am accessing the instances of the web control

<ucRating:Rating id="Rating1" runat="server"></ucRating:Rating>
<ucRating:Rating id="Rating2" runat="server"></ucRating:Rating>

---------------------------------------------------------------------------------------------

My aspx.cs looks like this

protected System.Web.UI.WebControls.DropDownList Rating1

and later I am accessing

string s = Rating1.SelectedValue.ToString();

-------------------------------------------------------
The error I get ..... The base class includes the field RatingQ1, buts its
type(System.Web.UI.WebControls.DropDownList) is not compatible with the type
of Control (ASP.uctlRating_acx)
"Joe Mayo" <jm***@nospamAtCSharpDashStation.com> wrote in message
news:%2***************@TK2MSFTNGP12.phx.gbl...
Hi Vivek,

The proper way to access control members is through a public interface
that encapsulates access. Add a property to your User Control that
delegates to the DropDownList, something like this:

public string SelectedValue
{
get
{
return DropDownList1.SelectedValue;
}
set
{
DropDownList1.SelectedValue = value;
}
}

Then on your Web Form, add a reference to each control, naming the
variable the same as the id you used for each user control on the page,
something like this:

protected WebUserControl1 WebUserControl11;
WebUserControl is the name of the User Control type you created.
WebUserControl11 is the id of a control of type WebUserControl1 on the Web
Form.

You can then access the DropDownList control on the user control through
the public property, via your Web Form, something like this:

string selectedVal = WebUserControl11.SelectedValue;

Joe
--
http://www.csharp-station.com

"Vivek Sharma" <jo*@bloggs.com> wrote in message
news:uM*************@TK2MSFTNGP12.phx.gbl...
Hi,

I have created a dropdownlist as a web user control. I am using its
multiple instances on the webpage. How do I access the selectedValue of
each instance? All the instances have different IDs.

Thanks

Vivek


Nov 17 '05 #3
The message is correct because your control type is not DropDownList - its
type is Rating. If you examine the code-behind for the user control, you'll
find that its type is defined as:

public class Rating: System.Web.UI.UserControl
{
....
}

which means that it should be defined as a field in the class of the
code-behind of your Web Form like this:

protected <your namespace>.Rating Rating1

where <your namespace> should be replaced with the namespace that the User
Control code-behind class, Rating, is defined in to fully qualify the
declaration.

Then use the property access technique I showed you to encapsulate access to
the DropDownList control.

Joe
--
http://www.csharp-station.com

"Vivek Sharma" <jo*@bloggs.com> wrote in message
news:Of**************@TK2MSFTNGP10.phx.gbl...
Thanks Joe,

I am not sure where am I failing ...

This is what I tried

My web user control filename: uctlRating.ascx contains dropdownlist
As mentioned I created a public interface.

I declared the contol at the top of the HTML
<%@ Register TagPrefix="ucRating" TagName="Rating" src="...">

In HTML I am accessing the instances of the web control

<ucRating:Rating id="Rating1" runat="server"></ucRating:Rating>
<ucRating:Rating id="Rating2" runat="server"></ucRating:Rating>

---------------------------------------------------------------------------------------------

My aspx.cs looks like this

protected System.Web.UI.WebControls.DropDownList Rating1

and later I am accessing

string s = Rating1.SelectedValue.ToString();

-------------------------------------------------------
The error I get ..... The base class includes the field RatingQ1, buts its
type(System.Web.UI.WebControls.DropDownList) is not compatible with the
type of Control (ASP.uctlRating_acx)
"Joe Mayo" <jm***@nospamAtCSharpDashStation.com> wrote in message
news:%2***************@TK2MSFTNGP12.phx.gbl...
Hi Vivek,

The proper way to access control members is through a public interface
that encapsulates access. Add a property to your User Control that
delegates to the DropDownList, something like this:

public string SelectedValue
{
get
{
return DropDownList1.SelectedValue;
}
set
{
DropDownList1.SelectedValue = value;
}
}

Then on your Web Form, add a reference to each control, naming the
variable the same as the id you used for each user control on the page,
something like this:

protected WebUserControl1 WebUserControl11;
WebUserControl is the name of the User Control type you created.
WebUserControl11 is the id of a control of type WebUserControl1 on the
Web Form.

You can then access the DropDownList control on the user control through
the public property, via your Web Form, something like this:

string selectedVal = WebUserControl11.SelectedValue;

Joe
--
http://www.csharp-station.com

"Vivek Sharma" <jo*@bloggs.com> wrote in message
news:uM*************@TK2MSFTNGP12.phx.gbl...
Hi,

I have created a dropdownlist as a web user control. I am using its
multiple instances on the webpage. How do I access the selectedValue of
each instance? All the instances have different IDs.

Thanks

Vivek



Nov 17 '05 #4

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

Similar topics

3
by: ram | last post by:
I have a user control uc (with a dropdownlist uc_dd) that I dynamically add to a pane on a button_click. this pane is hosted on the winform. now when I add two instances of the user control...
4
by: John Holmes | last post by:
I'm using data to rename some web controls on a form that uses a repeater contol and so it can have mulitple instances of the same control set. The controls get renamed (thanks to Steven Cheng's...
1
by: Sammy | last post by:
When a user control access SQL Server, what happens if I host it multiple times on one webpage (create several instances of that user control), in terms of trips being made to the server? How can I...
0
by: clintonG | last post by:
I applied aspnet_regsql to SQL2K which was working fine throughout Beta 2 development. After installing Visual Studio and SQL Express RTM my application has blown up. Logging in to the application...
6
by: | last post by:
I have made some user controls with custom properties. I can set those properties on instances of my user controls, and I have programmed my user control to do useful visual things in response to...
7
by: =?Utf-8?B?Li46OiBLZXZpbiA6Oi4u?= | last post by:
I have a problem with accessing controls that I have loaded dynamically and added to a web page. The scenario: I have a webpage that displays multiple instances of a user control on the page. ...
1
by: glennelong | last post by:
Problem - Accessing each member of the custom control. I have a custom control which I created to hold 4 radio buttons. I have added 3 instances of the control to my application form. I can access...
4
by: nottarealaddress | last post by:
I'm trying to get my feet wet in VB2005 (our new standard at work after officially stopping new development in VB6 about a month ago). I'm working with a simple sql 2005 table of 50 entries, one...
8
by: GaryDean | last post by:
I have a Wizard page and need to affect the next and previous buttons from my code-behind. I've googled around and found two solutions, and neither appear to work. I can access the SideBarList...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.