我有以下情况:用户选择了产品,检索了产品记录并设置了延期交货标志。我想问用户是否想要将其包含在订单中。我似乎无法在IF语句中找到任何证明它的例子。
我的VB代码片段:
Dim backorder = myDataTable.Rows(0)("backorder").ToString()
If backorder = "True" And <somehow ask user it they want to order anyway> Then
'do something
End If
我在aspx文件中的Javascript:
<script type = "text/javascript">
function Confirm() {
var confirm_value = document.createElement("INPUT");
confirm_value.type = "hidden";
confirm_value.name = "confirm_value";
if (confirm("Selected item is on temporary back order. Do yuou want to include it on this order?")) {
confirm_value.value = "Yes";
} else {
confirm_value.value = "No";
}
document.forms[0].appendChild(confirm_value);
}
</script>
我可以通过javascript读取从VB添加到页面的变量,但无法确定如何调用javascript以提示用户。 出于某种原因,我无法理解这一点。请举例说明。
答案 0 :(得分:1)
使用RegisterStartupScript
方法:
Sub ProcessOrder()
'Your code to process the selected products here...
Dim orderAnyway As string = hdnConfirmResponse.Value.Trim()
If selectedItem.BackOrdered Then
If orderAnyway = "" Then
Dim cs As ClientScriptManager = Page.ClientScript
' Define the name and type of the client scripts on the page.
Dim csname1 As String = "ConfirmScript"
Dim cstype As Type = Me.GetType()
' Check to see if the startup script is already registered.
If (Not cs.IsStartupScriptRegistered(cstype, csname1)) Then
Dim cstext1 As String = "Confirm();"
cs.RegisterStartupScript(cstype, csname1, cstext1, True)
End If
Else
If orderAnyway = "yes" Then
'ADD THE BACKORDERED ITEM SINCE THEY CONFIRMED
End If
End If
End If
End Sub
答案 1 :(得分:0)
我讨厌回答我自己的问题,但这最终有效。混合了mjw的回答以及对javasvript尝试的更多研究。我没有让JS创建隐藏字段,而是将其添加到HTML
<asp:HiddenField runat="server" id ="confirm_value"/>
新VB:
If myDataTableFrame.Rows.Count > 0 Then
Dim backorder = myDataTableFrame.Rows(0)("backorder").ToString()
If backorder = "True" And confirm_value.Value <> "Yes" Then
Dim cs As ClientScriptManager = Page.ClientScript
' Check to see if the startup script is already registered.
If (Not cs.IsStartupScriptRegistered(Me.GetType(), "ConfirmScript")) Then
Dim cstext1 As String = "Confirm();"
cs.RegisterStartupScript(Me.GetType(), "ConfirmScript", "Confirm();", True)
End If
Return
Else
confirm_value.Value = "No"
...
新Javascript: 请参阅ASP.NET set hiddenfield a value in Javascript如果他们回答了&#34; OK&#34;
,还会添加按钮。<script type = "text/javascript">
function Confirm() {
if (confirm("Selected item is on temporary back order. If you want to include it on this order click OK, then resubmit it?")) {
//had to resort to this to find hidden field rather than document.getElementById('confirm_value')
var x = document.getElementById('<%= confirm_value.ClientID%>');
x.value = 'Yes';
//re-click the button
document.getElementById('<%=bttnadd.ClientID%>').click();
} else {
var x = document.getElementById('<%= confirm_value.ClientID%>');
x.value = 'No';
}
}
</script>