UpdatePanel控件(二)——二级DropDownList联动(2)

来源:互联网 发布:c语言中exit 0 编辑:程序博客网 时间:2024/05/17 22:39

UpdatePanel控件包含Update()方法。在异步回传过程中,你可以使用该方法以编程方式更新UpdatePanel中的内容。

UpdatePanel控件何时更新其内容是由两个属性决定的:UpdateMode和ChildrenAsTriggers。如果UpdateMode="Conditional" ChildrenAsTriggers="false",(ChildrenAsTriggers="false"即没有定义任何触发器),那么更新UpdatePanel控件内容的唯一方式就是调用Update()方法。不能用二级DropDownList联动(1)的方法做。

 

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="CascadingDropDownsNoAjax.aspx.cs"
    Inherits="sample_CascadingDropDownsNoAjax" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Cascading DropDwonList Controls</title>
</head>
<body>
    <form id="form1" runat="server">
    <div style="width: 100%;">
        <asp:ScriptManager ID="sm1" runat="server">
        </asp:ScriptManager>
        <div style="width: 10%; float: left;">
            <asp:UpdatePanel ID="UpdatePanel3" UpdateMode="Conditional" ChildrenAsTriggers="false"
                runat="server">
                <ContentTemplate>
                    <asp:DropDownList ID="DropDownList1" AutoPostBack="True" runat="server" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"
                        Width="100%">
                    </asp:DropDownList>
                </ContentTemplate>
            </asp:UpdatePanel>
        </div>
        <div style="width: 10%;">
            <asp:UpdatePanel ID="UpdatePanel2" runat="server">
                <ContentTemplate>
                    <asp:DropDownList ID="DropDownList2" runat="server" Width="100%" CssClass="ddlMark"
                        ForeColor="Red">
                    </asp:DropDownList>
                </ContentTemplate>
            </asp:UpdatePanel>
        </div>
    </div>
    </form>
</body>
</html>

 

注意:第二个ContentTemplate最好写上。

后台:

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;

public partial class sample_CascadingDropDownsNoAjax : System.Web.UI.Page
{
    private Student Stu = new Student();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BinGrid();
        }
    }
    private void BinGrid()
    {
        DropDownList1.DataSource = Stu.Student_Select();
        DropDownList1.DataTextField = "sname";
        DropDownList1.DataValueField = "sid";
        DropDownList1.DataBind();
        DropDownList1.Items.Insert(0, new ListItem("请选择姓名", ""));
        DropDownList2.Items.Insert(0, new ListItem("请选择成绩", ""));
    }
    /// <summary>
    /// 以编程方式更新UpdatePanel,利用的是UpdatePanel控件包含的Update()方法
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        string sid = DropDownList1.SelectedValue;
        DropDownList2.DataSource = Stu.Mark_Select(sid);
        DropDownList2.DataTextField = "marks";
        DropDownList2.DataValueField = "uid";
        DropDownList2.DataBind();
    }
}

 

原创粉丝点击