下拉列表-获取选定值的ID [英] Dropdownlist - Get Id of Selected Value

查看:156
本文介绍了下拉列表-获取选定值的ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,
对我来说这是一个困难的问题,实际上我在SQL Server数据库中有两个表,

Hello everyone,
It is a difficult problem for me, Actually I have TWO TABLES in SQL Server Database,

1. "Category" Table:
----------------------
Cate_id	  Category
  1	   Books
  2	   Cd's
  3	   DVD's
  4	   Files
----------------------

2. "Sub_ Category" Table:
-----------------------------------------
Sub_Cate_id	Sub_Category	Cate_id
  1       	 I.T Books	  1
  2	         Asp.Net Book	  1
  3	         Java Books	  1
  4	         Music CD	  2
  5	         Movie CD	  2
  6	         Songs CD	  2
-----------------------------------------

/* Selet Query*/
select cat_name, sub_name from catagories c inner join catagories_sub sc on c.cat_id = sc.cat_id


例如,选择的类别"是书籍","I.T书籍","Asp.Net书籍"和"Java图书是图书"的子类别"

问题:
问题在于,在Visual Studio中,当我完成将类别"添加到SQL数据库时,
现在,我找到了添加子类别的表单,还有一个DropDownList(先前从SQL"Category Table"中选择的类别).当用户在文本框中编写新的"Sub_Category"并单击添加时,如何获得所选的项目ID(保存在"Category" SQL表中),然后将其保存在子类别表中

我不知道我是否正确解释了我的问题,对不起,出错了.

感谢您的帮助!


for suppose, Selected category is Books, So, I.T Books, Asp.Net Books & Java Books are "Sub-Categories" of "Books"

Problem:
The Problem is that, In Visual Studio, when I finish adding "Category" to the SQL database,
Now, I found form to add sub-category, also there is a DropDownList (which Previously Selected Categories from SQL "Category Table"). When user write a new ''Sub_Category'' in textbox and click to add it, HOW can I get the selected Item, Id(Saved in "Category" SQL table), and then save it in sub-category table

I don''t know I explain my question properly or not, Sorry for mistakes.

Thanks for Helping Me!

推荐答案

如果您在DropDownList中加载如下数据,
If you have load data in DropDownList as below,
DropDownList1.DataSource = DtCategory
DropDownList1.ValueMember = "Cate_id"
DropDownList1.DisplayMember = "Category"


然后,下拉列表将具有以下值


then,dropdownlist will have value as below

dropdownIndex ValueMember Displaymember
0              1	   Books
1              2	   Cd's
2              3	   DVD's
3              4	   Files


因此,当您选择 DVD的时,


so,when you select DVD''s,

DropDownList1.SelectedIndex will be 2




and

DropDownList1.SelectedValue will be 3


并且您需要第二个值保存在数据库中
因此,使用 DropDownList1.SelectedValue 来获取cate_Id并在添加子类别时将其保存在数据库中

祝您编程愉快!
:)


and you need second value to save in database
so, use DropDownList1.SelectedValue for fetch cate_Id and save it in database while adding Subcategory

Happy coding!
:)


要获取CategoryID,您必须指定Category Dropdownlist的名称,如下所示

For getting the CategoryID you have to specify the name of your Category Dropdownlist, like as follows

ddlCategory.SelectedValue



您将获得类别的值并将其保存在数据库中.

谢谢



You will get the value of your category and save it in database.

Thanks


谢谢大家的答案&建议,

但是,我还想分享我在其他ASP论坛上发现的我的问题的另一个答案,如果还有其他人必须面对该问题或Asp的初学者遵循该答案,

首先创建一个填充DropDownList的方法:
Thank you all for Answers & Suggestions,

But, I also want to share one more Answer of my question, which I found at other Asp Forum, If any one also have to face that problem or beginner to Asp follow that Answer,

First Create a Method for filling DropDownList:
private void FillCatagoryList()
    {
        // Filling the Drop DownListItems //
        try
        {       
            // Using Connection String from web.config File //
            SqlConnection sqlcon = new SqlConnection (ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString); //    
            sqlcon.Open();
            DataSet ds = new DataSet();
            SqlCommand cmd = new SqlCommand("select * from catagories", sqlcon);
            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            adapter.Fill(ds);
            List_Catagories.DataSource = ds;
            List_Catagories.DataTextField = "cat_name";
            List_Catagories.DataValueField = "cat_id";
            List_Catagories.DataBind();
        }
        catch (Exception err)
        {
            lblResults.Text = "Error reading list of names. ";
            lblResults.Text += err.Message;
            // The Code below Shows the Alert Box
            // Response.Write("<script>alert('No Image Selected!')</script>");
        }
        finally
        {
            sqlcon.Close();
        }


先前的代码将同时保存类别ID和&类别名称,并在"List_Catagories.DataTextField"的"DropDownList"中显示类别名称,并将"Cat_id"保存在"List_Catagories.DataValueField"中.

然后使用以下代码保存新的子类别项目(用户输入)及其代码(从DropDownList选定的项目):


Previous Code will save temprory both Category Id & Category Name, and shows Category Name in "DropDownList" from "List_Catagories.DataTextField" and save "Cat_id" in "List_Catagories.DataValueField".

Then Save the New Sub-Category Item(User Input) and its code(From DropDownList Selected Item) using the following code:

try
{
// Using Connection String from web.config File //
SqlConnection sqlcon = new SqlConnection (ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString); //    
sqlcon.Open();
string nam = txtSubCategory.Text.Trim();
int Sel_id = Convert.ToInt32(List_Catagories.SelectedValue);
SqlCommand cmd  = new SqlCommand("INSERT INTO catagories_sub (category_id, Category_Name) values ("+ Sel_id +", '"+ nam +"')", sqlcon);
cmd.ExecuteNonQuery();
}
catch (Exception err)
{
lblResults.Text = "Error reading list of names. ";
lblResults.Text += err.Message;
}
finally
{
sqlcon.Close();
}



因此,这就是解决方案,它将类别表的类别ID保存到子类别,如果我们通过内部联接进行选择,结果将为我们提供类别和子类别匹配的记录,我正在粘贴SQL查询以便您可以轻松执行,



So, That''s the Solution, and it will save a Category id of Category table to the sub category, if we select through inner join, the result will give us the category and sub-category matched record, I am pasting my SQL Query so you can execute it easiy,

select cat_name, sub_name from category c inner join sub_category sc on c.cate_id = sc.cate_id



谢谢,
问候,
穆斯塔法·伊克巴尔(Mustafa Iqbal).



Thanks,
Regards,
Mustafa Iqbal.


这篇关于下拉列表-获取选定值的ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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