Solved

Using an ASP.NET User Control

Posted on 2012-12-20
6
354 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

685 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