欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#中DropDownList動(dòng)態(tài)生成的方法

 更新時(shí)間:2015年03月27日 14:47:20   作者:小啊斌  
這篇文章主要介紹了C#中DropDownList動(dòng)態(tài)生成的方法,實(shí)例分析了C#中DropDownList的使用技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下

本文實(shí)例講述了C#中DropDownList動(dòng)態(tài)生成的方法。分享給大家供大家參考。具體分析如下:

今天學(xué)習(xí)了DropDownList的動(dòng)態(tài)生成 以及asp.net的生命周期,還有ViewState,因?yàn)轫?xiàng)目需要根據(jù)需求動(dòng)態(tài)生成條件 所以研究了一部分代碼來(lái)完成這些工作,好了話不多說(shuō)了,來(lái)看代碼

首先是網(wǎng)頁(yè)的代碼:

<span style="font-size:18px;"><body>
  <form id="form1" runat="server">
  <div>
    <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
 //placeholder控件相當(dāng)于一個(gè)容器,用于存放其他控件
    <br />
    <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button"/></div>
  </form>
</body></span>

下面是c#程序

public partial class _Default : System.Web.UI.Page
{
  private CategoryBLL _categoryBLL = new CategoryBLL();
  private Category _category = new Category();
  private void Page_Init(object sender, System.EventArgs e)
  //可以看一下asp的生命周期
  {
    BindDrpClass();
  }
  protected void Page_Load(object sender, EventArgs e)
  {
    string str = "8/14/19/25";
 //數(shù)據(jù)庫(kù)讀取
    string[] arr = str.Split('/');
    if (arr.Length == PlaceHolder1.Controls.Count)
 //防止新增類(lèi)別時(shí)讀取出錯(cuò)
    {
      for (int i = 0; i < PlaceHolder1.Controls.Count; i++)
      {
        if (PlaceHolder1.Controls[i] is DropDownList)
        {
          ((DropDownList)PlaceHolder1.Controls[i]).SelectedValue = arr[i];
        }
      }
    }
  }
  private void BindDrpClass()
  {
    DataTable dt = _categoryBLL.GetCategory();
    DataRow[] drs = dt.Select("pid=0");
    foreach (DataRow dr in drs)
    {
      string id = dr["id"].ToString();
      string name = dr["name"].ToString();
      DropDownList ddl = new DropDownList();
      //具體添加的操作
      ddl.Items.Clear();
      ddl.ID = "ddl" + id;
      ddl.Items.Add(new ListItem("-" + name + "-", id));
      PlaceHolder1.Controls.Add(ddl);
      int sonparentid = int.Parse(id);
      BindDrpNode(sonparentid, dt, ddl);
    }
  }
  private void BindDrpNode(int parentid, DataTable dt, DropDownList ddl)
  {
    DataRow[] drs = dt.Select("pid= " + parentid);
    foreach (DataRow dr in drs)
    {
      string id = dr["id"].ToString();
      string name = dr["name"].ToString();
      ddl.Items.Add(new ListItem(name, id));
      PlaceHolder1.Controls.Add(ddl);
    }
  }
  protected void Button1_Click(object sender, EventArgs e)
  {
    string category = "";
    string category2 = "";
    foreach (Control childControl in PlaceHolder1.Controls)
    {
      if (childControl is DropDownList) 
      {
        category += "/" + ((DropDownList)childControl).SelectedItem.Text;
        category2 += "/" + ((DropDownList)childControl).SelectedValue;
      }
    }
    if (category.Length > 0)
      category = category.Remove(0, 1);
    Response.Write(category);
    Response.Write("<br />"); 
    if (category2.Length > 0)
      category2 = category2.Remove(0, 1);
    Response.Write(category2);
  }
}

希望本文所述對(duì)大家的C#程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • C# 串口接收數(shù)據(jù)中serialPort.close()死鎖的實(shí)例

    C# 串口接收數(shù)據(jù)中serialPort.close()死鎖的實(shí)例

    下面小編就為大家分享一篇C# 串口接收數(shù)據(jù)中serialPort.close()死鎖的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2017-11-11
  • C#中word導(dǎo)出功能的騷操作詳解

    C#中word導(dǎo)出功能的騷操作詳解

    這篇文章主要給大家介紹了關(guān)于C#中word導(dǎo)出功能騷操作的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-02-02
  • C#交錯(cuò)數(shù)組用法實(shí)例

    C#交錯(cuò)數(shù)組用法實(shí)例

    這篇文章主要介紹了C#交錯(cuò)數(shù)組用法,較為詳細(xì)的分析了交錯(cuò)數(shù)組的概念、用法并實(shí)例分析了交錯(cuò)數(shù)組的使用技巧,需要的朋友可以參考下
    2015-04-04
  • C# 7.2中結(jié)構(gòu)體性能問(wèn)題的解決方案

    C# 7.2中結(jié)構(gòu)體性能問(wèn)題的解決方案

    這篇文章主要給大家介紹了關(guān)于C# 7.2中結(jié)構(gòu)體性能問(wèn)題的解決方案,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2018-08-08
  • C#實(shí)現(xiàn)無(wú)限級(jí)聯(lián)下拉列表框

    C#實(shí)現(xiàn)無(wú)限級(jí)聯(lián)下拉列表框

    這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)無(wú)限級(jí)聯(lián)下拉列表框的相關(guān)資料,感興趣的小伙伴們可以參考一下
    2016-03-03
  • 用C#將圖片保存至Oracle BLOB字段中的方法

    用C#將圖片保存至Oracle BLOB字段中的方法

    這篇文章主要介紹了用C#將圖片保存至Oracle BLOB字段中的方法, 依靠ImageViewer庫(kù)進(jìn)行操作,需要的朋友可以參考下
    2015-07-07
  • Unity實(shí)現(xiàn)簡(jiǎn)單換裝系統(tǒng)

    Unity實(shí)現(xiàn)簡(jiǎn)單換裝系統(tǒng)

    這篇文章主要為大家詳細(xì)介紹了Unity實(shí)現(xiàn)簡(jiǎn)單換裝系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-04-04
  • 最新評(píng)論