DetailsView中的DropDownList如何與另一DropDownList連動

DetailsView中的DropDownList如何與另一DropDownList連動

今天在論壇上看到有討論如何讓DetailsView中的DropDownList互動,平常比較少用DetailsView順便藉機練習一下,說明如下:

  • 以下程式碼示範本文網頁的配置:
   1:  <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="DVDemo.aspx.cs" Inherits="WebApplication1.DVDemo" %>
   2:   
   3:  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
   4:   
   5:  <html xmlns="http://www.w3.org/1999/xhtml">
   6:  <head runat="server">
   7:      <title></title>
   8:  </head>
   9:  <body>
  10:      <form id="form1" runat="server">
  11:      <div>
  12:  <asp:detailsview ID="Detailsview1" runat="server" height="50px" width="125px" 
  13:              AutoGenerateRows="False" onitemcommand="Detailsview1_ItemCommand" 
  14:              onmodechanging="Detailsview1_ModeChanging" AllowPaging="True" 
  15:              DataKeyNames="RegionID" onpageindexchanging="Detailsview1_PageIndexChanging" >
  16:      <Fields>
  17:          <asp:BoundField DataField="RegionId" HeaderText="RegionId" ReadOnly="True" />
  18:          <asp:TemplateField HeaderText="RegionDescription">
  19:              <EditItemTemplate>
  20:                  <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("RegionDescription") %>'></asp:TextBox>
  21:                  <asp:DropDownList ID="DropDownList1" AutoPostBack="true" runat="server" 
  22:                      onselectedindexchanged="DropDownList1_SelectedIndexChanged">
  23:                  </asp:DropDownList>
  24:                  <asp:DropDownList ID="DropDownList2" runat="server">
  25:                  </asp:DropDownList>
  26:              </EditItemTemplate>
  27:              <InsertItemTemplate>
  28:                  <asp:TextBox ID="TextBox1" runat="server" 
  29:                      Text='<%# Bind("RegionDescription") %>'></asp:TextBox>
  30:              </InsertItemTemplate>
  31:              <ItemTemplate>
  32:                  <asp:Label ID="Label1" runat="server" Text='<%# Bind("RegionDescription") %>'></asp:Label>
  33:              </ItemTemplate>
  34:          </asp:TemplateField>
  35:          <asp:TemplateField HeaderText="Edit">
  36:              <ItemTemplate>
  37:                  <asp:Button ID="Button1" runat="server" Text="Edit" CommandName="Edit" />
  38:              </ItemTemplate>
  39:          </asp:TemplateField>
  40:      </Fields>
  41:          </asp:detailsview>    
  42:          <br />
  43:          <asp:Button ID="Button2" runat="server" Text="Save" onclick="Button2_Click" />
  44:      </div>
  45:      </form>
  46:  </body>
  47:  </html>

image

【程式碼說明】

筆者於DetailsView的ReadOnly模式中透過Button1的CommandName來觸發DetailsView的ItemCommand事件,再於事件中以DetailsView的ChangeMode方法來觸發DetailsView的ModeChanging事件。

  • 以下程式碼示範當使用者按下Edit按鈕時,觸發ItemCommand事件所進行的動作:
   1:  protected void Detailsview1_ItemCommand(object sender, DetailsViewCommandEventArgs e)
   2:  {
   3:      if (e.CommandName == "Edit")
   4:      {
   5:          Detailsview1.ChangeMode(DetailsViewMode.Edit);
   6:          DVBind();
   7:      }
   8:  }
  • 上述程式碼的DetailsView.ChangeMode方法會觸發ModeChanging事件,程式碼如下:
   1:  protected void Detailsview1_ModeChanging(object sender, DetailsViewModeEventArgs e)
   2:  {
   3:      if (Detailsview1.CurrentMode == DetailsViewMode.Edit)
   4:      {
   5:          using (NorthwindEntities context = new NorthwindEntities())
   6:          {
   7:              DropDownList ddl1 = Detailsview1.FindControl("DropDownList1") as DropDownList;
   8:              if (ddl1 != null)
   9:              {
  10:                  ddl1.DataSource = from p in context.Categories
  11:                                    select p;
  12:                  ddl1.DataTextField = "CategoryName";
  13:                  ddl1.DataValueField = "CategoryID";
  14:                  ddl1.DataBind();
  15:                  ddl1.Items.Insert(0, new ListItem("---", string.Empty));
  16:              }
  17:          }
  18:      }
  19:  }
  • 由於DropDownList1的AutoPostBack設定為True,因此當使用者改變選項時,將觸發DropDownList1_SelectedIndexChanged,程式碼如下:
   1:  protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
   2:  {
   3:      DropDownList ddl1 = sender as DropDownList;
   4:      if (!string.IsNullOrEmpty(ddl1.SelectedValue))
   5:      {
   6:          DropDownList ddl2 = Detailsview1.FindControl("DropDownList2") as DropDownList;
   7:          if (ddl2 != null)
   8:          {
   9:              using (NorthwindEntities context = new NorthwindEntities())
  10:              {
  11:                  int CategoryID = int.Parse(ddl1.SelectedValue);
  12:                  ddl2.DataSource = from p in context.Products
  13:                                    where p.CategoryID == CategoryID
  14:                                    select p;
  15:                  ddl2.DataTextField = "ProductName";
  16:                  ddl2.DataValueField = "ProductID";
  17:                  ddl2.DataBind();
  18:              }
  19:          }
  20:      }
  21:  }

