Quote:ผมลองแล้วมันไม่ได้อ่ะครับ
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DropDownListTest.aspx.cs" Inherits="DropDownListTest" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:DropDownList ID="DropDownList1" runat="server"> </asp:DropDownList> <br /> <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label> </div> </form> </body> </html>
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.Configuration; public partial class DropDownListTest : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { //เรียก sql connection string จาก web.config string ConnectionString = WebConfigurationManager.ConnectionStrings["SqlConnectionString"].ToString(); //เขียน sql command ในการ query ข้อมูลจาก db string CommandString = "Select * From [Global_UserClass]"; //ใช้ sqldatasource เป็นตัวกลางในการติดต่อ db SqlDataSource DataSource = new SqlDataSource("System.Data.SqlClient", ConnectionString, CommandString); DataSource.DataSourceMode = SqlDataSourceMode.DataSet; if (!IsPostBack) { //bind data ให้ dropdownlist DropDownList1.DataSource = DataSource; DropDownList1.DataTextField = "ClassName"; DropDownList1.DataValueField = "ClassID"; DropDownList1.DataBind(); } //กำหนดให้ dropdownlist สามารภ postback ได้ DropDownList1.AutoPostBack = true; //จัดการ event handle ให้ event SelectedIndexChanged ไปทำคำสั่งที่เราต้องการ DropDownList1.SelectedIndexChanged += new EventHandler(DropDownList11_SelectedIndexChanged); } protected void DropDownList11_SelectedIndexChanged(object sender, EventArgs e) { //ให้ label แสดงข้อมูลที่เลือก Label1.Text = "[" + DropDownList1.SelectedItem.Value + "] - " + DropDownList1.SelectedItem.Text; } }
เพื่อความปลอดภัยของเว็บบอร์ด ไม่อนุญาติให้แทรก แท็ก [img]....[/img] โดยการอัพโหลดไฟล์รูปจากที่อื่น เช่นเว็บไซต์ ฟรีอัพโหลดต่าง ๆ อัพโหลดแทรกรูปภาพ ให้ใช้บริการอัพโหลดไฟล์ของไทยครีเอท และตัดรูปภาพให้พอดีกับสกรีน เพื่อความโหลดเร็วและไฟล์ไม่ถูกลบทิ้ง