0
我已經創建了2個DropDownLists第一個下拉列表包含「TeamName」,第二個包含「TeamMember」名稱。要求是當我們從下拉列表中選擇特定的TeamName時,第二個下拉列表應該自動填充團隊成員名稱,並且在按鈕單擊數據應該插入數據庫之後,它對第一次按鈕點擊工作正常,但第二次在同一頁面上「TeamName」下拉列表不工作顯示部門名稱,突然它會丟失。所以請告訴我我需要做什麼來解決這個問題?如何在asp.net中點擊按鈕後在下拉列表中存儲值?
以下是我做到了實現這一任務
User.aspx page code:
代碼
User.aspx.cs頁面代碼:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindTeamName();
txtCurrentDate.Text = DateTime.Now.ToString("MM-dd-yyyy");
txtCurrentDate.ForeColor = System.Drawing.Color.Green;
}
}
private void BindTeamName()
{
SqlConnection con = new SqlConnection(SqlString);
con.Open();
SqlCommand cmd = new SqlCommand("select * from TeamName", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
con.Close();
ddl1.DataSource = ds;
ddl1.DataTextField = "TeamName";
ddl1.DataValueField = "TeamId";
ddl1.DataBind();
ddl1.Items.Insert(0, new ListItem("--Select--", "0"));
}
protected void ddl1_SelectedIndexChanged(object sender, EventArgs e)
{
int TeamId = Convert.ToInt32(ddl1.SelectedValue);
SqlConnection con = new SqlConnection(SqlString);
con.Open();
SqlCommand cmd = new SqlCommand("select * from TeamResource where TeamId=" + TeamId, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
con.Close();
ddl2.DataSource = ds;
ddl2.DataTextField = "EmpName";
ddl2.DataValueField = "EmpId";
ddl2.DataBind();
ddl2.Items.Insert(0, new ListItem("--Select--", "0"));
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
if (Page.IsValid)
{
using (SqlConnection con = new SqlConnection(SqlString))
{
SqlCommand cmd = new SqlCommand("InsertUserData", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@WorkDate", txtCurrentDate.Text);
cmd.Parameters.AddWithValue("@TeamName", ddl1.SelectedItem.Text);
cmd.Parameters.AddWithValue("@TeamMember", ddl2.SelectedItem.Text);
cmd.Parameters.AddWithValue("@AvailableBandwidth", ddlAvailable.SelectedItem.Value);
con.Open();
int ReturnCode = (int)cmd.ExecuteScalar();
if (ReturnCode == -1)
{
lblMsg.Text = "Already Data present";
lblMsg.ForeColor = System.Drawing.Color.Red;
ClearFields();
}
else
{
lblMsg.Text = "Data inserted successfully";
lblMsg.ForeColor = System.Drawing.Color.Green;
ClearFields();
}
}
謝謝您的幫助,現在它工作正常 – svs