ListControl控件联动

第一种方式(通过autopostback)

 

ContractedBlock.gif ExpandedBlockStart.gif Code
<asp:ListBox ID="ListBox1" Rows=8 Width="130px" AppendDataBoundItems="true" DataSourceID="Sqldatasource2"  DataTextField="t_name" DataTextFormatString="{0}>>" DataValueField="t_id" AutoPostBack="true" runat="server">
    
<asp:ListItem Text="--------请选择--------" Value="" />
</asp:ListBox>
<asp:ListBox ID="ListBox2" Rows=8 Width="120px" DataSourceID="Sqldatasource3"  DataTextField="t_name" DataValueField="t_id" runat="server">
    
<asp:ListItem Text="--------请选择--------" Value="" />                                                            
</asp:ListBox>

<asp:SqlDataSource id="SqlDataSource3" runat="server" SelectCommand="select t_id,t_name from wt_type where t_upid=@upid order by t_id" ConnectionString="<%$ ConnectionStrings:CnnString%>" >
    
<SelectParameters>
        
<asp:ControlParameter ControlID="ListBox1" Name="upid" PropertyName="selectedvalue"   />
    
</SelectParameters>
</asp:SqlDataSource> 

<asp:SqlDataSource id="SqlDataSource2" runat="server" SelectCommand="select t_id,t_name from wt_type where t_upid='' or t_upid is null order by t_id" ConnectionString="<%$ ConnectionStrings:CnnString%>" >
</asp:SqlDataSource>

//设定 ListBox是否在数据绑定中清除<asp:ListItem Text="--------请选择--------" Value="" />列表项

AppendDataBoundItems="true|false"

//绑定TEXT和对应的VALUE

DataTextField="t_name"和DataValueField="t_id"

//格式化绑定的字符串

DataTextFormatString="{0}>>"

// 是否自动产生向服务器的回发

AutoPostBack="true|false"


controlparameter介绍

PropertyName 属性是可选的,而通常设置 ControlIDPropertyName 属性,以便 Evaluate 方法正确绑定到控件。如果设置 ControlID 属性 (Property) 而不设置 PropertyName 属性 (Property),则 Evaluate 方法试图使用 ControlValuePropertyAttribute 属性 (Attribute) 来标识默认的 PropertyName 属性 (Property)。(指定此属性是控件作者的责任)。如果失败,Evaluate 会引发 ArgumentException 异常。

下表标识哪些 ASP.NET 控件使用 ControlValuePropertyAttribute 属性 (Attribute) 来修饰属性 (Property)。

控件

属性

Calendar

SelectedDate

CheckBox

Checked

DataList

SelectedValue

DetailsView

SelectedValue

FormView

SelectedValue

GridView

SelectedValue

Label

Text

ListControl

SelectedValue

Menu

SelectedValue

TextBox

Text

TreeView

SelectedValue

第2种方式(通过onselectedindexchanged事件和autopostback)

 

界面

ContractedBlock.gif ExpandedBlockStart.gif Code
<asp:DropDownList ID="ddlcollege" runat="server"  AutoPostBack="true">
</asp:DropDownList>

<asp:DropDownList ID="ddlclass" runat="server" AppendDataBoundItems="true">
</asp:DropDownList>

底层函数库

ContractedBlock.gif ExpandedBlockStart.gif Code
    public void InitCombolistbydb(DropDownList obj, string sqlstr, string selectvalue)
    {
        DataTable dt 
= base.Getdatatable(sqlstr);
        
        
for (int i=0; i < dt.Rows.Count;i++ )
        {
            ListItem newitem 
= new ListItem();
            newitem.Value 
= dt.Rows[i][0].ToString();
            newitem.Text 
= dt.Rows[i][1].ToString();

            
if (newitem.Text.Trim() == selectvalue.Trim())
            {
                obj.SelectedIndex 
= i;
            }
            obj.Items.Add(newitem);
        }
    }
    
public void remove(DropDownList obj)
    {
        
for (int i = 0; i < obj.Items.Count;i=i)
        {
            obj.Items.RemoveAt(
0);
        }
    }
    
public void remove(DropDownList obj, int tag)
    {
        
for (int i = 0; i < tag; i++)
        {
            obj.Items.RemoveAt(obj.Items.Count
-1);
        }
    }

 逻辑层

ContractedBlock.gif ExpandedBlockStart.gif Code
    protected void Page_Load(object sender, EventArgs e)
    {
        
if(!page.ispostback)
        {
            Binddata();
        }
    }

    
public void Binddata()
    {
        BindDdl();
        BindDdl1();
    }
    
    
public void BindDdl()
    {
        StringBuilder sb 
= new StringBuilder();
        sb.AppendFormat(
"select collegeid,name from t_college");
        fun.InitCombolistbydb(ddlcollege, sb.ToString(), 
"");
    }
    
public void BindDdl1()
    {
        fun.remove(ddlclass);
        StringBuilder sb 
= new StringBuilder();
        sb.AppendFormat(
"select classid,name from t_class where collegeid={0}"int.Parse(ddlcollege.SelectedValue.ToString()));
        fun.InitCombolistbydb(ddlclass, sb.ToString(), 
"");
    }
    
protected void ddlcollege_indexchanged(object sender, EventArgs e)
    { 
        BindDdl1();
    }

 这种方式需要注意的是每次postback都没有删除listcontrol中原有的项(自定义的InitCombolistbydb()只是

obj.items.add())所以需要在写一个自定义的remove()函数

转载于:https://www.cnblogs.com/tangjian/archive/2009/02/26/1398726.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值