asp.net Dropdownlist被删除后没有刷新

时间:2017-04-06 15:00:17

标签: javascript c# asp.net .net webforms

我有2个下拉列表,其中包含公司名称和公司地址以及链接到存储过程的删除按钮。即使我在该地址下拉列表上调用databind(),也不会刷新地址。有人能指出我正确的方向吗?

//Button to remove Company
    protected void btnremovecompany_2(object sender, EventArgs e)
    {
        if (ddlcompanyaddress2.SelectedIndex != 0) /*checked to see if an address is Selected first*/
        {
            string confirmValue = Request.Form["confirm_value"];
            if (confirmValue == "Yes")/* if yes is clicked then procedd to deactivate that company address*/
            {
                String strConnString = ConfigurationManager.ConnectionStrings["DBConnection"].ConnectionString;
                SqlConnection con = new SqlConnection(strConnString);
                SqlCommand cmd = new SqlCommand();
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = "usp_update_company_active";
                cmd.Parameters.Add("@companyname", SqlDbType.VarChar).Value = ddlcompany2.SelectedItem.Text.Trim();
                cmd.Parameters.Add("@address", SqlDbType.VarChar).Value = ddlcompanyaddress2.SelectedItem.Text.Trim();
                cmd.Parameters.Add("@category", SqlDbType.VarChar).Value = ddlTrades2.SelectedItem.Text.Trim();
                cmd.Connection = con;
                **ddlcompanyaddress2.DataBind();**
                try
                {
                    con.Open();
                    cmd.ExecuteNonQuery();

                }
                catch (Exception ex)
                {
                    throw ex;
                }
                finally
                {   
                    /*Display message saying company is deactivated*/
                    string message = "Company  has been removed";
                    string script = "window.onload = function(){ alert('";
                    script += message;
                    script += "')};";
                    ClientScript.RegisterStartupScript(this.GetType(), "SuccessMessage", script, true);
                    con.Close();
                    con.Dispose();                        
                }                    
            }
            else
            {
                /*if canceled is clicked then display no changes*/
                this.Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('No Changes were made!')", true);
            }
        }
        else
        {
            string message = "Please pick an address first.";
            string script = "window.onload = function(){ alert('";
            script += message;
            script += "')};";
            ClientScript.RegisterStartupScript(this.GetType(), "SuccessMessage", script, true);
        }

    }

2 个答案:

答案 0 :(得分:0)

您可能缺少下拉列表的数据源。我无法在您的代码中找到数据源。您刚刚调用了数据绑定方法。 你能绑定数据源吗?

答案 1 :(得分:0)

将此代码添加到try {}区域,并使用新数据重置所有下拉列表。

ddlgettrades2();
getcompany2(ddlTrades2.SelectedItem.Text);
getaddress2(ddlcompany2.SelectedItem.Text);