下拉列表中不能有多个选定项目 [英] Cannot have multiple selected items in dropdown list

查看:72
本文介绍了下拉列表中不能有多个选定项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三个日期,月份和年份的下拉列表和一个按钮。我收到了错误。我得到:不能在DropDownList中选择多个项目

I have three dropdownlist for date,month,and Year and a button. I am getting the error. I am getting:Cannot have multiple items selected in a DropDownList

 protected void Page_Load(object sender, EventArgs e)
        {
         for (int month = 1; month < 13; month++)
            {
            
                ddlMonth.Items.Add(month.ToString());

            }
            ddlMonth.Items.FindByValue(System.DateTime.Now.Month.ToString()).Selected = true;
            for (int year = 1947; year < 2015; year++)
            {

                ddlYear.Items.Add(year.ToString());

            }
           ddlYear.Items.FindByValue(System.DateTime.Now.Year.ToString()).Selected = true;
FillDays();
}
public void FillDays()
        {
            ddlDate.Items.Clear();
            //getting number of days in selected month & year
            int noofdays = DateTime.DaysInMonth(Convert.ToInt32(ddlYear.SelectedValue), Convert.ToInt32(ddlMonth.SelectedValue));

            //Fill days
            for (int i = 1; i <= noofdays; i++)
            {
                ddlDate.Items.Add(i.ToString());
            }
            ddlDate.Items.FindByValue(System.DateTime.Now.Day.ToString()).Selected = true;// Set current date as selected
        }
 protected void save_Click(object sender, EventArgs e)
        {
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "Insert_Reminder";
            cmd.CommandType = CommandType.StoredProcedure;
            string datestring = string.Format("{0}-{1}-{2}", ddlMonth.SelectedValue, ddlDate.SelectedValue.PadLeft(2, '0'), ddlYear.SelectedValue.PadLeft(2, '0'));
            string timestring = ddlHour.SelectedValue +":"+ddlMinute.SelectedValue +":"+ddlsecond.SelectedValue;
            cmd.Parameters.Add("@birthdate",SqlDbType.Date).Value=datestring;
            cmd.Parameters.Add("@reminder_time", SqlDbType.Time).Value = timestring.Trim();
            cmd.Parameters.Add("@reminder_detail", SqlDbType.Text).Value = txt_reminder.Text.Trim();
            cmd.Connection=con;
            try
            {
                con.Open();
                cmd.ExecuteNonQuery();
                Response.Write("<script>alert('Data Inserted Successfully')</script>");
            }
            catch(Exception ex)
            {
                   Response.Write(ex.Message);
            }
            finally
            {
                con.Close();
                con.Dispose();
            }
        }

推荐答案

ASP.NET DropDownList仅用于单选。您可以使用具有选择模式属性的ListBox,并可以选择乘法值...

从这里开始阅读: ASP.NET中的多选下拉列表 [ ^ ]
ASP.NET DropDownList is only for single selection. You may use ListBox which has property for selection mode and can select multiply values...
Start reading here: Multi select Dropdown list in ASP.NET[^]


这篇关于下拉列表中不能有多个选定项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