[英]Javascript Function Not Firing from C# Function ASP.Net
嘗試開發如下屏幕時,我正在使用asp:Repeater控件: 提交表單
當在TextChanged事件上輸入ToolId時,我將MaxToolLife的值存儲為兩倍。 當用戶輸入的值大於ToolLife字段的存儲值時,我需要顯示“是/否”彈出窗口,說明“輸入的值大於現有值。您要繼續嗎?” 按鈕提交或文本更改事件。 目前,我正在使用以下代碼,但沒有收到Javascript警報。
protected void btnSubmit_Click(object sender, EventArgs e)
{
if (Convert.ToDouble(txtToolLifeAchieved.Text) > maxToolLife)
{
Page.ClientScript.RegisterStartupScript(Page.GetType(), "Confirm", "javascript:Confirm();", true);
if (hdnconfirm.Value=="Yes")
{
row["Pcs Produced"] = Convert.ToDouble(txtToolLifeAchieved.Text);
}
else
{
txtToolLifeAchieved.Text = "1";
txtToolLifeAchieved.Focus();
return;
}
}
else
{
row["Pcs Produced"] = Convert.ToDouble(txtToolLifeAchieved.Text);
}
}
代替Page.ClientScript,我還使用了“ Page.ClientScript.RegisterClientScriptBlock”和/ScriptManager.RegisterStartupScript。 到目前為止,沒有任何工作。 我無法從后面的代碼調用javascript函數。 任何立即的反應都會有很大幫助。
Confirm()如下所示:
function Confirm() { var confirm_value = document.createElement("INPUT"); confirm_value.type = "hidden"; confirm_value.name = "confirm_value"; if (confirm("The data already exists. Do you want to Overwrite data?")) { confirm_value.value = "Yes"; document.getElementById('<%= hdnconfirm.ClientID %>').value = "Yes"; } else { confirm_value.value = "No"; document.getElementById('<%= hdnconfirm.ClientID %>').value = "No"; } document.forms[0].appendChild(confirm_value); }
因此,我將堅持您的原始代碼,這就是您的服務器方法應如下所示:
// This is just to illustrate the limit
private const double MaxToolLife = 100;
protected void btnSubmit_Click(object sender, EventArgs e)
{
// Check the limit first
if (Convert.ToDouble(txtToolLifeAchieved.Text) > MaxToolLife)
{
// If the hidden field is empty show the confirm
// By default the hidden field is empty, it means that user just
// pressed the submit button but not the confirmation dialog
if (string.IsNullOrWhiteSpace(hdnconfirm.Value))
{
ClientScript.RegisterStartupScript(this.GetType(), "Confirm", "Confirm();", true);
return;
}
// If the hidden field is not empty, this postback was made by the confirm
// So check for the value of the hidden field
if (hdnconfirm.Value == "Yes")
{
// Handle 'Yes'
}
else
{
// Handle 'No'
}
}
else
{
// The limit is not reached
}
}
我在上面省略了您的特定代碼。 表單如下所示:
<form id="form1" runat="server">
<!-- Hidden field for the confirm result -->
<asp:HiddenField ID="hdnconfirm" runat="server" />
<!-- Text box for user input -->
<asp:TextBox ID="txtToolLifeAchieved" runat="server"></asp:TextBox>
<!-- Submit button -->
<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" />
</form>
比對的前form
在頁面標簽,把你的JavaScript函數,它必須是形式標記之前的地方,因為調用ClientScript.RegisterStartupScript
將在您的末尾添加腳本form
標記和你那個時候Confirm()
方法來定義。 這是您的javascript方法:
<script type="text/javascript">
function Confirm() {
if (confirm("The data already exists. Do you want to Overwrite data?")) {
document.getElementById('<%= hdnconfirm.ClientID %>').value = "Yes";
}
else {
document.getElementById('<%= hdnconfirm.ClientID %>').value = "No";
}
// Post the form back to server using the '__EVENTTARGET' hidden field
var form = document.getElementById("form1");
// Create hidden field
var input = document.createElement("input");
input.setAttribute("type", "hidden");
input.setAttribute("name", "__EVENTTARGET");
input.setAttribute("id", "__EVENTTARGET");
input.setAttribute("value", '<%= btnSubmit.ClientID %>');
// Append input to the form element
form.appendChild(input);
// Submit the form
form.submit();
}
</script>
無需為確認結果創建另一個隱藏字段。 而是恰到好處的確認后,您需要創建的隱藏字段name
設置為__EVENTTARGET
,而隱藏字段的值必須是提交按鈕的名稱,並不僅僅是調用submit
表單上的方法。 這會將案例回發到服務器,同時hdnconfirm
設置為Yes或No,同時執行btnSubmit_Click
方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.