Solved

Using an ASP.NET User Control

Posted on 2012-12-20
6
351 Views
Last Modified: 2012-12-20
I am new to creating custom web controls and wanted to start with something simple so I am trying to create a DropDown List for State to be used on address forms.

I am able to set the initial value but when I change the value and try to retrieve it, I am getting the original value that the control was initialized to.

Here is my code for the control (StateUserControl.ascx)
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="StateUserControl.ascx.cs" Inherits="CVL.StateUserControl" %>
<asp:DropDownList ID="StateDropDownList" ClientIDMode="Static" runat="server">
    <asp:ListItem Value=""></asp:ListItem>
    <asp:ListItem Value="AL">Alabama</asp:ListItem>
    <asp:ListItem Value="AK">Alaska</asp:ListItem>
    <asp:ListItem Value="AZ">Arizona</asp:ListItem>
    <asp:ListItem value="AR">Arkansas</asp:ListItem>
    <asp:ListItem value="CA">California</asp:ListItem>
    <asp:ListItem value="CO">Colorado</asp:ListItem>
    <asp:ListItem value="CT">Connecticut</asp:ListItem>
    <asp:ListItem value="DE">Delaware</asp:ListItem>
    <asp:ListItem value="DC">District of Columbia</asp:ListItem>
    <asp:ListItem value="FL">Florida</asp:ListItem>
    <asp:ListItem value="GA">Georgia</asp:ListItem>
    <asp:ListItem value="HI">Hawaii</asp:ListItem>
    <asp:ListItem value="ID">Idaho</asp:ListItem>
    <asp:ListItem value="IL">Illinois</asp:ListItem>
    <asp:ListItem value="IN">Indiana</asp:ListItem>
    <asp:ListItem value="IA">Iowa</asp:ListItem>
    <asp:ListItem value="KS">Kansas</asp:ListItem>
    <asp:ListItem value="KY">Kentucky</asp:ListItem>
    <asp:ListItem value="LA">Louisiana</asp:ListItem>
    <asp:ListItem value="ME">Maine</asp:ListItem>
    <asp:ListItem value="MD">Maryland</asp:ListItem>
    <asp:ListItem value="MA">Massachusetts</asp:ListItem>
    <asp:ListItem value="MI">Michigan</asp:ListItem>
    <asp:ListItem value="MN">Minnesota</asp:ListItem>
    <asp:ListItem value="MS">Mississippi</asp:ListItem>
    <asp:ListItem value="MO">Missouri</asp:ListItem>
    <asp:ListItem value="MT">Montana</asp:ListItem>
    <asp:ListItem value="NE">Nebraska</asp:ListItem>
    <asp:ListItem value="NV">Nevada</asp:ListItem>
    <asp:ListItem value="NH">New Hampshire</asp:ListItem>
    <asp:ListItem value="NJ">New Jersey</asp:ListItem>
    <asp:ListItem value="NM">New Mexico</asp:ListItem>
    <asp:ListItem value="NY">New York</asp:ListItem>
    <asp:ListItem value="NC">North Carolina</asp:ListItem>
    <asp:ListItem value="ND">North Dakota</asp:ListItem>
    <asp:ListItem value="OH">Ohio</asp:ListItem>
    <asp:ListItem value="OK">Oklahoma</asp:ListItem>
    <asp:ListItem value="OR">Oregon</asp:ListItem>
    <asp:ListItem value="PA">Pennsylvania</asp:ListItem>
    <asp:ListItem value="RI">Rhode Island</asp:ListItem>
    <asp:ListItem value="SC">South Carolina</asp:ListItem>
    <asp:ListItem value="SD">South Dakota</asp:ListItem>
    <asp:ListItem value="TN">Tennessee</asp:ListItem>
    <asp:ListItem value="TX">Texas</asp:ListItem>
    <asp:ListItem value="UT">Utah</asp:ListItem>
    <asp:ListItem value="VT">Vermont</asp:ListItem>
    <asp:ListItem value="VA">Virginia</asp:ListItem>
    <asp:ListItem value="WA">Washington</asp:ListItem>
    <asp:ListItem value="WV">West Virginia</asp:ListItem>
    <asp:ListItem value="WI">Wisconsin</asp:ListItem>
    <asp:ListItem value="WY">Wyoming</asp:ListItem>
</asp:DropDownList>

Open in new window


Here is the code behind on the control:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace CVL
{
    public partial class StateUserControl : System.Web.UI.UserControl
    {
        public string StateName
        {
            get { return StateDropDownList.SelectedValue; }
            set 
            {
                StateDropDownList.ClearSelection();
                StateDropDownList.Items.FindByValue(value).Selected = true;
            }
        }
    }
}

Open in new window


Here is the code behind on my test page:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace CVL
{
    public partial class Test : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            StateName.StateName = "FL";
        }

        protected void SaveBTN_Click(object sender, EventArgs e)
        {
            StatusLBL.Text = StateName.StateName;
        }
    }
}

Open in new window


As you can see, I initialize the drop down to FL (Florida).  Florida displays on the screen as expected.

When I select a different state such as Delaware, the screen shows Delaware but when I click on Submit to display the selected value, I get FL.  I am expecting DE.  Can someone tell me what I am doing wrong?
0
Comment
Question by:dyarosh
  • 3
  • 3
6 Comments
 

Author Comment

by:dyarosh
ID: 38710208
I've narrowed the problem down to the foloowing code:

StateDropDownList.Items.FindByValue(value).Selected = true

For some reason, this doens't allow me to get a differenct selected value.
0
 
LVL 3

Expert Comment

by:Andy Green
ID: 38710327
The problem is that the user control is distroyed on postback along with any value.

I had a similar problem recently and these links helped.

http://stackoverflow.com/questions/5425679/findcontrol-wont-find-my-dynamically-added-usercontrol


http://www.codeproject.com/Articles/59781/Dynamic-Loading-of-ASP-NET-User-Controls

Andy
0
 
LVL 3

Expert Comment

by:Andy Green
ID: 38710333
This is the original thead I posted, there are some other clues in there too.

http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/Q_27968007.html

Andy
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 

Author Comment

by:dyarosh
ID: 38710435
Thanks for the links but I am not loading my controls dynamically.  All the controls are placed on the web page and are static.  If I don't try and initialize the value of the DropDownList then everything works fine.  It is only when I try and initialize the value that I run into the problem.
0
 
LVL 3

Accepted Solution

by:
Andy Green earned 500 total points
ID: 38710459
The first part of my thread was concerning statically loading controls, make sure you have an if not postback test so the control only initialises on first load.

If not ispostback then
    StateName.StateName = "FL"
end

I know its VB that the way I role.

Andy
0
 

Author Comment

by:dyarosh
ID: 38710853
Thank you.  I didn't even realize I was missing that until you pointed it out.  So dumb!!!
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now