2016-06-21 77 views
0

我從來沒有使用C#.NET用戶控件,和我工作的一個項目,我有3個dropdownlists,並需要把它們放入一個用戶控件。我想知道如何去做這件事。我將發佈我的下拉列表的代碼,以及屏幕抓取他們目前的樣子,以及他們應該看起來像什麼。謝謝。添加DropDownLists到用戶控件C#

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(); 
     } 

我希望他們看起來像

First picture

他們的樣子

Second Picture

回答

1

添加您的下拉列表,最簡單的方式向用戶控件是什麼在項目解決方案中創建用戶控件,然後放入在ui中使用p.net webform代碼,然後您可以使用與創建Web窗體相同的方式編寫代碼。

樣品用戶控制

<%@ Control Language="C#" ClassName="SampleUserControl" %> 
<h3> <u>User Control</u> </h3> 
<script runat=server> 
</script> 
Drop down 1: <asp:DropDownList id="ColorList" 
       AutoPostBack="True" 
       OnSelectedIndexChanged="Selection_Change" 
       runat="server"> 

       <asp:ListItem Selected="True" Value="White"> White </asp:ListItem> 
       <asp:ListItem Value="Silver"> Silver </asp:ListItem> 
       <asp:ListItem Value="DarkGray"> Dark Gray </asp:ListItem> 
       <asp:ListItem Value="Khaki"> Khaki </asp:ListItem> 
       <asp:ListItem Value="DarkKhaki"> Dark Khaki </asp:ListItem> 
      </asp:DropDownList> 
<asp:label id="Label1" runat=server/> 

可以在頁面上使用的用戶控制和動態加載的內容,只是初始化在頁面上您希望在使用它,並通過在值的方法相同你加載一個類/頁面

+0

「他們看起來像什麼」和「我想他們看起來像什麼」是後退,我需要他們是相反的,我修復了對圖片的評論。現在是正確 –

+0

@JordanDarland不僅僅是一個樣式問題?如果您在動態創建它們時將類或ID應用於下拉控件,則可以使用CSS創建樣式。 –