[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Using an ASP.NET User Control

Posted on 2012-12-20
6
Medium Priority
?
358 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
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!

 

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

656 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