[英]ASP.NET C# backend, did the user press OK or Cancel from JavaScript confirm box?
在網站的C#后端代碼中,我有以下代碼,為用戶顯示一條消息,枚舉他們將要提交的數據,並要求人確認信息看起來准確。
這是在C#中執行的,而不是在JavaScript中執行的,那么如何訪問confirm
函數的結果呢?
如果單擊“確定”,我的C#邏輯需要遵循不同的路徑,而不是單擊“取消”。
我正在研究一個非常古老的應用程序(13年),這個應用程序已經有一段時間未被觸及了。
因此,我無法改變應用程序的一般設計。
ClientScript.RegisterStartupScript(this.GetType(), "InvalidEntrySubTypeAlert", "confirm('" +
"Are you sure you want to create the following deal?\n" +
"\nDeal ID :\t\t\t" + nDealID +
"\nCompany Name :\t\t" + txtCompanyName.Text +
"\nEntry Subject :\t\t" + txtEntrySubject.Text +
"\nBusiness Lead :\t\t" + ddlBusinessLead.SelectedItem.Text +
"\nLicense Status :\t\t" + ddlLicenseStatus.SelectedItem.Text +
"\nEffective Date :\t\t" + string.Format("{0:MM/dd/yyyy}", calEffectiveDate.SelectedDate) +
"\nExpiration Date :\t\t" + string.Format("{0:MM/dd/yyyy}", calExpirationDate.SelectedDate) +
"\nLicense Location :\t\t" + txtLicenseLocation.Text +
"\nEntry Sub Type :\t\t" + ddlEntrySubType.SelectedItem.Text.Split(' ')[0]
+ "');", true);
我想我會從客戶端調用頁面方法(帶有webmethod屬性的代碼后面的方法)或使用ajax來調用Web服務。
由於在客戶端執行確認操作,因此沒有直接鏈接到您的服務器端
您可以使用HiddenField
從客戶端設置值,然后在服務器上訪問它。
這是一個樣機
<asp:HiddenField ID="hndSetFromJS" runat="server"></asp:HiddenField>
並設置這樣的值
document.getElementById("#<%= hndSetFromJS.ClientID %>").value = "yourValue";
要么
$("#<%= hndSetFromJS.ClientID %>").val("yourValue"); // Jquery
您可以在客戶端創建2個功能
function functionOK()
{
//some code
}
function functionCancel()
{
//some code
}
並改變你的代碼
ClientScript.RegisterStartupScript(this.GetType(), "InvalidEntrySubTypeAlert", "if(confirm('" +
"Are you sure you want to create the following deal?\n" +
"\nDeal ID :\t\t\t" + nDealID +
"\nCompany Name :\t\t" + txtCompanyName.Text +
"\nEntry Subject :\t\t" + txtEntrySubject.Text +
"\nBusiness Lead :\t\t" + ddlBusinessLead.SelectedItem.Text +
"\nLicense Status :\t\t" + ddlLicenseStatus.SelectedItem.Text +
"\nEffective Date :\t\t" + string.Format("{0:MM/dd/yyyy}", calEffectiveDate.SelectedDate) +
"\nExpiration Date :\t\t" + string.Format("{0:MM/dd/yyyy}", calExpirationDate.SelectedDate) +
"\nLicense Location :\t\t" + txtLicenseLocation.Text +
"\nEntry Sub Type :\t\t" + ddlEntrySubType.SelectedItem.Text.Split(' ')[0]
+ "')){ functionOK();} else{functionCancel()}", true);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.