Solved

Using an ASP.NET User Control

Posted on 2012-12-20
6
356 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 

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

Independent Software Vendors: 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!

Question has a verified solution.

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

Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

691 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