Problem accessing UserControl properties from ASP.NET Web Form CodeBehind

Discussion in 'ASP .Net Web Controls' started by jdn, Sep 17, 2003.

  1. jdn

    jdn Guest

    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
     
    jdn, Sep 17, 2003
    #1
    1. Advertising

  2. RE: Problem accessing UserControl properties from ASP.NET Web Form Code Behind

    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.
     
    Jacob Yang [MSFT], Sep 17, 2003
    #2
    1. Advertising

  3. jdn

    jdn Guest

    Re: Problem accessing UserControl properties from ASP.NET Web FormCode Behind

    jdn wrote:

    > 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
    >
    >


    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
     
    jdn, Sep 17, 2003
    #3
  4. jdn

    jdn Guest

    Re: Problem accessing UserControl properties from ASP.NET Web FormCode Behind

    jdn wrote:

    > jdn wrote:
    >
    >> 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
    >>
    >>

    >
    > 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
     
    jdn, Sep 18, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John
    Replies:
    2
    Views:
    570
  2. Mat
    Replies:
    4
    Views:
    378
  3. Phl
    Replies:
    2
    Views:
    546
    =?Utf-8?B?VGFtcGEgLk5FVCBLb2Rlcg==?=
    Nov 18, 2004
  4. protic
    Replies:
    0
    Views:
    364
    protic
    Oct 15, 2008
  5. Kan Grewal

    Web Usercontrol in a Web Usercontrol not displaying...

    Kan Grewal, Jul 6, 2004, in forum: ASP .Net Web Controls
    Replies:
    2
    Views:
    127
    Kan Grewal
    Jul 7, 2004
Loading...

Share This Page