【发布时间】:2018-12-01 11:59:09
【问题描述】:
我有一个案例,文本框的值是在页面加载时从数据库中获取的。单击按钮时,我使用 javascript 更新文本框。但是当我尝试将值保存到数据库时,更新的新值不存在。如何保存通过 javascript 更新的信息。请帮帮我。
下面的 javascript 函数将信息添加到文本框 ApproverListObj。在页面加载时,我使用选择查询从数据库中填充所有需求。
function AddButtonApproverList() {
var ApproverListObj = document.getElementById("<%=ApproverList.ClientID%>");
var ApproverListDDObj = document.getElementById("<%=ApproverListDD.ClientID%>");
var output;
if (ApproverListObj.value.includes(ApproverListDDObj.options[ApproverListDDObj.selectedIndex].text) == false) {
document.getElementById("<%=ChangesMade.ClientID%>").value = "1";
output = ApproverListObj.value.concat(ApproverListDDObj.options[ApproverListDDObj.selectedIndex].text, "; \n");
ApproverListObj.value = output;
}
return false;
}
使用 javascript 更新代码后,在提交按钮上,我将更新数据库,如下所示。
protected void SubmitClaim_Click(object sender, EventArgs e)
{
string cmdString = null, approverList = null;
DisposeConnIfRequired();
Conn = new SqlConnection(LoginDbCS);
approverList = "[" + ClaimTypeDD.SelectedItem.Text + "ApproverList]";
cmdString = ("UPDATE dbo.AdminDetails SET " + approverList + "=@ApproverList");
SqlCommand cmd = new SqlCommand(cmdString, Conn);
try
{
cmd.Parameters.Add("ApproverList", SqlDbType.NVarChar).Value = ApproverList.Text;
if (Conn.State.Equals(ConnectionState.Closed))
{
Conn.Open();
cmd.ExecuteNonQuery();
Conn.Close();
}
}
catch (Exception Ex)
{
if (Conn != null)
Conn.Dispose();
Application["TheException"] = Ex;
Response.Redirect("ErrPage.aspx", false);
return;
}
}
但是当我检查 ApproverList 时,使用 javascript 所做的更改不存在。我不确定这里发生了什么。
【问题讨论】:
-
请在此处使用 或在 codepen 中发布您当前的代码,以便我们提供帮助。
-
@Saddy 已经更新了后面的代码和javascript。
标签: c# jquery asp.net webforms postback