By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,625 Members | 1,283 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,625 IT Pros & Developers. It's quick & easy.

Accessing the instances of the web user control

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.