通过C#将数据绑定到下拉列表和选定值 [英] Bind data to dropdownlist and selectedvalue by C#

查看:480
本文介绍了通过C#将数据绑定到下拉列表和选定值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两张桌子:



类别:CatID,CatName

新闻:NewsID,CatID,标题,内容



现在,我想,当我在表NewS上编辑时,我有下拉列表并绑定表类别中的所有值并显示之前的选定值



请帮帮我



我的尝试:



I have two table:

Category : CatID, CatName
News : NewsID, CatID,Title,Content

Now, i want, when i edit on table NewS, i have dropdownlist and bind all values from table Category and show SelectedValue previous

Please help me

What I have tried:

try
                    {
                        SqlConnection conn = new SqlConnection(connStr);
                        SqlDataReader reader = null;
                 
                        conn.Open();
                        string sql = "SELECT  * FROM NEWS where NewsID= " + sid + "";
                      

                        SqlCommand comm = new SqlCommand();
                        comm.Connection = conn;
                        comm.CommandText = sql;

                        reader = comm.ExecuteReader();

                    while (reader.Read())
                     {
                            string Newsid = reader.GetValue(reader.GetOrdinal("Newsid")).ToString();
                            string CatID = reader.GetValue(reader.GetOrdinal("CatID")).ToString();
                            
                            string Title = reader.GetValue(reader.GetOrdinal("Title")).ToString();
                            string Content = reader.GetValue(reader.GetOrdinal("Content")).ToString();
                           


                           

                            if (Newsid != "")
                         {

                            
                             SqlDataReader reader = null;
                             string sql = "SELECT  CatID, CatName FROM Category  order by Catid desc";

                             SqlCommand comm = new SqlCommand();
                             comm.Connection = conn;
                             comm.CommandText = sql;
                             reader = comm.ExecuteReader();
                            
                             while (reader.Read())
                             {
                                 string CatID_root = reader.GetValue(reader.GetOrdinal("CatID")).ToString();
                                 string CatName = reader.GetValue(reader.GetOrdinal("CatName")).ToString();
                               
                                
                                 if (CatID_root == CatID)
                                 {
                                    
                                     Dropdownlist.DataSource = reader;
                                     Dropdownlist.DataValueField = "CatID";
                                     Dropdownlist.DataTextField = "CatName";
                                     Dropdownlist.SelectedValue = CatName;                                   
                                     Dropdownlist.DataBind();
                                    
                                    
                                 }
                                 else
                                 {
                                     Dropdownlist.DataSource = reader;
                                     Dropdownlist.DataValueField = "id";
                                     Dropdownlist.DataTextField = "bulkname";
                                     Dropdownlist.DataBind();
                                    

                                 }

                             }

                         }
                    } 

推荐答案

你好

SelectedValue属性选择CatID字段的值。



更改你的代码这个:

Hello
The SelectedValue property select the value of the CatID field.

Change your code for this:
Dropdownlist.SelectedValue = CatID;





如果你想使用要在控件中找到记录的CatName字段,请使用以下代码:



If you want to use the CatName field to find your record in the control, use this code:

Dropdownlist.SelectedIndex = Dropdownlist.Items.IndexOf(Dropdownlist.Items.FindByText(CatName));





现在他的最终代码如下:





Now his final code would look like this:

Dropdownlist.DataSource = reader;
Dropdownlist.DataValueField = "CatID";
Dropdownlist.DataTextField = "CatName";
Dropdownlist.DataBind();

Dropdownlist.SelectedValue = CatID;

or 

Dropdownlist.SelectedIndex = Dropdownlist.Items.IndexOf(Dropdownlist.Items.FindByText(CatName));





我希望我帮助过你。



I hope I have helped you.


这篇关于通过C#将数据绑定到下拉列表和选定值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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