单击 html table 中的按钮时如何调用 javascript 函数?
How to call a javascript function when a button inside html table is clicked?
更新安马蒂爵士
你好先生谢谢你回答我的问题
我将此代码添加到
page_load
string eventTarget = this.Request["__EVENTTARGET"];
if (eventTarget == "DeleteRecord")
{
Label1.Text = "Method called!!!";
// Delete your record here
}else
{
Label1.Text = "No method";
}
在页面加载时(运行 时间)label1 将其文本更改为 "No method"
然后我点击了按钮,弹出窗口出现了。我选择了是按钮,但没有任何反应,它甚至没有关闭弹出窗口
为了检查 javascript 是否正常工作,我更改了此代码
__doPostBack("DeleteRecord", '');
到
$("[id*=btnadd]").click();
点击弹出窗口的是按钮后,点击按钮 BTNADD
先生,我的问题是为什么 __doPostBack("DeleteRecord", '');对我不起作用?我什至尝试了一些在线发布的教程,但还是一样
这是javascript函数:
<script type="text/javascript">
$(function () {
//$("[id*=btnDelete]").removeAttr("onclick");
$("[id*=btnDelete]").removeAttr("onclick");
$("#dialog").dialog({
modal: true,
autoOpen: false,
title: "Confirmation",
width: 350,
height: 160,
buttons: [
{
id: "Yes",
text: "Yes",
click: function () {
$("[id*=btnDelete]").attr("rel", "delete");
$("[id*=btnDelete]").click();
}
},
{
id: "No",
text: "No",
click: function () {
$(this).dialog('close');
}
}
]
});
$("[id*=btnDelete]").click(function () {
if ($(this).attr("rel") != "delete") {
$('#dialog').dialog('open');
return false;
} else {
__doPostBack(this.name, '');
}
});
});
</script>
这是背后的代码
protected void DeleteRecord(object sender, EventArgs e)
{
ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Record Deleted.')", true);
}
Html代码
<asp:Button ID="btnDelete" runat="server" Text="Delete" OnClick="DeleteRecord" UseSubmitBehavior="false" />
<div id="dialog" style="display: none">
Do you want to delete this record?
</div>
完美运行
但是我如何调用里面的函数 html table
<tr>
<td>
<%# DataBinder.Eval(Container.DataItem, "signatoryid") %>
</td>
<td>
<%# DataBinder.Eval(Container.DataItem, "signatoryname") %>
</td>
<td>
<%# DataBinder.Eval(Container.DataItem, "signatoryPosition") %>
</td>
<td>
<%# DataBinder.Eval(Container.DataItem, "signatoryOffice") %>
</td>
<td>
<button type="button" class="btndelete btn btn-xs btn-danger" OnClick="DeleteRecord">
<span class="glyphicon glyphicon-trash" aria-hidden="true"></span>Delete
</button>
</td>
</tr>
客户端
创建一个打开对话框的函数并在 onClick
事件中调用它:
<button type="button" class="btndelete btn btn-xs btn-danger" OnClick="javascript:deleteRecord();">
<span class="glyphicon glyphicon-trash" aria-hidden="true"></span>Delete
</button>
<script>
$(function () {
$("#dialog").dialog({
modal: true,
autoOpen: false,
title: "Confirmation",
width: 350,
height: 160,
buttons: [
{
id: "Yes",
text: "Yes",
click: function () {
__doPostBack("DeleteRecord", '');
}
},
{
id: "No",
text: "No",
click: function () {
$(this).dialog('close');
return false;
}
}
]
});
});
function deleteRecord(){
$("#dialog").dialog('open');
}
</script>
服务器端代码
如果您是手工制作的 __doPostback()
,您必须在 Page_Load()
或 [=17= 上检索 EventTarget
和 EventArguments
(如果有) ] 如果您愿意,当前页面的事件处理程序:
protected void Page_Load(object sender, EventArgs e)
{
string eventTarget = this.Request["__EVENTTARGET"];
if(eventTarget == "DeleteRecord")
{
// Delete your record here
}
}
更新安马蒂爵士 你好先生谢谢你回答我的问题 我将此代码添加到
page_load
string eventTarget = this.Request["__EVENTTARGET"];
if (eventTarget == "DeleteRecord")
{
Label1.Text = "Method called!!!";
// Delete your record here
}else
{
Label1.Text = "No method";
}
在页面加载时(运行 时间)label1 将其文本更改为 "No method"
然后我点击了按钮,弹出窗口出现了。我选择了是按钮,但没有任何反应,它甚至没有关闭弹出窗口
为了检查 javascript 是否正常工作,我更改了此代码
__doPostBack("DeleteRecord", '');
到
$("[id*=btnadd]").click();
点击弹出窗口的是按钮后,点击按钮 BTNADD
先生,我的问题是为什么 __doPostBack("DeleteRecord", '');对我不起作用?我什至尝试了一些在线发布的教程,但还是一样
这是javascript函数:
<script type="text/javascript">
$(function () {
//$("[id*=btnDelete]").removeAttr("onclick");
$("[id*=btnDelete]").removeAttr("onclick");
$("#dialog").dialog({
modal: true,
autoOpen: false,
title: "Confirmation",
width: 350,
height: 160,
buttons: [
{
id: "Yes",
text: "Yes",
click: function () {
$("[id*=btnDelete]").attr("rel", "delete");
$("[id*=btnDelete]").click();
}
},
{
id: "No",
text: "No",
click: function () {
$(this).dialog('close');
}
}
]
});
$("[id*=btnDelete]").click(function () {
if ($(this).attr("rel") != "delete") {
$('#dialog').dialog('open');
return false;
} else {
__doPostBack(this.name, '');
}
});
});
</script>
这是背后的代码
protected void DeleteRecord(object sender, EventArgs e)
{
ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Record Deleted.')", true);
}
Html代码
<asp:Button ID="btnDelete" runat="server" Text="Delete" OnClick="DeleteRecord" UseSubmitBehavior="false" />
<div id="dialog" style="display: none">
Do you want to delete this record?
</div>
完美运行
但是我如何调用里面的函数 html table
<tr>
<td>
<%# DataBinder.Eval(Container.DataItem, "signatoryid") %>
</td>
<td>
<%# DataBinder.Eval(Container.DataItem, "signatoryname") %>
</td>
<td>
<%# DataBinder.Eval(Container.DataItem, "signatoryPosition") %>
</td>
<td>
<%# DataBinder.Eval(Container.DataItem, "signatoryOffice") %>
</td>
<td>
<button type="button" class="btndelete btn btn-xs btn-danger" OnClick="DeleteRecord">
<span class="glyphicon glyphicon-trash" aria-hidden="true"></span>Delete
</button>
</td>
</tr>
客户端
创建一个打开对话框的函数并在 onClick
事件中调用它:
<button type="button" class="btndelete btn btn-xs btn-danger" OnClick="javascript:deleteRecord();">
<span class="glyphicon glyphicon-trash" aria-hidden="true"></span>Delete
</button>
<script>
$(function () {
$("#dialog").dialog({
modal: true,
autoOpen: false,
title: "Confirmation",
width: 350,
height: 160,
buttons: [
{
id: "Yes",
text: "Yes",
click: function () {
__doPostBack("DeleteRecord", '');
}
},
{
id: "No",
text: "No",
click: function () {
$(this).dialog('close');
return false;
}
}
]
});
});
function deleteRecord(){
$("#dialog").dialog('open');
}
</script>
服务器端代码
如果您是手工制作的 __doPostback()
,您必须在 Page_Load()
或 [=17= 上检索 EventTarget
和 EventArguments
(如果有) ] 如果您愿意,当前页面的事件处理程序:
protected void Page_Load(object sender, EventArgs e)
{
string eventTarget = this.Request["__EVENTTARGET"];
if(eventTarget == "DeleteRecord")
{
// Delete your record here
}
}