如何在下拉列表中选择默认值时指定id为零

编程入门 行业动态 更新时间:2024-10-08 06:26:07
本文介绍了如何在下拉列表中选择默认值时指定id为零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个下拉列表,我希望如果选择没有任何id应该存储在数据库0其他明智的,如果选择的东西然后该项目的ID。?? 代码..

I have A drop Down list and i want that if selected nothing the id should be stored in database 0 other wise if selected something then the id of that item .?? code..

protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { Query = @"SELECT [category_id] FROM [Category_Master]"; ds1 = dl.fetchrecord(Query); ddl_parent.Items.Insert(0, new ListItem("---none---")); int i = 0; while (i < ds1.Tables[0].Rows.Count) { flag = 1; catname = ""; index = 1; ListItem li = new ListItem(); li.Text = catmapping(ds1.Tables[0].Rows[i]["category_id"].ToString()); li.Value = ds1.Tables[0].Rows[i]["category_id"].ToString(); ddl_parent.Items.Add(li); i = i + 1; } } lbl_mess.Text = ""; } string catmapping(string cat_id) { while (flag == 1) { Query = "select Category_ParentId,Category_Name from Category_Master where category_id=" + cat_id + ""; ds = dl.fetchrecord(Query); cat_id = ds.Tables[0].Rows[0]["Category_ParentId"].ToString(); str = ds.Tables[0].Rows[0]["Category_Name"].ToString(); if (index == 1) { catname = str; } else { catname = str + ">>" + catname; } if (cat_id == "0") { flag = 0; } index++; } return catname; } protected void Button1_Click(object sender, EventArgs e) { if (IsPostBack) { Query = @"INSERT INTO [RbmDatabase].[dbo].[Category_Master] ([Category_ParentId] ,[Category_Name] ,[Category_MetaTag] ,[Category_MetaTagKeywords] ,[Category_Description] ,[Category_SortOrder] ,[Category_Status] ,[Category_UpdateDate]) VALUES ('" + dl.QoutesHandel(ddl_parent.SelectedValue.ToString()) + "','" + dl.QoutesHandel(txt_category.Text.Trim()) + "','" + dl.QoutesHandel(txt_MTdesc.Text.Trim()) + "','" + dl.QoutesHandel(txt_MTkey.Text.Trim()) + "','" + dl.QoutesHandel(txt_Desc.Text.Trim()) + "','" + dl.QoutesHandel(txt_order.Text.Trim()) + "','" + dl.QoutesHandel(ddl_status.SelectedIndex.ToString()) + "','" + String.Format("{0:yyyy-MM-dd}", DateTime.Now) + "')"; dl.insertrecord(Query, lbl_mess); lbl_mess.Text = "Record Inserted Successfull.....!"; } }

推荐答案

这是我做的一个例子最近这个: Here is an example how i have done this recently: using (SqlConnection connDB = new SqlConnection()) using(SqlDataAdapter adapter = new SqlDataAdapter()) { connDB.ConnectionString = @"Server=SRV-WEB\SQLEXPRESS;Database=????;User Id=?????;Password=?????;"; adapter.SelectCommand = connDB.CreateCommand(); adapter.SelectCommand.CommandText = "SELECT [ConsultantID],[name] FROM [dbo].[ConsultantOptions] WHERE Enabled = 1 ORDER BY [name] ASC; "; using(DataTable data = new DataTable()) { adapter.Fill(data); data.Rows.Add(new string[] { "0", "All" }); DataView view = data.AsDataView(); view.Sort = "name"; this.cboConsultantName.DataSource = view; this.cboConsultantName.DataTextField = "name"; this.cboConsultantName.DataValueField = "ConsultantID"; this.cboConsultantName.DataBind(); this.cboConsultantName.SelectedValue = "All"; } }

要引用ID值,我会使用以下行:

To reference the ID value I would use the following line:

this.cboConsultantName.SelectedItem.Value

更多推荐

如何在下拉列表中选择默认值时指定id为零

本文发布于:2023-10-16 13:58:59,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1497771.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:为零   默认值   如何在   列表中   id

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!