?
Solved

need to add a blank value at the top of a dropdownlist that is binded to a stored proc.

Posted on 2009-04-30
4
Medium Priority
?
140 Views
Last Modified: 2012-05-06
I have the following: this is in my aspx page.
    <KORE:uxLookupFieldTicketAssignedTo runat="server" ID="uxLookupFieldTicketAssignedTo"
                                    KeyField="Agent" ValueField="AgentID" />
  -----------------------------------------------------------------------------------------------------                                      
When the dropdownlist has a blank value at the top I want to also notify the user that he/she must select a value:
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 uxLookupFieldTicketAssignedTo : System.Web.UI.UserControl
    {
        private string _keyField;
        private string _valueField;
        private string _defaultValue;

        public uxLookupFieldTicketAssignedTo()
        {
           
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            LookupFieldTicketAssignedTo lookup = new LookupFieldTicketAssignedTo();

            this.LookupFieldTicketAssignedToDropDown.Items.AddRange(lookup.getLookupListItemGroup(this.KeyField, this.ValueField));

            ListItem selected = this.LookupFieldTicketAssignedToDropDown.Items.FindByValue(this.DefaultValue);

            this.LookupFieldTicketAssignedToDropDown.SelectedIndex = this.LookupFieldTicketAssignedToDropDown.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;
            }
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;
 
namespace KORE.SIDWebClient.DAL
{
    public class LookupFieldTicketAssignedToData
    {
        public static DataSet getLookupFieldTicketAssignedToData()
        {
            Database db = DatabaseFactory.CreateDatabase();
            string sql = "sprLookupFieldTicketAssignedTo";
 
            DbCommand cmd = db.GetStoredProcCommand(sql);
            DataSet fieldTicketAssignedToDataSet = db.ExecuteDataSet(cmd);
 
            return fieldTicketAssignedToDataSet;
        }
    }
}
=======================================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using KORE.SIDWebClient.DAL;
using System.Web.UI.WebControls;
 
 
namespace KORE.SIDWebClient.BLL
{
    public class LookupFieldTicketAssignedTo
    {
        private DataSet _lookupdata;
 
        #region "constructors"
 
        public LookupFieldTicketAssignedTo()
        {
            this._lookupdata = LookupFieldTicketAssignedToData.getLookupFieldTicketAssignedToData();
        }
 
        #endregion
 
 
 
        #region "methods"
 
        public ListItem[] getLookupListItemGroup(string keyField, string valueField)
        {
            ListItem[] list = new ListItem[this._lookupdata.Tables[0].Rows.Count];
 
            int i = 0;
 
            foreach (DataRow dr in this._lookupdata.Tables[0].Rows)
            {
                list[i++] = new ListItem(dr[keyField].ToString(), dr[valueField].ToString());
            }
 
            return list;
        }
 
        #endregion
    }
}

Open in new window

0
Comment
Question by:mathieu_cupryk
  • 3
4 Comments
 
LVL 7

Accepted Solution

by:
dacIT earned 2000 total points
ID: 24276572
Give this a shot. It uses the "AppendDataBoundItems" property set to true, then code the blank one in the markup.

http://www.devx.com/tips/Tip/27951
0
 
LVL 7

Assisted Solution

by:dacIT
dacIT earned 2000 total points
ID: 24276574
You could also do it in the code by adding a listitem in the "DataBound" event of the control.
0
 

Author Comment

by:mathieu_cupryk
ID: 24276612
Ok I have this :
<%@ 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>

How am I going to put a validator control to check if null?
0
 
LVL 7

Expert Comment

by:dacIT
ID: 24276627
You can create a custom validator that checks the the selected value of "" and returns "false" or if that listitem stays at the top of the list, just return false if the selected index is 0.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
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 …
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses
Course of the Month13 days, 14 hours left to enroll

807 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