将数据绑定到下拉列表 [英] binding data to the drop down list

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

问题描述



我有两个下拉列表
1-ddlcity
2-ddlBranch

我在SQL数据库中从tblCity绑定了ddlcity

代码在这里:

Hi,

I have two dropdownlist
1-ddlcity
2-ddlBranch

I bind the ddlcity from tblCity in sql database

the code is here:

Protected Sub Page_DataBinding(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.DataBinding
        Dim conn As New SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("schoolz").ConnectionString)
        conn.Open()
        Dim cmd As New SqlCommand("select strName from tblCity", conn)
        Dim dr As SqlDataReader
        dr = cmd.ExecuteReader()
        ddlCity.DataSource = dr
        ddlCity.DataTextField = "strName"
        ddlCity.DataBind()
        dr.Close()
        conn.Close()
    End Sub



我希望当我从ddlcity中选择城市时,所有分支将显示在第二个下拉ddlBranch中.

为此,我编写了存储过程



I want that when I select the city from ddlcity, then all the branch will display in the second dropdown ddlBranch.

For this I write the stored procedure

create proc spEditReport
@intReturnAdd int=0 output
as
begin
declare @add int
select strName from tblBranch where intAddressID=@intReturnAdd

select @add= tblAddress.intAddressID
from tblAddress  join tblCity 
on tblAddress.intAddressID=tblCity.intCityID
if @@error <> 0
    begin
        rollback transaction
        return @intReturnAdd
    end
    commit transaction
    set @intReturnAdd = @add
    return @intReturnAdd
end




选择城市时,有人可以告诉我如何编码以显示分支吗?


请帮帮我.
谢谢和问候
subiya




Can anybody tell me how to code to display the branches when I select the city?


please help me out.
thanks and regard
subiya

推荐答案

我有一个疑问,为什么使用存储过程来做到这一点?
您可以在包含城市的下拉列表的selectedIndex更改事件中简单地获取选定城市的ID,并且可以传递此ID以在where子句中获取该特定城市的受尊敬的分支机构.
然后将它们加载到另一个下拉列表中.

我在这里假设您已经在tblcity中声明了一个主键,该主键必须是tblBranch的外键.
尝试此操作可能会解决您的问题.
I have query that Why you are using stored procedure to do this?
You can simply fetch the ID of the Selected City in the selectedIndex changed event of your dropdownlist containing cities.And Can pass this ID to fetch the respected Branches of that particular city in where clause .
And then load them in the other dropdownlist .

I assume here that You have declared a primary key in tblcity which must be foreign key for tblBranch.
Try this may it can solve your Problem.


尝试此操作
关于ddlCity的选定索引更改事件
Try This
On selected Index Changed Event Of your ddlCity
 Dim sd As New SqlDataAdapter("select cityID from tableCity where cityName=''" + cmb1.SelectedText.ToString() + "''",connection)
        Dim dt As New DataTable
        sd.Fill(dt)
        If dt.Rows.Count > 0 Then
conn.Open()
        Dim cmd1 As New SqlCommand("select BranchNames from tableBranch where cityID=''"+dt.Rows (0).Item (0).ToString ()+"''", conn)
        Dim dr1 As SqlDataReader
        dr1 = cmd.ExecuteReader()
      ddlBranch.DataSource = dr1
        ddlBranch.DataTextField = "BranchName"
        ddlBranch.DataBind()
        dr1.Close()
        conn.Close()


根据您的需要实施它.我以Field为例,所以请检查并尝试


Implement this as Per Your need.I have taken Field Just For Example.So check it and Try


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

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