Solved

Using an ASP.NET User Control

Posted on 2012-12-20
6
346 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

Author Comment

by:dyarosh
Comment Utility
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
Comment Utility
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
Comment Utility
Thank you.  I didn't even realize I was missing that until you pointed it out.  So dumb!!!
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

AJAX ModalPopupExtender has a required property "TargetControlID" which may seem to be very confusing to new users. It means the server control that will be extended by the ModalPopup, for instance, if when you click a button, a ModalPopup displays,…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

762 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

7 Experts available now in Live!

Get 1:1 Help Now