我有一個(gè) DropDownList,其中填充了 SQL 表中的數(shù)據(jù)。在網(wǎng)絡(luò)表單中,當(dāng)用戶從該列表中選擇一個(gè)項(xiàng)目時(shí),我希望它將所選選項(xiàng)插入到另一個(gè) SQL 表中,除了 DropDownLists 之外的所有內(nèi)容都有效我試過(guò)了 :cmd.Parameters.AddWithValue("@*", ddl*.SelectedValue);cmd.Parameters.AddWithValue("@*", ddl*.SelectedItem.Text);ETCusing System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using System.Data.SqlClient;using System.Windows.Forms;using System.Data;using System.Configuration;using System.Text;using System.Drawing;namespace VXUK2{ public partial class booking_system : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { // SQL Query For DropDownList1 (CIT Company) SqlConnection con = new SqlConnection(); con.ConnectionString = ("Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=C:\\****;Persist Security Info=True;User ID=***;Password=****;Connect Timeout=30"); con.Open(); SqlCommand cmd = new SqlCommand("Select CIT_ID, CIT_CompanyName from CIT_Details", con); ddlCITCompany.DataSource = cmd.ExecuteReader(); ddlCITCompany.DataTextField = "CIT_CompanyName"; ddlCITCompany.DataValueField = "CIT_ID"; ddlCITCompany.DataBind(); // SQL Query for DropDownList2 (Site Details) SqlConnection con2 = new SqlConnection(); con2.ConnectionString = ("Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=C:\\***;Persist Security Info=True;User ID=***;Password=****;Connect Timeout=30"); con2.Open(); SqlCommand cmd2 = new SqlCommand("Select Site_ID, Site_Name from Site_Details", con2); ddlVisitingCentre.DataSource = cmd2.ExecuteReader(); ddlVisitingCentre.DataTextField = "Site_Name"; ddlVisitingCentre.DataValueField = "Site_ID"; ddlVisitingCentre.DataBind(); }
1 回答

aluckdog
TA貢獻(xiàn)1847條經(jīng)驗(yàn) 獲得超7個(gè)贊
您必須添加 not isPostBack,因?yàn)槊看握{(diào)用它的任何函數(shù)都會(huì)重新加載整個(gè)網(wǎng)頁(yè),因此每次您可以看到第一個(gè)數(shù)據(jù)都被選中。
通過(guò)添加 !IsPostBack,頁(yè)面將不會(huì)隱式重新加載數(shù)據(jù)。
這可以解決您選擇數(shù)據(jù)的問(wèn)題。
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
//Your Code
}
}
- 1 回答
- 0 關(guān)注
- 117 瀏覽
添加回答
舉報(bào)
0/150
提交
取消