首页 > 编程 > .NET > 正文

ASP.NET 2.0中给dropDownList服务器控件添加项的新方法

2024-07-10 13:06:20
字体:
来源:转载
供稿:网友

介绍asp.net 2.0的新特性。是《asp.net2.0应用开发技术》一书的延续。

 

在asp.net 2.0中,可以在数据绑定时,通过设置dropdownlist的appenddatabounditems属性为true,在数据绑定之前添加一个新的项目,并且这个新加的项目会保存在viewstate之中。下面就是一个实现的例子:

c#代码

<%[email protected] page language="c#" %>
<%[email protected] import namespace="system.data" %>
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en"
 "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">

<script runat="server">...
  icollection createdatasource()
  ...{
    datatable dt = new datatable();
    datarow dr;
    dt.columns.add(new datacolumn("id", typeof(int32)));
    dt.columns.add(new datacolumn("text", typeof(string)));
    for (int i = 0; i < 6; i++)
    ...{
      dr = dt.newrow();
      dr[0] = i;
      dr[1] = "列表项目 " + i.tostring();
      dt.rows.add(dr);
    }
    dataview dv = new dataview(dt);
    return dv;
  }
  protected void button1_click(object sender, eventargs e)
  ...{
    response.write("<li>dropdownlist1 您选择的项目:" + dropdownlist1.selectedvalue
      + " ; " + dropdownlist1.selecteditem.text);
    response.write("<li>dropdownlist2 您选择的项目:" + dropdownlist2.selectedvalue
      + " ; " + dropdownlist2.selecteditem.text);
  }

  protected void page_load(object sender, eventargs e)
  ...{
    if (!ispostback)
    ...{
      dropdownlist1.appenddatabounditems = true;
      dropdownlist1.items.add(new listitem("-- 请选择一个选择项 --", ""));
      dropdownlist2.datasource = dropdownlist1.datasource = createdatasource();
      dropdownlist2.datatextfield = dropdownlist1.datatextfield = "text";
      dropdownlist2.datavaluefield = dropdownlist1.datavaluefield = "id";
      dropdownlist1.databind();
      dropdownlist2.databind();
    }
  }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
  <title>dropdownlist 补充例子</title>
</head>
<body>
<form id="form1" runat="server">
  <asp:dropdownlist id="dropdownlist1" runat="server">
  </asp:dropdownlist>
  <asp:dropdownlist id="dropdownlist2" runat="server" appenddatabounditems="true">
  <asp:listitem text="请选择" value=""></asp:listitem>
  </asp:dropdownlist>
  <asp:button id="button1" runat="server" text="得到选择的值" onclick="button1_click" />
</form>
</body>
</html>

vb.net代码

<%[email protected] page language="vb" autoeventwireup="true" %>

<%[email protected] import namespace="system.data" %>
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en"
 "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">

<script runat="server">...
  protected sub button1_click(byval sender as object, byval e as system.eventargs)
    response.write("<li>dropdownlist1 您选择的项目:" + dropdownlist1.selectedvalue + _
      " ; " + dropdownlist1.selecteditem.text)
    response.write("<li>dropdownlist2 您选择的项目:" + dropdownlist2.selectedvalue + _
      " ; " + dropdownlist2.selecteditem.text)
  end sub
 
  protected sub page_load(byval sender as object, byval e as system.eventargs)
    if not ispostback then
      dropdownlist1.appenddatabounditems = true
      dropdownlist1.items.add(new listitem("-- 请选择一个选择项 --", ""))
      dropdownlist2.datasource = createdatasource()
      dropdownlist1.datasource = createdatasource()
      dropdownlist2.datatextfield = "text"
      dropdownlist1.datatextfield = "text"
      dropdownlist2.datavaluefield = "id"
      dropdownlist1.datavaluefield = "id"
      dropdownlist1.databind()
      dropdownlist2.databind()
    end if
  end sub
 
  function createdatasource() as icollection
    dim dt as datatable = new datatable()
    dim dr as datarow
    dt.columns.add(new datacolumn("id", gettype(system.int32)))
    dt.columns.add(new datacolumn("text", gettype(string)))
    for i as integer = 0 to 6
      dr = dt.newrow()
      dr(0) = i
      dr(1) = "列表项目 " + i.tostring()
      dt.rows.add(dr)
    next
    dim dv as dataview = new dataview(dt)
    return dv
  end function

</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="head1" runat="server">
  <title>dropdownlist 补充例子</title>
</head>
<body>
  <form id="form1" runat="server">
    <asp:dropdownlist id="dropdownlist1" runat="server">
    </asp:dropdownlist>
    <asp:dropdownlist id="dropdownlist2" runat="server" appenddatabounditems="true">
      <asp:listitem text="请选择" value=""></asp:listitem>
    </asp:dropdownlist>
    <asp:button id="button1" runat="server" text="得到选择的值" onclick="button1_click" />
  </form>
</body>
</html>


 

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表