ASP.NET: Dynamically set Text and Value fields for
來源:互聯網
上載者:User
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)