下拉列表问题 [英] drop down list problem

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

问题描述

Dim sql1 As String = "select * from Office_Mast where Office_Cd='" & officeCd.Trim & "' order by Office_Name"
            Dim cmd3 As New SqlCommand(sql1, conn)
            Dim dr1 As SqlDataReader
            dr1 = cmd3.ExecuteReader
            ddl_editOffice.DataTextField = "Office_Name"
            ddl_editOffice.DataValueField = "Office_Cd"
            ddl_editOffice.DataBind()
            ddl_editOffice.Items.Insert(0, "--Select--")
            If dr1.IsClosed = False Then dr1.Close()
            ddl_editOffice.SelectedValue = officeCd







i得到这个执行时出错

'ddl_editOffice'有一个无效的SelectedValue,因为它在项目列表中不存在。参数名称:值

plz任何一个提供解决方案




i get this error while execute
'ddl_editOffice' has a SelectedValue which is invalid because it does not exist in the list of items. Parameter name: value
plz any one provide solution

推荐答案

这是我提供的一个类,你只需传递下拉id,表姓名和表格ID



Here is one class i've provided, you just pass dropdown id, table name and table id

public void PopulateDropDown(DropDownList ddlList, string tblName, string colName, string colValue, string srchCodn, Page form)
    {
        try
        {
            string sqlQuery = "Select 'Select' as " + colName + ",0 as " + colValue + " Union Select " + colName + "," + colValue + " from " + tblName;
            if (srchCodn != "")
            {
                sqlQuery += " " + srchCodn;
            }
            adp = new SqlDataAdapter(sqlQuery, conn);
            tbl = new DataTable();
            adp.Fill(tbl);
            ddlList.DataSource = tbl;
            ddlList.DataTextField = colName;
            ddlList.DataValueField = colValue;
            ddlList.DataBind();
        }
        catch (Exception ex)
        {
            MsgBox("Populate DropDown Error: " + ex.Message, form);
        }
    }





希望这可以帮助你绑定下拉列表。快乐编码。



Hope this helps you to bind dropdown. Happy coding.


实际问题是,当我从数据库访问其值后有空格的值,所以我在收到的值后使用trim并解决问题。

感谢所有这些都为我提供了解决方案。
actually problem was that when i access value from database its having spaces after value so i use trim after received value and solve problem.
Thanks to all to provide me solution.


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

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