【完整程式碼】

   1:  using System;
   2:  using System.Collections.Generic;
   3:  using System.Linq;
   4:  using System.Web;
   5:  using System.Web.UI;
   6:  using System.Web.UI.WebControls;
   7:   
   8:  namespace WebApplication1
   9:  {
  10:      public partial class DVDemo : System.Web.UI.Page
  11:      {
  12:          protected void Page_Load(object sender, EventArgs e)
  13:          {
  14:              if (!IsPostBack) DVBind();
  15:          }
  16:   
  17:          private void DVBind()
  18:          {
  19:              using (NorthwindEntities context = new NorthwindEntities())
  20:              {
  21:                  Detailsview1.DataSource = from p in context.Regions
  22:                                            select p;
  23:                  Detailsview1.DataBind();
  24:   
  25:              }
  26:          }        
  27:   
  28:          protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
  29:          {
  30:              DropDownList ddl1 = sender as DropDownList;
  31:              if (!string.IsNullOrEmpty(ddl1.SelectedValue))
  32:              {
  33:                  DropDownList ddl2 = Detailsview1.FindControl("DropDownList2") as DropDownList;
  34:                  if (ddl2 != null)
  35:                  {
  36:                      using (NorthwindEntities context = new NorthwindEntities())
  37:                      {
  38:                          int CategoryID = int.Parse(ddl1.SelectedValue);
  39:                          ddl2.DataSource = from p in context.Products
  40:                                            where p.CategoryID == CategoryID
  41:                                            select p;
  42:                          ddl2.DataTextField = "ProductName";
  43:                          ddl2.DataValueField = "ProductID";
  44:                          ddl2.DataBind();
  45:                      }
  46:                  }
  47:              }
  48:          }
  49:   
  50:          protected void Detailsview1_ModeChanging(object sender, DetailsViewModeEventArgs e)
  51:          {
  52:              if (Detailsview1.CurrentMode == DetailsViewMode.Edit)
  53:              {
  54:                  using (NorthwindEntities context = new NorthwindEntities())
  55:                  {
  56:                      DropDownList ddl1 = Detailsview1.FindControl("DropDownList1") as DropDownList;
  57:                      if (ddl1 != null)
  58:                      {
  59:                          ddl1.DataSource = from p in context.Categories
  60:                                            select p;
  61:                          ddl1.DataTextField = "CategoryName";
  62:                          ddl1.DataValueField = "CategoryID";
  63:                          ddl1.DataBind();
  64:                          ddl1.Items.Insert(0, new ListItem("---", string.Empty));
  65:                      }
  66:                  }
  67:              }
  68:          }
  69:   
  70:          protected void Detailsview1_ItemCommand(object sender, DetailsViewCommandEventArgs e)
  71:          {
  72:              if (e.CommandName == "Edit")
  73:              {
  74:                  Detailsview1.ChangeMode(DetailsViewMode.Edit);
  75:                  DVBind();
  76:              }
  77:          }
  78:   
  79:          protected void Button2_Click(object sender, EventArgs e)
  80:          {
  81:              string RegionID = string.Empty;
  82:              string RegionDescription = string.Empty;
  83:              string CategoryID = string.Empty;
  84:              string ProductID = string.Empty;
  85:   
  86:              if (Detailsview1.CurrentMode == DetailsViewMode.Edit)
  87:              {
  88:                  RegionID = Detailsview1.DataKey[0].ToString();
  89:                  TextBox txtRegionDesc = Detailsview1.FindControl("TextBox1") as TextBox;
  90:                  if (txtRegionDesc != null) RegionDescription = txtRegionDesc.Text;
  91:                  DropDownList ddl1 = Detailsview1.FindControl("DropDownList1") as DropDownList;
  92:                  if (ddl1 != null) CategoryID = ddl1.SelectedValue;
  93:                  DropDownList ddl2 = Detailsview1.FindControl("DropDownList2") as DropDownList;
  94:                  if (ddl2 != null) ProductID = ddl2.SelectedValue;
  95:              }
  96:   
  97:              Response.Write(string.Format("{0},{1},{2},{3}", RegionID, RegionDescription, CategoryID, ProductID));
  98:          }
  99:   
 100:          protected void Detailsview1_PageIndexChanging(object sender, DetailsViewPageEventArgs e)
 101:          {
 102:              Detailsview1.ChangeMode(DetailsViewMode.ReadOnly);
 103:              Detailsview1.PageIndex = e.NewPageIndex;
 104:              DVBind();
 105:          }      
 106:      }
 107:  }