0

我正在尝试将默认值返回到我的 2 下拉列表中。在通过项目属性添加 DDLocation 时,我已将我的 DDLpolice 与我的 SQLserver 数据绑定。每当管理员选择一个位置时,它都会显示可用的选项。

这是2个下拉列表

  1. 定位
  2. DDL警察

我在相应的 DDL 中有以下值

  • 定位

    1. 选择位置(默认值)
    2. 选择 TownA(项目值)
    3. 选择 TownB(项目值)
  • DDLpolice(选择:TownA)

    1. 选择官员(默认值)
    2. 选择 123(项目值)
    3. 选择 124(项目值)
  • DDpolice(选择:TownB)

    1. 选择位置(默认值)
    2. 选择 126(项目值)
    3. 选择 127(项目值)

在我的数据库中,各个 Town 都有各自的policyID

  • A镇
    1. 123
    2. 124
  • B镇
    1. 126
    2. 127

每当管理员选择特定的 caseID、位置和警察 ID 时,它都会自动更新数据库,这非常好。因此,从逻辑上讲,它应该“刷新”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 默认值。

4

0 回答 0