本文介绍了如何禁用checkboxlist中的多个复选框。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的任务是在线电影票预订。剧院有25张票。我点击5张票并在复选框列表中提交。



页面加载后,5张票将被禁用。在asp.net中的解决方案。



我尝试了什么:



我仍​​然完成,如果我点击5张票将存储在数据库中的5张票。

i给(checkboxlistID.item.enabled = false)。



第一张票只有disable.after页面加载所有票据被点击。但它没有禁用。





my task is online movie ticket booking. Theater have 25 tickets.I click 5 tickets and submit in the checkbox list.

After the page load that 5 tickets will be disable.give the solution in asp.net.

What I have tried:

still i complete,if i click the 5 tickets that 5 tickets will be store in the database.
i gave (checkboxlistID.item.enabled=false).

first ticket only disable.after page load all tickets are clicked.but it did not disable.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace WebApplication1
{
    public partial class MovieDetails : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                chkSeatsAvailability.Items.Clear();
                PopulateBookedSeats();
                
            }
        } 

        private void PopulateBookedSeats()
        {
            using (SqlConnection conn = new SqlConnection())
            {
                conn.ConnectionString = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
                        
                using (SqlCommand cmd = new SqlCommand())
                {
                    cmd.CommandText = "select * from tblBookedSeats";
                    cmd.Connection = conn;
                    conn.Open();
                    using (SqlDataReader sdr = cmd.ExecuteReader())
                    {
                        while (sdr.Read())
                        {
                            ListItem item = new ListItem();
                            item.Text = sdr["Seat_No"].ToString();
                            item.Value = sdr["Seat_ID"].ToString();
                            item.Selected = Convert.ToBoolean(sdr["IsSelected"]);
                            if(item.Selected)
                            item.Attributes.CssStyle.Add("readonly","true");
                            chkSeatsAvailability.Items.Add(item);                            
                        }
                        
                    }
                          
                    
                    conn.Close();
                }
            }
        }  

        protected void btnSubmit_Click(object sender, EventArgs e)
        {

            copyMovieName.Text = lblMovieName.Text;
            lblSelectedMovie.Text = ddlMovieNames.SelectedItem.Text;

            copyDate.Text = lblDate.Text;
            lblSelectedDate.Text = txtDate.Text;


            copyShowName.Text = lblShowName.Text;
            lblSelecetedShow.Text = ddlShowName.SelectedItem.Text;


            copySeats.Text = lblSeatsAvailability.Text;


            String CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;

            using (SqlConnection cn = new SqlConnection(CS))
            {
                cn.Open();                

                string interests = string.Empty;
                foreach (ListItem li in chkSeatsAvailability.Items)
                {
                    if (li.Selected)
                    {
                        interests += li + ",";
                        lblSelectedSeats.Text = interests;                        
                                            
                    }
                }       

                SqlCommand cmd = cn.CreateCommand();

                cmd.CommandType = CommandType.Text;

                cmd.CommandText = "insert into tblBookingDetails(Date,Movie_Name,Show_Name,Seats_Availability) values(@Date,@Movie_Name,@Show_Name,@Seats_Availability)";
                cmd.Parameters.AddWithValue("@Movie_Name", ddlMovieNames.SelectedItem.Text);
                cmd.Parameters.AddWithValue("@Date",Convert.ToDateTime(txtDate.Text));
                cmd.Parameters.AddWithValue("@Show_Name", ddlShowName.SelectedItem.Text);
                cmd.Parameters.AddWithValue("@Seats_Availability", interests);

                cmd.ExecuteNonQuery();               

                cn.Close();
                

                String str = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
                using (SqlConnection conn = new SqlConnection())
                {
                    conn.ConnectionString = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
                            
                    using (SqlCommand command = new SqlCommand())
                    {
                        command.CommandText = "update tblBookedSeats set IsSelected = @IsSelected where Seat_ID=@Seat_ID";
                        command.Connection = conn;

                        conn.Open();
                        foreach (ListItem item in chkSeatsAvailability.Items)
                        {
                            command.Parameters.Clear();
                            command.Parameters.AddWithValue("@IsSelected", item.Selected);
                            command.Parameters.AddWithValue("@Seat_ID", item.Value);
                            command.ExecuteNonQuery();
                            [
                        }
                       
                        conn.Close();

                       
                    }
                }         
               
            }

        }



        protected void chkSeatsAvailability_SelectedIndexChanged(object sender, EventArgs e)
        {
            listSeats.Items.Clear();
            foreach (ListItem li in chkSeatsAvailability.Items)
            {
                if (li.Selected)
                {
                    listSeats.Items.Add(li.Text);
                }

                if (chkSeatsAvailability.SelectedIndex==-1)
                {
                    lblTotalList.ForeColor = System.Drawing.Color.Red;
                }
                else
                {
                    lblTotalList.ForeColor = System.Drawing.Color.Black;
                }
            }
            lblTotalList.Text = listSeats.Items.Count.ToString() + " Ticket(s) are choosing";

        }

        
           
    }
}

