这是脚本,我想在后面的代码中使用它我使用了按钮按钮的clentclick属性我想使用这个代码而不使用按钮
<script type = "text/javascript">
function Confirm() {
var confirm_value = document.createElement("INPUT");
confirm_value.type = "hidden";
confirm_value.name = "confirm_value";
if (confirm("Do you want to save data?")) {
confirm_value.value = "Yes";
} else {
confirm_value.value = "No";
}
document.forms[0].appendChild(confirm_value);
}
</script>
<asp:TextBox ID="TextBox1" runat="server" ontextchanged="TextBox1_TextChanged"></asp:TextBox>
我还能做些什么,以便我能够实现
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
// TextBox1.Attributes.Add("OnClientClick", "Confirm()");
string confirmValue = Request.Form["confirm_value"];
if (confirmValue == "Yes")
{
//Your logic for OK button
}
else
{
//Your logic for cancel button
}
}
public void OnConfirm(object sender, EventArgs e)
{
}
答案 0 :(得分:0)
在页面上留下一个按钮,使用css隐藏它,然后在设置值后从JavaScript调用click():
<script type = "text/javascript">
function Confirm() {
var confirm_value = document.createElement("INPUT");
confirm_value.type = "hidden";
confirm_value.name = "confirm_value";
if (confirm("Do you want to save data?")) {
confirm_value.value = "Yes";
} else {
confirm_value.value = "No";
}
document.forms[0].appendChild(confirm_value);
document.getElementById("Button1").click();
}
</script>
<asp:TextBox ID="TextBox1" runat="server" onchange="Confirm()"></asp:TextBox>
<div style="display:none;"><asp:Button ID="Button1" ClientIDMode="static" runat="server" onclick="Button1_Clicked" /></div>
然后在你的代码隐藏中:
protected void Button1_Clicked(object sender, EventArgs e)
{
string confirmValue = Request.Form["confirm_value"];
if (confirmValue == "Yes")
{
//Your logic for OK button
}
else
{
//Your logic for cancel button
}
}