我的項目是在asp.net中的webform1中包含一個文本框和一個保存 按鈕,當用戶在文本框中輸入一些國家名稱時,它應該然後點擊保存按鈕名稱應該被保存在數據庫表中名爲 的國家。 然後在webform2中,保存的國家應該在頁面加載的下拉列表中加載,然後應該有一些文本框,其中用戶將在下拉列表中 輸入所選國家的狀態, 和上次在webform3國家/地區加載下拉列表中的用戶選擇國家/地區時,它應該在另一個 下拉列表中加載該國家的狀態,並且當用戶從第二個下拉列表中選擇一個狀態並且 進入該州的城市時,它應該被保存在數據庫中 點擊保存按鈕。 我有問題,當我運行webform2的國家得到加載在下拉列表1,但是當我選擇一個國家,它不會加載 該國在dropdownlist2國家。 有幫助嗎? 這裏是我的代碼,到目前爲止,ASP.Net C#和微軟SQL連接
using 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.Data;
namespace assign1 {
public partial class city: System.Web.UI.Page {
protected void Page_Load(object sender, EventArgs e) {
if (IsPostBack == false) {
SqlConnection conn = new SqlConnection("Data Source = HAFIZ_HARRON; Database = 7thSemester; Integrated Security = true");
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = new SqlCommand("SELECT CName,CID FROM country", conn);
DataTable dt = new DataTable();
da.Fill(dt);
ddlCountry.DataSource = dt;
ddlCountry.DataTextField = "CName";
ddlCountry.DataValueField = "CID";
ddlCountry.DataBind();
}
}
protected void btnSave_Click(object sender, EventArgs e) {
SqlConnection conn = new SqlConnection("Data Source = HAFIZ_HARRON; Database = 7thSemester; Integrated Security = true");
SqlCommand cmd = new SqlCommand("INSERT INTO city(CIName,SID,CID) Values('" + txtCity.Text + "','" + ddlState.SelectedIndex + "','" + ddlCountry.SelectedIndex + "')", conn);
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
}
protected void ddlState_SelectedIndexChanged(object sender, EventArgs e) {
/* SqlConnection conn = new SqlConnection("Data Source = HAFIZ_HARRON; Database = 7thSemester; Integrated Security = true");
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = new SqlCommand("SELECT SName,SID FROM state", conn);
DataTable dt = new DataTable();
da.Fill(dt);
ddlState.DataSource = dt;
ddlState.DataTextField = "SName";
ddlState.DataValueField = "SID";
ddlState.DataBind(); */
}
protected void ddlCountry_SelectedIndexChanged(object sender, EventArgs e) {
SqlConnection conn = new SqlConnection("Data Source = HAFIZ_HARRON; Database = 7thSemester; Integrated Security = true");
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = new SqlCommand("SELECT SName,SID FROM state", conn);
DataTable dt = new DataTable();
da.Fill(dt);
ddlState.DataSource = dt;
ddlState.DataTextField = "SName";
ddlState.DataValueField = "SID";
ddlState.DataBind();
}
}
}
----------
選擇狀態爲特定的國家需要你設定的條件國家=? ??否則你加載你的表中的每個狀態。這是現在發生嗎?表_state_的結構是什麼? – Steve
表狀態我使用了國家表中的stateid,stateName和(countryid) – harron
SqlConnection conn = new SqlConnection(「Data Source = HAFIZ_HARRON; Database = 7thSeter; Integrated Security = true」); SqlDataAdapter da = new SqlDataAdapter(); da.SelectCommand = new SqlCommand(「SELECT SName,SID FROM state WHERE CID =」,conn); DataTable dt = new DataTable(); da.Fill(dt); 我如何設置條件的國家da.SelectCommand =新的SqlCommand(「選擇SName,SID從狀態WHERE CID = ???」,康涅狄格州); ??? – harron