【发布时间】:2017-04-06 15:00:17
【问题描述】:
我有一个包含公司名称和公司地址的 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);
}
}
【问题讨论】:
-
可能是您的 proc 名称暗示更新而不是删除,另外,您在执行 proc 之前绑定了列表。
-
@DaveBecker 所以数据绑定在更新时不起作用?我将 databind() 移到了 try{} 中,但它仍然不起作用。
-
storedproc 从数据库中删除数据?下拉列表的数据源是什么?您是否将下拉列表绑定到 page_load 中的数据源?
-
@ChetanRanpariya 存储过程只是将活动列设置为 0。DDL 仅填充活动为 =1 的行。 DDL 的数据源是公司表中的一列。不,我没有在页面加载中绑定它我有 3 个动态 DDL。当第一个被选中时,下一个填充,然后从第二个填充第三个(我想要重新绑定的地址)。
-
当您更改数据库中的任何数据时(标记行 IsActive=0)不会影响绑定到 DDL 的数据源,因为它不再连接,除非您使用 SQL DataSource 填充 DDL .因此,您需要做的是使用与第一次绑定或选择其他 DDL 时相同的逻辑重新绑定 DDL。
标签: javascript c# asp.net .net webforms