Link to home
Start Free TrialLog in
Avatar of mathieu_cupryk
mathieu_cuprykFlag for Canada

asked on

Easy question how to get a value of a dropdownlist user control.

I created the following:
<add tagPrefix="KORE" tagName="uxLookupFieldTicketStatus" src="~/Controls/Lookup/uxLookupFieldTicketStatus.ascx" />
            
in my web config.

I want to access the dropdownlist value. and assign it.
protected void btnSaveExit_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                // Instantiate business object
                BLL.Tickets tickets = new BLL.Tickets();
                tickets.TicketType = ???

            }
        }

where the
<td class="TicketLabel"><asp:Label ID="lblType" runat="server" Text="Type:" Font-Bold="True"></asp:Label></td>
                                        <td class="TicketLabel">
                                        <KORE:uxLookupFieldTicketType runat="server" ID="uxLookupFieldTicketType1"
                                    KeyField="TicketType" ValueField="ID" />
                                    </td>                  

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="uxLookupFieldTicketStatus.ascx.cs" Inherits="KORE.SIDWebClient.UI.Controls.uxLookupFieldTicketStatus" %>
 
<asp:DropDownList ID="LookupFieldTicketStatusDropDown" AppendDataBoundItems="true" Width="200px" runat="server">
<asp:ListItem Value=""></asp:ListItem>
</asp:DropDownList><br />
<asp:RequiredFieldValidator id="CVLookupFieldTicketStatusDropDown"
        Text="(Required)"
        InitialValue=""
        ControlToValidate="LookupFieldTicketStatusDropDown"
        Runat="server" />
====================================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using KORE.SIDWebClient.BLL;
using System.ComponentModel;
 
namespace KORE.SIDWebClient.UI.Controls
{
    public partial class uxLookupFieldTicketStatus : System.Web.UI.UserControl
    {
        private string _keyField;
        private string _valueField;
        private string _defaultValue;
 
        public uxLookupFieldTicketStatus()
        {
            
        }
 
        protected void Page_Load(object sender, EventArgs e)
        {
            LookupFieldTicketStatus lookup = new LookupFieldTicketStatus();
 
            this.LookupFieldTicketStatusDropDown.Items.AddRange(lookup.getLookupListItemGroup(this.KeyField, this.ValueField));
 
            ListItem selected = this.LookupFieldTicketStatusDropDown.Items.FindByValue(this.DefaultValue);
 
            this.LookupFieldTicketStatusDropDown.SelectedIndex = this.LookupFieldTicketStatusDropDown.Items.IndexOf(selected);
        }
 
        [Category("Data"), Description("The dataset column that will be display field in the dropdown"), Browsable(true)]
        public string KeyField
        {
            get
            {
                return _keyField;
            }
            set
            {
                _keyField = value;
            }
        }
 
        [Category("Data"), Description("The dataset column that will be value field in the dropdown"), Browsable(true)]
        public string ValueField
        {
            get
            {
                return _valueField;
            }
            set
            {
                _valueField = value;
            }
        }
 
        public string DefaultValue
        {
            get
            {
                return _defaultValue;
            }
            set
            {
                _defaultValue = value;
            }
        }
    }
}

Open in new window

Avatar of GiftsonDJohn
GiftsonDJohn
Flag of India image

Hi,

Similarly create another function as below

 [Category("Data"), Description("Selected Index of the DropDownList"), Browsable(true)]
        public string SelectedIndex
        {
            get
            {
                return LookupFieldTicketStatusDropDown.SelectedIndex;
            }          
        }

 [Category("Data"), Description("Selected Text of the DropDownList"), Browsable(true)]
        public string SelectedText
        {
            get
            {
                return LookupFieldTicketStatusDropDown.SelectedText;
            }          
        }

 [Category("Data"), Description("Selected Valueof the DropDownList"), Browsable(true)]
        public string SelectedValue
        {
            get
            {
                return LookupFieldTicketStatusDropDown.SelectedValue;
            }          
        }

