如何根据数据库表记录更改下拉列表的列表项 [英] How to change the list items of of dropdown list based on database table records

查看:89
本文介绍了如何根据数据库表记录更改下拉列表的列表项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个基于Web的应用程序(学生注册表)。在这个网页中,我有1个下拉列表(显示周六,周日,周一,周二,........等演讲)。选择一个列表项后(例如:星期六它将显示可用的课程作为单选按钮(例如:MS-Office,C#.Net)。在下拉列表的选定索引更改事件中 - 我写了一个开关案例:对于每个不同的case。

I am creating a web based application (Student Registration Form). In this Web Page I have 1 dropdown list (which shows the lecturetimings like Saturday, Sunday, Monday, Tuesday,........etc). After selecting a list item(ex: Saturday It will show availble courses as Radio buttons (Example:MS-Office,C#.Net). In the selected index change event of drop down list-I have written a switch case: for every different case.

case 0: r1.Visible = false; r2.Visible = false; break; 
case 1: r1.Visible = false; r2.Visible = false; r1.Text = "MS-Office"; r1.Visible = true; break; 
case 2: r1.Visible = false; r2.Visible = false; r1.Text = "Hardware & Networking"; r2.Text="C#.net"; r1.Visible = true; r2.Visible = true; break;



...........

案例7写到。



然后我有一些文本框(StudentName,EmailId,ContactNo。和地址)。

有一个提交按钮(打开)点击此处,信息将保存在数据库表注册中(StudentName,EmailId,ContactNo。,地址,讲座,课程)

Ex:(John,john @ john.com,9999999999,印度,星期六,MS-Office)。



现在我的要求是如果有星期六的10条记录,那么星期六的下拉列表中不应该显示。



我在存储过程的帮助下解决了这个问题。我已经拿了一个数据表,我插入的Lecturetimings值小于10条记录。当我显示列表项时从数据表中,它工作正常,它只显示少于10条记录的项目。



但问题是它最初应该是所有列表项,如果数据库中没有记录。另一个问题是,当我选择任何列表项时,那么它应显示相应的单选按钮(最初它显示,但从数据表中获取数据后它无法正常工作)。

提前感谢您为我付出痛苦。



到目前为止我的尝试代码是:




...........
Upto case7 written.

Then I have some text boxes for (StudentName, EmailId, ContactNo.& address).
There is a submit button( on clicking this, the information wil be saved in data base table "registration"(StudentName,EmailId,ContactNo.,Address, Lecturetimings, Course)
Ex:(John, john@john.com, 9999999999, India, Saturday,MS-Office).

Now My requirement is if there 10 records with Saturday then in the dropdown list saturday should not be shown.

I solved this problem with help of a stored procedure.I have taken a data table and I inserted the values of "Lecturetimings" are less than 10 records. When I display the list items from the data table, it is working fine, it is showing only that items which are less than 10 records.

But the problem is it should initially all the list items,If there is no records in the database. And another problem is, when I select any list item then it should show the appropriate radio buttons(Initially it was showing, but after getting the data from the data table it is not working).
Thanks in advance for taking pains for me.

My Tried code so far is :

References: d1=dropdownlist1
	     r1=radiobutton1
	     r2=radiobutton2


Code:


protected void Page_Load(object sender, EventArgs e)
        {
            DataTable records = new DataTable(); 
             string myConnection = "Data Source=server;Initial Catalog=college;User ID=sa;Password=123";
            SqlConnection con = new SqlConnection(myConnection);
            con.Open();
            SqlCommand sqlCommand = new SqlCommand(); 
            sqlCommand.CommandType = CommandType.StoredProcedure; 
            sqlCommand.CommandText = "GetListRecordNotHavingTenCount"; 
            sqlCommand.Connection = con;
            SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sqlCommand); sqlDataAdapter.Fill(records);
           // d1.DataSource = records;
           // d1.DataValueField = "Id"; 
           // d1.DataTextField = "Lecturetimings"; 
            //this.d1.DataBind();
            
        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {

            
            switch (d1.SelectedIndex)
            {
                case 0:
                    r1.Visible = false;
                    r2.Visible = false;
                    break;
                case 1:
                    r1.Visible = false;
                    r2.Visible = false;
                    r1.Text = "C Language";
		      r2.Text = "C ++";
		      r1.Visible = true;
r2.Visible = true;
                    break;
                case 2:
                    r1.Visible = false;
                    r2.Visible = false;
                    r1.Text = "Basic Hardware";
                    r1.Visible = true;
                    break;
                case 3:
                    r1.Visible = false;
                    r2.Visible = false;
                    r1.Text = "Networking";
                    r1.Visible = true;
                    break;
                case 4:
                    r1.Visible = false;
                    r2.Visible = false;
                    r1.Text = "Adobe Flash";
			r2.Text = "Adobe Photoshop";

                    r1.Visible = true;
			r2.Visible = true;
                    break;
                case 5:
                    r1.Visible = false;
                    r2.Visible = false;
                    r1.Text = "C#.Net";
                    r1.Visible = true;
                    break;
                case 6:
                    r1.Visible = false;
                    r2.Visible = false;
                    r1.Text = "Basic Hardware";
                    r1.Visible = true;
                    break;
                case 7:

                    r1.Text = "Adobe Flash";
                    r2.Text = "Networking";
                    r1.Visible = true;
                    r2.Visible = true;
                    break;

            }

        }


        protected void Button1_Click1(object sender, EventArgs e)
        {
            String myConnection = " Data Source=server;Initial Catalog=college;User ID=sa;Password=123";
            SqlConnection con = new SqlConnection(myConnection);
            SqlCommand cmd;
                  
            con.Open();
            cmd = new SqlCommand(@"INSERT INTO [dbo].[Register]([Sname],[Email]
           ,[ContactNo],[Activity],[Lecturetimings])
     VALUES(''" + name.Text + "'',''" + email.Text + "'',''" + contact.Text + "'',''" + r1.Text + "'',''"+d1.SelectedValue+"'')", con);
            cmd.ExecuteNonQuery();
            con.Close();
ScriptManager.RegisterStartupScript(this, GetType(), "alert", "alert(''SuccessFully Submitted'');", true);
            d1.SelectedIndex = 0;
            name.Text = "";
                       email.Text = "";
            contact.Text = "";
            CheckBox1.Checked = false;
            r1.Visible = false;
            r2.Visible = false;
            
                  }

推荐答案

1)您可以动态地设置时间,而不是让您的单选按钮出现添加它们。

例如。你可以有LectureTimings表,有Day和Course作为列。

如果用户选择,星期六:你应该有一个查询准备好以List< Courses>,

迭代该列表以在屏幕上动态生成那么多单选按钮。

在这种情况下你不需要编写switch case,for循环将生成无线电将是足够。

2)如果列表中没有项目,则count将为零,因此在这种情况下,循环不会生成任何单选按钮。这解决了第一个没有针对LectureTimings的数据的问题。

理想情况下,标签的Visible = false。

如果List< Courses>中没有项目,请将标签显示为可见,并显示此演讲时间没有课程的消息,否则在循环的帮助下生成单选按钮。
1) Rather than having your radio buttons present from the design time, you can dynamically add them.
For eg. You can have LectureTimings table, have Day and Course as columns.
If user selects, Saturday: you should have a query ready to get all the courses in the form of List<Courses>,
iterate that list to generate those many radio buttons dynamically on your screen.
You don''t need to write switch case in that case, a for loop will to generate radios will be enough.
2) If there are no items in the list, count will be zero so for loop will not generate any radio button in that case. this resolves your first issue of not having data against a LectureTimings.
Ideally, have a label with Visible=false.
If you have no items in List<Courses>, make label visible and show message that there are no courses for this lecture timing, else let radio radio buttons get generated with the help of loop.


只需一两个修改。根据我的代码,您可以执行两项任务。

1.将数据绑定到下拉列表。

2.更改单选按钮的属性基于你的下拉列表选择的索引更改事件。



在绑定数据之前,你可以像下面的代码片段一样进行这样的小的空检查。



Just one or two modifications. From what I understand from your code is that you want to perform two task.
1. Binding data to your dropdownlist.
2. Change the radio button''s properties based on you dropdownlist selected index change event.

Before binding data, you can do a small null check like this as shown in the code snippet below.

sqlDataAdapter.Fill(records);
if(records != null)
{
    d1.DataSource = records;
    d1.DataBind();
}





如果您的DataTable或DataSet为空,上面的代码可以避免任何运行时异常。



而不是设置单独的单选按钮的行为。您可以考虑使用RadioButtonList。

将主题值存储在数据库的单独表格中,并定义外键与您执行存储过程的表格的关系。



然后你可以将dropdownlist.selectedindex作为主题搜索查询的参数。这样就可以避免很多静态定义,并且需要更少的代码更改。



如果您需要有关此方法的更多详细信息,请告诉我。



The above code avoids any run time exception if your DataTable or DataSet is null.

Instead of setting separate radio button''s behavior. You may consider using RadioButtonList.
Store the subject values in a separate table in your DB and define Foreign Key for relationship with your table where you were executing the stored procedure.

Then you can make dropdownlist.selectedindex as a parameter for the subject search query. This way you avoid a lot of static definitions and require less code change.

Let me know if you need more details on this approach.

这篇关于如何根据数据库表记录更改下拉列表的列表项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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