ASP.NET: Dynamically set Text and Value fields for

This code was written in response to a message posted on one of Charles Carroll's ASP.NET lists. You can sign up for one or all of the lists here.
Code:

<%@ Page Language="VB" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<html>
<script language="VB" runat="server">
    Sub Page_Load(sender As Object, e As EventArgs)
    If Not IsPostBack Then
           DropDownList1.DataTextField = "pub_name"
       DropDownList1.DataValueField = "pub_id"
       BindList
        End If
    End Sub

    Sub ButtonClick(sender As Object, e As EventArgs)
    If sender.ID = "Button1" Then
       DropDownList1.DataTextField = "pub_id"
       DropDownList1.DataValueField = "pub_name"
    Else
           DropDownList1.DataTextField = "pub_name"
       DropDownList1.DataValueField = "pub_id"
    End If

    BindList

    Button1.Visible = "False"
    Button2.Visible = "True"
    End Sub

    Sub BindList()
    Dim myConnection As SqlConnection = new SqlConnection("Data Source=(local)\NetSDK; Trusted_Connection=Yes; Initial Catalog=pubs")

    Dim myCommand As SqlCommand = New SqlCommand("Select pub_id, pub_name From publishers", myConnection)
    
    myConnection.Open()

    DropDownList1.DataSource = myCommand.ExecuteReader(CommandBehavior.CloseConnection)
        DropDownList1.DataBind()

        DropDownList1.Items.Insert(0, "Select an Item")
    DropDownList1.SelectedIndex = 0
    End Sub

</script>
<body>
<form runat="Server">
<asp:DropDownList id="DropDownList1" runat="server" />
<asp:Button id="Button1" Text="Switch Em" runat="server" />
<asp:Button id="Button2" Text="Switch Em Back" Visible="False" runat="server" />
</form>
</body>
</html>

时间: 2024-10-03 13:41:18

ASP.NET: Dynamically set Text and Value fields for的相关文章

ASP.NET: Dynamically set Text and Value fields for a DropDownList

asp.net This code was written in response to a message posted on one of Charles Carroll's ASP.NET lists. You can sign up for one or all of the lists here. Code: <%@ Page Language="VB" %><%@ Import Namespace="System.Data" %>

asp.net中DetailsView的使用方法

aspx页面: 复制代码 代码如下: <asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" Height="50px" Width="500px" OnModeChanging="DetailsView1_ModeChanging" OnItemDeleting="Detail

asp编程中非常实用的例子

编程 1.如何用Asp判断你的网站的虚拟物理路径 答:使用Mappath方法 < p align="center" >< font size="4" face="Arial" >< b > The Physical path to this virtual website is: < /b >< /font > < font color="#FF0000" si

将Delphi作为ASP.NET的脚本语言

asp.net|脚本    将Delphi视为脚本语言       支持ASP.net的第一件事是让ASP.NET将Delphi视为脚本语言,让ASP.NET能够为各种ASP文件类型调用Delphi的.NET编译器.       ASP.NET要在IIS虚路径的根目录下寻找Web.config文件.下面是ASP.NET中使用Delphi作脚本语言的web.config配制文件内容:       <configuration>    <system.web>    <compi

ASP.NET 2.0的新增服务、控件与功能

asp.net|控件 [导读]全文介绍了ASP.NET 2.0中新增的控件和功能,包括Master Pages,Data Source控件.Skin的支持.GridView和Details View的引入,在此基础上,ASP.NET 2.0的安全模型得到了极大的提高,包括登录控件.角色管理器.个性化等等方面的支持,在类库方面也增加了许多新特性,如全新的代码分隔模型.客户端回调等等,让读者对于ASP.NET的新特性有一个全面的了解. 在首次公诸于众以来的短短四年中,ASP.NET业已成为在Wind

ASP.NET 2.0中实现模板中的数据绑定

asp.net|模板|数据 模板化的数据绑定控件为我们在页面上显示数据提供了根本的灵活性.你可能还记得ASP.NET v1.x中的几个模板化控件(例如DataList和Repeater控件).ASP.NET 2.0仍然支持这些控件,但在模板中绑定数据的语法已经被简化和改善了.本文将讨论在数据绑定控件模板中绑定数据的多种方法. 数据绑定表达式 ASP.NET 2.0改善了模板中的数据绑定操作,把v1.x中的数据绑定语法DataBinder.Eval(Container.DataItem, fiel

用asp.net写的论坛程序--论坛主页

1) forum.aspx :- The main forum page <%@ Page Language="C#" Debug="true" %><%@ Assembly Name="System.Data" %><%@ Import Namespace="System.Data.ADO" %><%@ Import Namespace="System.Data"

用asp.net写的论坛程序--浏览贴及回复

2) reply.aspx : The topic viewing and replying page <%@ Page Language="C#" EnableSessionState="False" Debug="True" %><%@ Import Namespace="System" %><%@ Assembly Name="System.Data" %><

ASP.NET2.0简化的控件与简化的数据绑定

asp.net|控件|数据   模板化数据绑定控件提供在页中呈现数据的最大灵活性.您可以回想一下 ASP.NET 1.x 版中的几个模板化控件,如 DataList 和 Repeater 控件.ASP.NET 2.0 继续支持这些控件,但在此版本中,在模板中对控件进行数据绑定的方式已得到简化和改进.本部分讨论在数据绑定控件模板中进行数据绑定的各种方式. <%# Container.DataItem("Name") %> <%# Container.DataItem(&