Avatar of mathieu_cupryk

ASKER

Error      3      Cannot implicitly convert type 'int' to 'string'      C:\svn\SIDWEBMYBRANCH\code\KORE.SIDWebClient.UI\Controls\Lookup\uxLookupFieldTicketStatus.ascx.cs      38      24      KORE.SIDWebClient.UI

 [Category("Data"), Description("Selected Index of the DropDownList"), Browsable(true)]
        public string SelectedIndex
        {
            get
            {
                return LookupFieldTicketStatusDropDown.SelectedIndex;
            }
        }
=====================================================================

Error      5      'System.Web.UI.WebControls.DropDownList' does not contain a definition for 'SelectedText' and no extension method 'SelectedText' accepting a first argument of type 'System.Web.UI.WebControls.DropDownList' could be found (are you missing a using directive or an assembly reference?)      C:\svn\SIDWEBMYBRANCH\code\KORE.SIDWebClient.UI\Controls\Lookup\uxLookupFieldTicketStatus.ascx.cs      47      56      KORE.SIDWebClient.UI
[Category("Data"), Description("Selected Text of the DropDownList"), Browsable(true)]
        public string SelectedText
        {
            get
            {
                return LookupFieldTicketStatusDropDown.SelectedText;
            }
        }

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using KORE.SIDWebClient.BLL;
using System.ComponentModel;
 
namespace KORE.SIDWebClient.UI.Controls
{
    public partial class uxLookupFieldTicketStatus : System.Web.UI.UserControl
    {
        private string _keyField;
        private string _valueField;
        private string _defaultValue;
 
        public uxLookupFieldTicketStatus()
        {
            
        }
 
        protected void Page_Load(object sender, EventArgs e)
        {
            LookupFieldTicketStatus lookup = new LookupFieldTicketStatus();
 
            this.LookupFieldTicketStatusDropDown.Items.AddRange(lookup.getLookupListItemGroup(this.KeyField, this.ValueField));
 
            ListItem selected = this.LookupFieldTicketStatusDropDown.Items.FindByValue(this.DefaultValue);
 
            this.LookupFieldTicketStatusDropDown.SelectedIndex = this.LookupFieldTicketStatusDropDown.Items.IndexOf(selected);
        }
        [Category("Data"), Description("Selected Index of the DropDownList"), Browsable(true)]
        public string SelectedIndex
        {
            get
            {
                return LookupFieldTicketStatusDropDown.SelectedIndex;
            }
        }
 
        [Category("Data"), Description("Selected Text of the DropDownList"), Browsable(true)]
        public string SelectedText
        {
            get
            {
                return LookupFieldTicketStatusDropDown.SelectedText;
            }
        }
 
        [Category("Data"), Description("Selected Valueof the DropDownList"), Browsable(true)]
        public string SelectedValue
        {
            get
            {
                return LookupFieldTicketStatusDropDown.SelectedValue;
            }
        }
 
        [Category("Data"), Description("The dataset column that will be display field in the dropdown"), Browsable(true)]
        public string KeyField
        {
            get
            {
                return _keyField;
            }
            set
            {
                _keyField = value;
            }
        }
 
        [Category("Data"), Description("The dataset column that will be value field in the dropdown"), Browsable(true)]
        public string ValueField
        {
            get
            {
                return _valueField;
            }
            set
            {
                _valueField = value;
            }
        }
 
        public string DefaultValue
        {
            get
            {
                return _defaultValue;
            }
            set
            {
                _defaultValue = value;
            }
        }
    }
}

Open in new window

[Category("Data"), Description("Selected Index of the DropDownList"), Browsable(true)]
        public intSelectedIndex
        {
            get
            {
                return LookupFieldTicketStatusDropDown.SelectedIndex;
            }
        }
typo mistake :-)
[Category("Data"), Description("Selected Index of the DropDownList"), Browsable(true)]
        public int SelectedIndex
        {
            get
            {
                return LookupFieldTicketStatusDropDown.SelectedIndex;
            }
        }
