第一种方式(通过autopostback)

ListControl控件联动 


    <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)

ListControl控件联动 

界面

>

底层函数库

 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);
        }
    }

 逻辑层

 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()函数

相关文章:

  • 2021-12-24
  • 2021-08-25
  • 2021-06-07
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-01-01
  • 2022-01-03
  • 2021-11-14
  • 2021-07-16
  • 2021-10-02
  • 2022-12-23
相关资源
相似解决方案