推荐答案

if (item.Selected)
         item.Enabled = false;


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace WebApplication1
{
    public partial class MovieDetails : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                chkSeatsAvailability.Items.Clear();
                PopulateBookedSeats();
                
            }
        } 

        private void PopulateBookedSeats()
        {
            using (SqlConnection conn = new SqlConnection())
            {
                conn.ConnectionString = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
                        
                using (SqlCommand cmd = new SqlCommand())
                {
                    cmd.CommandText = "select * from tblBookedSeats";
                    cmd.Connection = conn;
                    conn.Open();
                    using (SqlDataReader sdr = cmd.ExecuteReader())
                    {
                        while (sdr.Read())
                        {
                            ListItem item = new ListItem();
                            item.Text = sdr["Seat_No"].ToString();
                            item.Value = sdr["Seat_ID"].ToString();
                            item.Selected = Convert.ToBoolean(sdr["IsSelected"]);
                            if(item.Selected)
                            item.Attributes.CssStyle.Add("readonly","true");
                            chkSeatsAvailability.Items.Add(item);                            
                        }
                        
                    }
                          
                    
                    conn.Close();
                }
            }
        }  

        protected void btnSubmit_Click(object sender, EventArgs e)
        {

            copyMovieName.Text = lblMovieName.Text;
            lblSelectedMovie.Text = ddlMovieNames.SelectedItem.Text;

            copyDate.Text = lblDate.Text;
            lblSelectedDate.Text = txtDate.Text;


            copyShowName.Text = lblShowName.Text;
            lblSelecetedShow.Text = ddlShowName.SelectedItem.Text;


            copySeats.Text = lblSeatsAvailability.Text;


            String CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;

            using (SqlConnection cn = new SqlConnection(CS))
            {
                cn.Open();                

                string interests = string.Empty;
                foreach (ListItem li in chkSeatsAvailability.Items)
                {
                    if (li.Selected)
                    {
                        interests += li + ",";
                        lblSelectedSeats.Text = interests;                        
                                            
                    }
                }       

                SqlCommand cmd = cn.CreateCommand();

                cmd.CommandType = CommandType.Text;

                cmd.CommandText = "insert into tblBookingDetails(Date,Movie_Name,Show_Name,Seats_Availability) values(@Date,@Movie_Name,@Show_Name,@Seats_Availability)";
                cmd.Parameters.AddWithValue("@Movie_Name", ddlMovieNames.SelectedItem.Text);
                cmd.Parameters.AddWithValue("@Date",Convert.ToDateTime(txtDate.Text));
                cmd.Parameters.AddWithValue("@Show_Name", ddlShowName.SelectedItem.Text);
                cmd.Parameters.AddWithValue("@Seats_Availability", interests);

                cmd.ExecuteNonQuery();               

                cn.Close();
                

                String str = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
                using (SqlConnection conn = new SqlConnection())
                {
                    conn.ConnectionString = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
                            
                    using (SqlCommand command = new SqlCommand())
                    {
                        command.CommandText = "update tblBookedSeats set IsSelected = @IsSelected where Seat_ID=@Seat_ID";
                        command.Connection = conn;

                        conn.Open();
                        foreach (ListItem item in chkSeatsAvailability.Items)
                        {
                            command.Parameters.Clear();
                            command.Parameters.AddWithValue("@IsSelected", item.Selected);
                            command.Parameters.AddWithValue("@Seat_ID", item.Value);
                            command.ExecuteNonQuery();
                            [
                        }
                       
                        conn.Close();

                       
                    }
                }         
               
            }

        }



        protected void chkSeatsAvailability_SelectedIndexChanged(object sender, EventArgs e)
        {
            listSeats.Items.Clear();
            foreach (ListItem li in chkSeatsAvailability.Items)
            {
                if (li.Selected)
                {
                    listSeats.Items.Add(li.Text);
                }

                if (chkSeatsAvailability.SelectedIndex==-1)
                {
                    lblTotalList.ForeColor = System.Drawing.Color.Red;
                }
                else
                {
                    lblTotalList.ForeColor = System.Drawing.Color.Black;
                }
            }
            lblTotalList.Text = listSeats.Items.Count.ToString() + " Ticket(s) are choosing";

        }

        
           
    }
}


这篇关于如何禁用checkboxlist中的多个复选框。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-16 01:27