SelectedText does not exist.
return LookupFieldTicketStatusDropDown.? is it Text?

[Category("Data"), Description("Selected Text of the DropDownList"), Browsable(true)]
        public string SelectedText
        {
            get
            {
                return LookupFieldTicketStatusDropDown.Text;
            }
        }


[Category("Data"), Description("Selected Text of the DropDownList"), Browsable(true)]
        public string SelectedText
        {
            get
            {
                return LookupFieldTicketStatusDropDown.SelectedItem.Text;
            }
        }
?
http://www.dreamincode.net/code/snippet3028.htm
       protected void btnSaveExit_Click(object sender, EventArgs e)
        {
           
           // Instantiate business object
           BLL.Tickets tickets = new BLL.Tickets();
           tickets.TicketType = uxLookupFieldTicketType1.SelectedValue;


           
        }

The values are null?
John where are u, man I need you.
Hi,

The selected value of dropdown list won't be null unless it doesn't have any items in it. If the dropdown list has atleat 1 item in it then it will return that value as selected value.

So you have to check

1. Whether the items are getting loaded into the Dropdown List
2. BLL.Tickets tickets = new BLL.Tickets(); has any problem.

Post the stack trace. it will really help to find the issue.
It doesn not bumb. But it is just null;
when I assign the value of the dropdownlist to the field of the object.
attached is my bo object
from the BLL

I have not yet wrote the DAL.

I can do this once all the stored procs are done.

using System;
using System.Text;
using System.Xml;
using System.Collections;
using System.Diagnostics;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Reflection;
using System.Xml.Serialization;
using System.IO;
using System.Collections.Generic;
 
 
 
namespace KORE.SIDWebClient.BLL
{
 
    [Serializable]
    public partial class Tickets
    {
 
 
        private int? ticketID = 0;
        private string ticketNumber = "";
        private string ticketType = "";
        private string ticketStatus = "";
        private string ticketSeverity = "";
        private string shortDescription = "";
        private string externalTicket = "";
        private string reasonMenu = "";
        private string problemNote = "";
        private string solutionNote = "";
        private string actionNote = "";
        private string followupNote = "";
        private bool? companiesFlag = false;
        private string agentCreated = "";
        private DateTime? dateCreated = System.DateTime.Now;
        private DateTime? dateLastModified = System.DateTime.Now;
        private string agentClosed = "";
        private DateTime? dateClosed = System.DateTime.Now;
        private string agentFollowup = "";
        private DateTime? followupBy = System.DateTime.Now;
        private bool? deletedFlag = false;
        private string ticketTypeOld = "";
        private string assignedTo = "";
        private string requestType = "";
        private string serviceImpact = "";
        private DateTime? dateTimeAssigned = System.DateTime.Now;
        private string priority = "";
        private DateTime? dateAssigned = System.DateTime.Now;
        private string trackIt = "";
        private string contactMethod = "";
        private string locationSIMs = "";
        private string userID = "";
        private string ticketApplicationType = "";
        private int? accountID = 0;
 
        
        #region "constructors"
 
        //public Company(int @companyID)
        //{
 
        //    DataSet CompanyDataSet = CompanyData.GetCompanyDataSet(@companyID);
 
