Problem accessing UserControl properties from ASP.NET Web Form CodeBehind

J

jdn

I have a simple problem that probably requires a simple solution but I
am obviously overlooking something.

I have a Address form on a ASP.NET page that include a state field. I
created a simple user control that is a drop down list of all 50 states
plus DC.

On a certain function (basically calling OnItemCommand from a datagrid),
I bind various textboxes and labels to the values pulled from a stored
procedure and they all work fine. But the User Control never gets the
proper value.

The user control has a public property that should set the selected
value for that dropdownlist such as:

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

and then I try to bind the value as such in the page it is embedded in:

_DDLStateControl.SelectedValue = myDataRow["State"].ToString();

In debugging mode, the myDataRow value is correct, but it doesn't
acutally set the selected value at all.

I reference the User Control in the page itself with
TagPrefix="UserControl" and TagName="StateList"

I'm wondering if I should be doing something like
UserControl:Statelist.SelectedValue = blah

but I don't know what the correct syntax is.

TIA
jdn
(e-mail address removed)
 
J

Jacob Yang [MSFT]

Hi Jdn,

I have reviewed your issue. Due to the nature of your issue I need to do
additional research to determine the best way to provide assistance. I will
contact you as soon as possible.

Best regards,

Jacob Yang
Microsoft Online Partner Support
<MCSD>
Get Secure! ¨C www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
J

jdn

jdn said:
I have a simple problem that probably requires a simple solution but I
am obviously overlooking something.

I have a Address form on a ASP.NET page that include a state field. I
created a simple user control that is a drop down list of all 50 states
plus DC.

On a certain function (basically calling OnItemCommand from a datagrid),
I bind various textboxes and labels to the values pulled from a stored
procedure and they all work fine. But the User Control never gets the
proper value.

The user control has a public property that should set the selected
value for that dropdownlist such as:

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

and then I try to bind the value as such in the page it is embedded in:

_DDLStateControl.SelectedValue = myDataRow["State"].ToString();

In debugging mode, the myDataRow value is correct, but it doesn't
acutally set the selected value at all.

I reference the User Control in the page itself with
TagPrefix="UserControl" and TagName="StateList"

I'm wondering if I should be doing something like
UserControl:Statelist.SelectedValue = blah

but I don't know what the correct syntax is.

TIA
jdn
(e-mail address removed)

Okay, I was able to fix the easy syntax part.

In the Page itself, the user control is given an id = "ucStateListControl"

So, in the code behind for the page, I can declare it like any other web
control:

protected _DDLStateListControl ucStateListControl;

and that works.

The problem now is I am getting a "specified argument was out of the
range of valid values" when I try to set the SelectedValue to "IL", even
though I have this ListItem in the DropDownList:

<asp:ListItem Value="IL" >IL</asp:ListItem>

I'm not sure why that is.

jdn
 
J

jdn

jdn said:
jdn said:
I have a simple problem that probably requires a simple solution but I
am obviously overlooking something.

I have a Address form on a ASP.NET page that include a state field. I
created a simple user control that is a drop down list of all 50
states plus DC.

On a certain function (basically calling OnItemCommand from a
datagrid), I bind various textboxes and labels to the values pulled
from a stored procedure and they all work fine. But the User Control
never gets the proper value.

The user control has a public property that should set the selected
value for that dropdownlist such as:

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

and then I try to bind the value as such in the page it is embedded in:

_DDLStateControl.SelectedValue = myDataRow["State"].ToString();

In debugging mode, the myDataRow value is correct, but it doesn't
acutally set the selected value at all.

I reference the User Control in the page itself with
TagPrefix="UserControl" and TagName="StateList"

I'm wondering if I should be doing something like
UserControl:Statelist.SelectedValue = blah

but I don't know what the correct syntax is.

TIA
jdn
(e-mail address removed)

Okay, I was able to fix the easy syntax part.

In the Page itself, the user control is given an id = "ucStateListControl"

So, in the code behind for the page, I can declare it like any other web
control:

protected _DDLStateListControl ucStateListControl;

and that works.

The problem now is I am getting a "specified argument was out of the
range of valid values" when I try to set the SelectedValue to "IL", even
though I have this ListItem in the DropDownList:

<asp:ListItem Value="IL" >IL</asp:ListItem>

I'm not sure why that is.

jdn

Fixed it.

Instead of trying to set the SelectedValue, I changed it to set the
SelectedItem.Text to what I was getting data from, and it worked just fine.

jdn
 

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

Forum statistics

Threads
474,077
Messages
2,570,567
Members
47,203
Latest member
EmmaSwank1

Latest Threads

Top