我正在尝试将默认值返回到我的2下拉列表中。我已经使用我的SQLserver数据绑定了我的DDLpolice,同时通过项属性添加了DDLocation。只要管理员选择了某个位置,它就会显示可用的选项。
以下是2下拉列表
我在各自的DDL中有以下这些值
lblocation
DDLpolice(选择:TownA)
DDpolice(选择:TownB)
在我的数据库中,各自的Town都有各自的警察局
每当管理员选择一个特定的caseID,一个位置和一个警察时,它会自动更新数据库,这是完全正常的。因此,逻辑上它应该“刷新”DDL而不显示所选的policeID。然而,DDL的价值仍然存在。 我添加了一个loadgrid,以刷新我的gridview信息。我在DDL上添加了一个Item.Clear(),它完全清除了我的数据DDL。
public partial class AssignTask : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
LoadGrid();
}
}
private void LoadGrid()
{
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "Data Source =localhost;" +
"Initial Catalog = project; Integrated Security = SSPI";
conn.Open();
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter("SELECT memberreportid, typeofcrime, location, crdatetime, citizenreport, image1, image2, image3, image4, image5 FROM MemberReport where handle='unhandled' AND caseprogress='inprogress'", conn);
da.Fill(ds);
GWCase.DataSource = ds;
GWCase.DataBind();
conn.Close();
}
protected void GWCase_SelectedIndexChanged(object sender, EventArgs e)
{
lblCID.Text = GWCase.SelectedRow.Cells[1].Text;
}
protected void lblocation_SelectedIndexChanged(object sender, EventArgs e)
{
using (var connAdd = new SqlConnection("Data Source = localhost; Initial Catalog = project; Integrated Security= SSPI"))
{
connAdd.Open();
var sql = "Select policeid from LoginRegisterPolice where location='" + lblocation.SelectedItem.Text + "' AND status='available' AND handle='offcase'";
using (var cmdAdd = new SqlDataAdapter(sql, connAdd))
{
DataSet ds2 = new DataSet();
cmdAdd.Fill(ds2);
DDLpolice.DataSource = ds2;
DDLpolice.DataTextField = "policeid";
DDLpolice.DataValueField = "policeid";
DDLpolice.DataBind();
}
connAdd.Close();
}
}
protected void btnAssign_Click(object sender, EventArgs e)
{
if (lblCID.Text.Equals(""))
{
lblmsg.Text = "Please select one case";
return;
}
else if (lblocation.SelectedItem.Text.Equals("Select Location"))
{
lblmsg.Text = "Please select a location";
return;
}
else if (DDLpolice.SelectedItem.Text.Equals("Select Officer"))
{
lblmsg.Text = "Please select an officer";
return;
}
else
{
lblmsg.Text = "The case has been successfully assigned.";
}
using (var connAdd = new SqlConnection("Data Source = localhost; Initial Catalog = project; Integrated Security= SSPI"))
{
connAdd.Open();
var sql = "Update AdminAssign Set assign ='" + DDLpolice.SelectedItem.Text + "' where memberreportid='" + lblCID.Text + "'";
using (var cmdAdd = new SqlCommand(sql, connAdd))
{
cmdAdd.ExecuteNonQuery();
}
sql = "Insert into PoliceReport (memberreportid, policeid) values ('" + lblCID.Text.Trim() + "','" + DDLpolice.SelectedItem.Text + "')";
using (var cmdAdd = new SqlCommand(sql, connAdd))
{
cmdAdd.ExecuteNonQuery();
}
sql = "Update LoginRegisterPolice Set handle = '" + lblCID.Text + "' where policeid='" + DDLpolice.SelectedItem.Text + "'";
using (var cmdAdd = new SqlCommand(sql, connAdd))
{
cmdAdd.ExecuteNonQuery();
}
sql = "Update MemberReport Set handle = 'handled' where memberreportid='" + lblCID.Text + "'";
using (var cmdAdd = new SqlCommand(sql, connAdd))
{
cmdAdd.ExecuteNonQuery();
}
connAdd.Close();
}
LoadGrid();
lblCID.Text = "";
lblocation.Items.Clear();
DDLpolice.Items.Clear();
}
protected void DDLpolice_SelectedIndexChanged1(object sender, EventArgs e)
{
}
}
}
因此,我想知道如何恢复我的DDLocation,默认值和项值以及DDLpolice默认值。