        //    this.CompanyName = CompanyDataSet.Tables[0].Rows[0]["Company"].ToString();
        //    this.AccountNumber = CompanyDataSet.Tables[0].Rows[0]["AccountNumber"].ToString();
        //    this.AccountRepresentative = CompanyDataSet.Tables[0].Rows[0]["AccountRepresentative"].ToString();
        //    this.AgentCreated = CompanyDataSet.Tables[0].Rows[0]["AgentCreated"].ToString();
        //    this.Country = CompanyDataSet.Tables[0].Rows[0]["Country"].ToString();
        //    this.DeviceInfo = CompanyDataSet.Tables[0].Rows[0]["DeviceInfo"].ToString();
        //    this.Email = CompanyDataSet.Tables[0].Rows[0]["Email"].ToString();
        //    this.EnterpriseID = int.Parse(CompanyDataSet.Tables[0].Rows[0]["EnterpriseID"].ToString());
        //    this.EnterpriseAccountStatus = CompanyDataSet.Tables[0].Rows[0]["EnterpriseAccountStatus"].ToString();
        //    this.EnterpriseSolutionNote = CompanyDataSet.Tables[0].Rows[0]["EnterpriseSolutionNote"].ToString();
        //    this.EnterpriseType = CompanyDataSet.Tables[0].Rows[0]["EnterpriseType"].ToString();
        //    this.Fax = CompanyDataSet.Tables[0].Rows[0]["Fax"].ToString();
        //    this.IndustryType = CompanyDataSet.Tables[0].Rows[0]["IndustryType"].ToString();
        //    this.Phone = CompanyDataSet.Tables[0].Rows[0]["DeviceInfo"].ToString();
        //    this.ServiceFlagGPRS = bool.Parse(CompanyDataSet.Tables[0].Rows[0]["ServiceFlagGPRS"].ToString());
        //    this.ServiceFlagSMS = bool.Parse(CompanyDataSet.Tables[0].Rows[0]["ServiceFlagSMS"].ToString());
        //    this.ServiceFlagVoice = bool.Parse(CompanyDataSet.Tables[0].Rows[0]["ServiceFlagVoice"].ToString());
        //    this.PriorityLevelOverride = bool.Parse(CompanyDataSet.Tables[0].Rows[0]["PriorityLevelOverride"].ToString());
        //    this.PriorityLevel = CompanyDataSet.Tables[0].Rows[0]["PriorityLevel"].ToString();
        //    this.ContractType = CompanyDataSet.Tables[0].Rows[0]["ContractType"].ToString();
 
        //    string csd = CompanyDataSet.Tables[0].Rows[0]["ContractStartDate"].ToString();
        //    this.ContractStartDate = (csd.Equals("")) ? DateTime.MinValue : DateTime.Parse(csd);
 
        //    string ced = CompanyDataSet.Tables[0].Rows[0]["ContractEndDate"].ToString();
        //    this.ContractEndDate = (ced.Equals("")) ? DateTime.MinValue : DateTime.Parse(ced);
 
        //    string dc = CompanyDataSet.Tables[0].Rows[0]["DateCreated"].ToString();
        //    this.DateCreated = (dc.Equals("")) ? DateTime.MinValue : DateTime.Parse(dc);
 
        //    string dlm = CompanyDataSet.Tables[0].Rows[0]["DateLastModified"].ToString();
        //    this.DateLastModified = (dlm.Equals("")) ? DateTime.MinValue : DateTime.Parse(dlm);
        //}
        #endregion
 
 
        #region Properties
        public System.Int32? TicketID
        {
            get
            {
                return ticketID;
            }
            set
            {
                ticketID = value;
            }
        }
 
        public System.String TicketNumber
        {
            get
            {
                return ticketNumber;
            }
            set
            {
                ticketNumber = value;
            }
        }
 
        public System.String TicketType
        {
            get
            {
                return ticketType;
            }
            set
            {
                ticketType = value;
            }
        }
 
        public System.String TicketStatus
        {
            get
            {
                return ticketStatus;
            }
            set
            {
                ticketStatus = value;
            }
        }
 
        public System.String TicketSeverity
        {
            get
            {
                return ticketSeverity;
            }
            set
            {
                ticketSeverity = value;
            }
        }
 
        public System.String ShortDescription
        {
            get
            {
                return shortDescription;
            }
            set
            {
                shortDescription = value;
            }
        }
 
        public System.String ExternalTicket
        {
            get
            {
                return externalTicket;
            }
            set
            {
                externalTicket = value;
            }
        }
 
        public System.String ReasonMenu
        {
            get
            {
                return reasonMenu;
            }
            set
            {
                reasonMenu = value;
            }
        }
 
