【发布时间】:2016-06-21 14:20:14
【问题描述】:
我从未在 C# .NET 中使用过用户控件,并且正在从事一个有 3 个下拉列表的项目,并且需要将它们放入用户控件中。我想知道我将如何去做这件事。我将发布我的下拉列表的代码,以及它们当前的外观以及它们应该是什么样的屏幕截图。谢谢。
public partial class _default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DropDownList1.DataBind();
DropDownList1_SelectedIndexChanged(sender, e);
DropDownList2.DataBind();
DropDownList2_SelectedIndexChanged(sender, e);
DropDownList3.DataBind();
}
}
// Drop Down List 1
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
XmlDataSource2.XPath = String.Format("mmsdata/mill[@n='{0}']/mach", DropDownList1.SelectedValue);
}
// Drop Down List 2
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
XmlDataSource3.XPath = String.Format("mmsdata/mill[@n='{0}']/mach[@n='{1}']/srn", DropDownList1.SelectedValue, DropDownList2.SelectedValue);
BindSensorList();
}
protected void BindSensorList()
{
//sender.GetType();
XmlDocument xdoc = XmlDataSource3.GetXmlDocument();
XmlNodeList nodes = xdoc.SelectNodes(XmlDataSource3.XPath);
var sensors = new List<Sensor>();
foreach (XmlNode node in nodes)
{
sensors.Add(new Sensor { id = node.Attributes["n"].Value, name = node.InnerText });
}
DropDownList3.DataSource = sensors;
DropDownList3.DataValueField = "id";
DropDownList3.DataTextField = "name";
DropDownList3.DataBind();
}
我希望它们看起来像什么
他们的样子
【问题讨论】: