DropDownList 連動

摘要:DropDownList 連動

第一步驟:設定DropDownList的AutoPostBack屬性為True是為了當有觸發事件時可以PostBack到Server

第二步驟:將UpdatePanel的UpdateMode屬性設定為Conditional,當Triggers定義的控制項及事件觸發才會進行更新的動作

第三步驟:需要設定Triggers,定義好哪個控制項進行哪種事件的觸發進行Update的動作

 

.aspx的Source Code

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="AddJob.aspx.cs" Inherits="Job_Web.AddJob" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
    <style type="text/css">
        #css_table {
            display: table;
        }

        .css_tr {
            display: table-row;
        }

        .css_td {
            display: table-cell;
        }
    </style>
</head>
<body>
    <a href="JobList.aspx">回Job List</a>
    <form runat="server">

        <div id="css_table">
            <div class="css_tr">
                <div class="css_td">
                    <asp:Label Text="JOB_NO" runat="server" />
                </div>
                <div class="css_td">
                    <asp:DropDownList ID="ddlJobNo" runat="server" DataTextField="JOB_NAME" DataValueField="JOB_NO" AutoPostBack="true" OnSelectedIndexChanged="ddlJobNo_SelectedIndexChanged" />
                </div>
            </div>


            <div class="css_tr">
                <div class="css_td">
                    <asp:Label Text="SERVICE_ID" runat="server"></asp:Label>
                </div>
                <div class="css_td">
                    <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
                    <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="false">
                        <ContentTemplate>
                            <asp:DropDownList ID="ddlService" runat="server" DataValueField="SERVICE_ID" DataTextField="SERVICE_ID" />
                        </ContentTemplate>
                        <Triggers>
                            <asp:AsyncPostBackTrigger ControlID="ddlJobNo"  EventName="SelectedIndexChanged" />
                        </Triggers>
                    </asp:UpdatePanel>
                </div>
            </div>

            <div class="css_tr">
                <div class="css_td">
                    <asp:Label Text="EVN_ID" runat="server" />
                </div>
                <div class="css_td">
                    <asp:DropDownList ID="ddlEvnID" runat="server">
                        <asp:ListItem Value="1">dev</asp:ListItem>
                        <asp:ListItem Value="2">stage</asp:ListItem>
                        <asp:ListItem Value="3">product</asp:ListItem>
                    </asp:DropDownList>
                </div>
            </div>
            <div class="css_tr">
                <div class="css_td">
                    <asp:Label Text="SVALUE1" runat="server"></asp:Label>
                </div>
                <div class="css_td">
                    <asp:TextBox ID="txtSValue1" runat="server"></asp:TextBox>
                </div>
            </div>
            <div class="css_tr">
                <div class="css_td">
                    <asp:Label Text="SVALUE2" runat="server"></asp:Label>
                </div>
                <div class="css_td">
                    <asp:TextBox ID="txtSValue2" runat="server"></asp:TextBox>
                </div>
            </div>
            <div class="css_tr">
                <div class="css_td">
                    <asp:Label Text="NVALUE1" runat="server"></asp:Label>
                </div>
                <div class="css_td">
                    <asp:TextBox ID="txtNValue1" runat="server"></asp:TextBox>
                </div>
            </div>
            <div class="css_tr">
                <div class="css_td">
                    <asp:Label Text="NVALUE2" runat="server"></asp:Label>
                </div>
                <div class="css_td">
                    <asp:TextBox ID="txtNValue2" runat="server"></asp:TextBox>
                </div>
            </div>
            <div class="css_tr">
                <div class="css_td">
                    <asp:Button ID="btnSave" Text="儲存" runat="server" OnClick="btnSave_Click" />
                </div>
            </div>
        </div>
    </form>
</body>
</html>

 

.cs語法

 public partial class AddJob : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.SetDDLJobItemSource();
                this.SetDDLServiceSource(this.ddlJobNo.SelectedValue);
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            Service.IJobService m_client = new Service.JobServiceClient();
            JobInfo m_job = new JobInfo();
            m_job.JobNo = this.ddlJobNo.SelectedValue;
            m_job.ServiceID = Guid.Parse(this.ddlService.SelectedValue);
            m_job.EvnID = int.Parse(this.ddlEvnID.SelectedValue);
            m_job.NValue1 = string.IsNullOrEmpty(this.txtNValue1.Text.Trim()) ? 0 : int.Parse(this.txtNValue1.Text.Trim());
            m_job.NValue2 = string.IsNullOrEmpty(this.txtNValue2.Text.Trim()) ? 0 : int.Parse(this.txtNValue1.Text.Trim());
            m_job.SValue1 = this.txtSValue1.Text;
            m_job.SValue2 = this.txtSValue2.Text;
            m_job.StatusFlag = 1;
            m_job.EmplNO = "test";
            string[] m_msg = m_client.CreateJob(m_job);
            if (m_msg[0].Equals("00"))
            {
                Response.Write(string.Format(@"", "建立成功", "Job ID ==> " + m_msg[2]));
            }
        }

        private DataTable GetJobItem()
        {
            T08S01_BL m_T08S01_BL = new T08S01_BL();
            return m_T08S01_BL.GetJobItem();
        }

        private void SetDDLJobItemSource()
        {
            this.ddlJobNo.DataSource = this.GetJobItem();

            this.ddlJobNo.DataBind();
        }

        private DataTable GetService(string pJobNO)
        {
            T08S01_BL m_T08S01_BL = new T08S01_BL();
            DataTable m_table = m_T08S01_BL.GetService(pJobNO);
            if (m_table != null)
            {
                DataRow m_row = m_table.NewRow();
                m_row[0] = "00000000-0000-0000-0000-000000000000";
                m_table.Rows.InsertAt(m_row, 0);
            }
            return m_table;
        }

        private void SetDDLServiceSource(string pJobNO)
        {
            this.ddlService.DataSource = this.GetService(pJobNO);
            this.ddlService.DataBind();
        }

        protected void ddlJobNo_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.ddlJobNo.SelectedIndex != -1)
                this.SetDDLServiceSource(this.ddlJobNo.SelectedValue);
        }
       
    }