        public System.String ProblemNote
        {
            get
            {
                return problemNote;
            }
            set
            {
                problemNote = value;
            }
        }
 
        public System.String SolutionNote
        {
            get
            {
                return solutionNote;
            }
            set
            {
                solutionNote = value;
            }
        }
 
        public System.String ActionNote
        {
            get
            {
                return actionNote;
            }
            set
            {
                actionNote = value;
            }
        }
 
        public System.String FollowupNote
        {
            get
            {
                return followupNote;
            }
            set
            {
                followupNote = value;
            }
        }
 
        public System.Boolean? CompaniesFlag
        {
            get
            {
                return companiesFlag;
            }
            set
            {
                companiesFlag = value;
            }
        }
 
        public System.String AgentCreated
        {
            get
            {
                return agentCreated;
            }
            set
            {
                agentCreated = value;
            }
        }
 
        public System.DateTime? DateCreated
        {
            get
            {
                return dateCreated;
            }
            set
            {
                dateCreated = value;
            }
        }
 
        public System.DateTime? DateLastModified
        {
            get
            {
                return dateLastModified;
            }
            set
            {
                dateLastModified = value;
            }
        }
 
        public System.String AgentClosed
        {
            get
            {
                return agentClosed;
            }
            set
            {
                agentClosed = value;
            }
        }
 
        public System.DateTime? DateClosed
        {
            get
            {
                return dateClosed;
            }
            set
            {
                dateClosed = value;
            }
        }
 
        public System.String AgentFollowup
        {
            get
            {
                return agentFollowup;
            }
            set
            {
                agentFollowup = value;
            }
        }
 
        public System.DateTime? FollowupBy
        {
            get
            {
                return followupBy;
            }
            set
            {
                followupBy = value;
            }
        }
 
        public System.Boolean? DeletedFlag
        {
            get
            {
                return deletedFlag;
            }
            set
            {
                deletedFlag = value;
            }
        }
 
        public System.String TicketTypeOld
        {
            get
            {
                return ticketTypeOld;
            }
            set
            {
                ticketTypeOld = value;
            }
        }
 
        public System.String AssignedTo
        {
            get
            {
                return assignedTo;
            }
            set
            {
                assignedTo = value;
            }
        }
 
        public System.String RequestType
        {
            get
            {
                return requestType;
            }
            set
            {
                requestType = value;
            }
        }
 
        public System.String ServiceImpact
        {
            get
            {
                return serviceImpact;
            }
            set
            {
                serviceImpact = value;
            }
        }
 
        public System.DateTime? DateTimeAssigned
        {
            get
            {
                return dateTimeAssigned;
            }
            set
            {
                dateTimeAssigned = value;
            }
        }
 
        public System.String Priority
        {
            get
            {
                return priority;
            }
            set
            {
                priority = value;
            }
        }
 
        public System.DateTime? DateAssigned
        {
            get
            {
                return dateAssigned;
            }
            set
            {
                dateAssigned = value;
            }
        }
 
        public System.String TrackIt
        {
            get
            {
                return trackIt;
            }
            set
            {
                trackIt = value;
            }
        }
 
        public System.String ContactMethod
        {
            get
            {
                return contactMethod;
            }
            set
            {
                contactMethod = value;
            }
        }
 
        public System.String LocationSIMs
        {
            get
            {
                return locationSIMs;
            }
            set
            {
                locationSIMs = value;
            }
        }
 
        public System.String UserID
        {
            get
            {
                return userID;
            }
            set
            {
                userID = value;
            }
        }
 
        public System.String TicketApplicationType
        {
            get
            {
                return ticketApplicationType;
            }
            set
            {
                ticketApplicationType = value;
            }
        }
 
        public System.Int32? AccountID
        {
            get
            {
                return accountID;
            }
            set
            {
                accountID = value;
            }
        }
 
        #endregion
 
    }
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of GiftsonDJohn
GiftsonDJohn
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
how can I do this?
Is it throwing any null reference errror?
It just is empty when I click on the submit button.
Excellent job. I created a sep question.