我有这个asp按钮:
<asp:Button ID="btnConfirm" runat="server" OnClick = "OnConfirm" Text = "Raise Confirm" />
我有这个c#代码
public void OnConfirm(object sender, EventArgs e)
{
string eventIDel = "anyStringGivenAtRunTime";
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "DeleteUserChosenEvent", "confirmDeletion('" + eventIDel + "');", true);
string confirmValue = confirmValue = Request.Form["confirm_value"];
if (confirmValue == "Yes")
{
// Do something like
this.Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('You clicked YES!');", true);
}
else
{
this.Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('You clicked NO!');", true);
}
}
我在代码中触发了这个JS函数......
代码背后&#34;触发&#34;:
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "JumpToBottom", "confirmDeletion('" + eventIDel + "');", true);" :
JS功能:
function confirmDeletion(str) {
var confirm_value = document.createElement("INPUT");
confirm_value.type = "hidden";
confirm_value.name = "confirm_value";
if (confirm("Do you want to delete Event: " + str + " ?")) {
confirm_value.value = "Yes";
} else {
confirm_value.value = "No";
}
document.forms[0].appendChild(confirm_value);
如果我使用如下语法,则在第二次点击后该事件才能正常工作:
this.btnConfirm.Attributes.Add("onclick", "confirmDeletion('" + eventIDel + "');");
但是如果我使用类似于原始语法的语法,则以NULL开始而不要求确认,之后它将用户决定存储在确认中并在下次用户按下按钮时使用它。
请帮助谢谢大时间。
答案 0 :(得分:0)
经过大量研究后,我明白如果我从后面的代码中调用它,页面循环将始终将JS函数放在最后。理想的方法是让我在ASP按钮上调用JS函数,默认情况下首先触发它。 ASP按钮
<asp:Button ID="BtnDelete" runat="server" Text="Delete" onclick="btnDelete_Click" class="buttons" Visible="true" disabled="disabled" OnClientClick="if (!deleteRow()) return false;" />
然后在JS函数中我将拉出整个gridview,在我的情况下识别每一行是通过一个单选按钮选择,然后提取我想要的单元格的文本
function deleteRow() {
// Get Gridview
var gridView = document.getElementById("<%= GridView1.ClientID %>");
// Loop through the Gridview
for (var i = 1; i < gridView.rows.length; i++) {
// Get the radio button of each row in the gridview and see if its checked
if (gridView.rows[i].cells[0].getElementsByTagName("INPUT")[0].checked == true)
// Get Event ID value from 1st cell of rowReturn Answer
return confirm("Are you sure you want to delete Event ID: " + gridView.rows[i].cells[1].innerText + "?");
}
}
之后,返回将带有按钮将使用以下逻辑处理的全部错误
OnClientClick="if (!deleteRow()) return false;"
如果用户选择“是”,则表示将继续使用负责删除的C#方法!!!
乌拉!!!