我试图在人力资源相关请求的窗体上设置级联下拉列表,并根据所选部门显示位置。如果我已经知道我在做什么,这可能会有所帮助,但整个项目是我第一次进入ASP.NET和C#,所以我正在学习。这就是说,我的位置下拉的编码方式类似于这样:ASP.NET/C#SelectedIndexChanged没有触发
<asp:ScriptManager ID="SCRMAN_PositionDropdown" runat="server" />
<asp:DropDownList ID="SEL_Position" tabindex="7" runat="server" AutoPostBack="true">
<asp:ListItem>Select a Department first</asp:ListItem>
</asp:DropDownList>
<span style="font-size:larger;">←</span>If "Other" specify in the Notes box
编辑:这里是dman2036指出,我显然绑这整个过程中对错误的下拉改变基础上@了一些改版后如何我部下拉编码。由于某种原因,结尾标签没有显示,但它在那里。 <fsst_custom:DropDownListGrouped ID="SEL_Department" TabIndex="6" runat="server" OnSelectedIndexChanged="SEL_Department_SelectedIndexChanged" required >
<% - 脚本将OPTIONS - %> '
C#的应控制这种如下(在相关部分)的代码。 SEL_Department
正在填充正确,但由于某种原因(可能在我的脚本中一些荒谬的错误)SelectedIndexChange
不是解雇。我可能没有包含一些“使用”声明或者我错误地调用它? 注:我认为它没有开火,因为更改SEL_Department至少应该清除SEL_Position的“选择部门第一”项目。
using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.DirectoryServices;
using System.Linq;
using System.Management;
using System.Security.Principal;
using System.Text;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net.Mail;
using FSST;
using FSST.Controls;
public partial class Casino_HR_Request : System.Web.UI.Page
{
#region Constructor
public Casino_HR_Request()
{
Load += new EventHandler(Page_Load);
}
#endregion
#region Department_And_Position
protected void SEL_Department_SelectedIndexChanged(object sender, EventArgs e)
{
SEL_Position.Items.Clear();
switch(SEL_Department.SelectedValue)
{
case "Executive":
SEL_Position.Items.Add(new ListItem("Select a position", ""));
SEL_Position.Items.Add(new ListItem("General Manager", "General Manager"));
SEL_Position.Items.Add(new ListItem("Assistant General Manager", "Assistant General Manager"));
SEL_Position.Items.Add(new ListItem("Internal Auditor", "Internal Auditor"));
SEL_Position.Items.Add(new ListItem("Compliance Officer", "Compliance Officer"));
SEL_Position.Items.Add(new ListItem("Executive Assistant", "Executive Assistant"));
SEL_Position.Items.Add(new ListItem("Other", "Other"));
break;
case "Finance":
SEL_Position.Items.Add(new ListItem("Select a position", ""));
SEL_Position.Items.Add(new ListItem("Controller", "Controller"));
SEL_Position.Items.Add(new ListItem("Assistant Controller", "Assistant Controller"));
SEL_Position.Items.Add(new ListItem("Cage Manager", "Cage Manager"));
SEL_Position.Items.Add(new ListItem("Cage Supervisor", "Cage Supervisor"));
SEL_Position.Items.Add(new ListItem("Cage Cashier", "Cage Cashier"));
SEL_Position.Items.Add(new ListItem("Count Manager", "Count Manager"));
SEL_Position.Items.Add(new ListItem("Assistant Count Manager", "Assistant Count Manager"));
SEL_Position.Items.Add(new ListItem("Count Team", "Count Team"));
SEL_Position.Items.Add(new ListItem("Senior Staff Accountant", "Senior Staff Accountant"));
SEL_Position.Items.Add(new ListItem("Accounts Payable", "Accounts Payable"));
SEL_Position.Items.Add(new ListItem("Accounts Receivable", "Accounts Receivable"));
SEL_Position.Items.Add(new ListItem("Purchasing", "Purchasing"));
SEL_Position.Items.Add(new ListItem("Receiving Clerk", "Receiving Clerk"));
SEL_Position.Items.Add(new ListItem("Senior Accounting Auditor", "Senior Accounting Auditor"));
SEL_Position.Items.Add(new ListItem("Accounting Auditor", "Accounting Auditor"));
SEL_Position.Items.Add(new ListItem("Other", "Other"));
break;
// A BUNCH MORE CASES GO HERE
}
}
#endregion
#region Page_Load
void Page_Load(object sender, EventArgs e)
{
//----------STATUS CHANGE MENU OPTIONS----------
SEL_StatusChange.Items.Add(NewGroupedListItem("New Hire", "New Hire", "Onboard or Separate"));
SEL_StatusChange.Items.Add(NewGroupedListItem("Separation", "Separation", "Onboard or Separate"));
SEL_StatusChange.Items.Add(NewGroupedListItem("Position Change", "Position Change", "Changes in employment"));
SEL_StatusChange.Items.Add(NewGroupedListItem("Add 2nd- or 3rd-Rate", "Add 2nd- or 3rd-Rate", "Changes in Employment"));
SEL_StatusChange.Items.Add(NewGroupedListItem("Remove 2nd- or 3rd-Rate", "Remove 2nd- or 3rd-Rate", "Changes in employment"));
SEL_StatusChange.Items.Add(NewGroupedListItem("Add listed access", "Add listed access", "Change in needs only"));
SEL_StatusChange.Items.Add(NewGroupedListItem("Remove listed access", "Remove listed access", "Change in needs only"));
//----------DEPARTMENT MENU OPTIONS----------
SEL_Department.Items.Add(NewGroupedListItem("", "Department Name", ""));
SEL_Department.Items.Add(NewGroupedListItem("Executive", "Executive", "Select a department"));
SEL_Department.Items.Add(NewGroupedListItem("Finance", "Finance", "Select a department"));
SEL_Department.Items.Add(NewGroupedListItem("Food and Beverage", "Food and Beverage", "Select a department"));
SEL_Department.Items.Add(NewGroupedListItem("Gaming Commission", "Gaming Commission", "Select a department"));
SEL_Department.Items.Add(NewGroupedListItem("Gas Stations", "Gas Stations", "Select a department"));
SEL_Department.Items.Add(NewGroupedListItem("Hotel", "Hotel", "Select a department"));
SEL_Department.Items.Add(NewGroupedListItem("Human Resources", "Human Resources", "Select a department"));
SEL_Department.Items.Add(NewGroupedListItem("IT", "IT", "Select a department"));
SEL_Department.Items.Add(NewGroupedListItem("Maintenance", "Maintenance", "Select a department"));
SEL_Department.Items.Add(NewGroupedListItem("Marketing", "Marketing", "Select a department"));
SEL_Department.Items.Add(NewGroupedListItem("Player's Club", "Player's Club", "Select a department"));
SEL_Department.Items.Add(NewGroupedListItem("Porters", "Porters", "Select a department"));
SEL_Department.Items.Add(NewGroupedListItem("Security", "Security", "Select a department"));
SEL_Department.Items.Add(NewGroupedListItem("Slots", "Slots", "Select a department"));
SEL_Department.Items.Add(NewGroupedListItem("Surveillance", "Surveillance", "Select a department"));
SEL_Department.Items.Add(NewGroupedListItem("Table Games", "Table Games", "Select a department"));
}
protected ListItem NewGroupedListItem(string value, string text, string group)
{
ListItem li = new ListItem(value, text);
li.Attributes.Add("optgroup", group);
return li;
}
#endregion
//TWO MORE REGIONS NOT RELEVANT TO THIS GO HERE
}
下面是DropdownListGrouped
的代码,其中一个朋友送我,我只是合并为他告诉我的。
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace FSST.Controls
{
/// <summary>
/// Provides options for grouping list items.
/// </summary>
/// <remarks>
/// Much came from here:
/// http://stackoverflow.com/questions/130020/dropdownlist-control-with-optgroups-for-asp-net-webforms
/// </remarks>
public class DropDownListGrouped : System.Web.UI.WebControls.DropDownList
{
const string OptionGroupTag = "optgroup";
const string OptionTag = "option";
#region DataGroupingField
public string DataGroupingField
{
get
{
object o = ViewState["DataGroupingField"];
return (o == null) ? "" : (string)o;
}
set
{
ViewState["DataGroupingField"] = value;
}
}
#endregion
#region PerformDataBinding
protected override void PerformDataBinding(System.Collections.IEnumerable dataSource)
{
base.PerformDataBinding(dataSource);
if (DataGroupingField.Length == 0)
return;
// Go back through the data source and assign the attributes
// We know the items were added to the end, so just work through them
if (dataSource != null)
{
ICollection collection = dataSource as ICollection;
int startIndex = 0;
if (collection != null)
startIndex = this.Items.Count - collection.Count;
string dataGroupingField = DataGroupingField;
foreach (object current in dataSource)
Items[startIndex++].Attributes.Add(OptionGroupTag, DataBinder.GetPropertyValue(current, DataGroupingField, null));
}
}
#endregion
#region RenderContents
protected override void RenderContents(System.Web.UI.HtmlTextWriter writer)
{
ListItemCollection items = this.Items;
int count = items.Count;
string tag;
string optgroupLabel;
if (count > 0)
{
bool flag = false;
string prevOptGroup = null;
for (int i = 0; i < count; i++)
{
tag = OptionTag;
optgroupLabel = null;
ListItem item = items[i];
if (item.Enabled)
{
if (item.Attributes != null && item.Attributes.Count > 0 && item.Attributes[OptionGroupTag] != null)
{
optgroupLabel = item.Attributes[OptionGroupTag];
if (prevOptGroup != optgroupLabel)
{
if (prevOptGroup != null)
writer.WriteEndTag(OptionGroupTag);
writer.WriteBeginTag(OptionGroupTag);
if (!string.IsNullOrEmpty(optgroupLabel))
writer.WriteAttribute("label", optgroupLabel);
writer.Write('>');
}
item.Attributes.Remove(OptionGroupTag);
prevOptGroup = optgroupLabel;
}
else
{
if (prevOptGroup != null)
writer.WriteEndTag(OptionGroupTag);
prevOptGroup = null;
}
writer.WriteBeginTag(tag);
if (item.Selected)
{
if (flag)
this.VerifyMultiSelect();
flag = true;
writer.WriteAttribute("selected", "selected");
}
writer.WriteAttribute("value", item.Value, true);
if (item.Attributes != null && item.Attributes.Count > 0)
item.Attributes.Render(writer);
if (optgroupLabel != null)
item.Attributes.Add(OptionGroupTag, optgroupLabel);
if (this.Page != null)
this.Page.ClientScript.RegisterForEventValidation(this.UniqueID, item.Value);
writer.Write('>');
HttpUtility.HtmlEncode(item.Text, writer);
writer.WriteEndTag(tag);
writer.WriteLine();
if (i == count - 1 && prevOptGroup != null)
writer.WriteEndTag(OptionGroupTag);
}
}
}
}
#endregion
#region SaveViewState
protected override object SaveViewState()
{
object[] state = new object[this.Items.Count + 1];
object baseState = base.SaveViewState();
state[0] = baseState;
bool itemHasAttributes = false;
for (int i = 0; i < this.Items.Count; i++)
{
if (this.Items[i].Attributes.Count > 0)
{
itemHasAttributes = true;
object[] attributes = new object[this.Items[i].Attributes.Count * 2];
int k = 0;
foreach (string key in this.Items[i].Attributes.Keys)
{
attributes[k] = key;
k++;
attributes[k] = this.Items[i].Attributes[key];
k++;
}
state[i + 1] = attributes;
}
}
if (itemHasAttributes)
return state;
return baseState;
}
#endregion
#region LoadViewState
protected override void LoadViewState(object savedState)
{
if (savedState == null)
return;
if (!(savedState.GetType().GetElementType() == null) &&
(savedState.GetType().GetElementType().Equals(typeof(object))))
{
object[] state = (object[])savedState;
base.LoadViewState(state[0]);
for (int i = 1; i < state.Length; i++)
{
if (state[i] != null)
{
object[] attributes = (object[])state[i];
for (int k = 0; k < attributes.Length; k += 2)
{
this.Items[i - 1].Attributes.Add
(attributes[k].ToString(), attributes[k + 1].ToString());
}
}
}
}
else
{
base.LoadViewState(savedState);
}
}
#endregion
}
}
我很困惑... SEL_Position没有任何项目,除非SelectedIndexChanged被调用,所以SelectedIndexChanged如何在没有项目可供选择时触发?您是否打算将此事件列入SEL_Department?这意味着,当我选择一个部门时,您然后加载职位? – dman2306
在'if(!Page.IsPostBack)'中包装你在page_load中的东西。目前您正在重新填充每个页面加载的ddl – Andrei
否您在哪里显示'DropDownListGrouped'的代码,因此无法知道此自定义控件为什么不触发事件。它是否需要像标准的DropDownList一样的'AutoPostBack'?我无法知道。 – dman2306