使用javascript触发一个asp:button点击事件 [英] Trigger an asp:button click event using javascript
问题描述
我目前正在处理一个需要在javascript代码中执行一系列事件后触发asp:button click事件的项目。
I am currently working on a project which needs to trigger an asp:button click event after doing a series of events in a javascript code.
请参阅我的代码
这是我的按钮,它是点击事件:
This is my button and it's click event:
<asp:Button ID="btnRefresh" name="btnRefresh" runat="server" Text="btnRefresh" onclick="btnRefresh_Click" />
protected void btnRefresh_Click(object sender, EventArgs e)
{
MyGrid.DataSource = null;
MyGrid.DataSource = GetDataForGrid();
MyGrid.DataBind();
}
我引用了一个包含我的所有JavaScript代码的javascript文件。我这样做,因为我不喜欢混淆我的.aspx文件中的JavaScript代码。
I have referenced a javascript file which holds all my javascript codes for the page. I do it this way since I don't really like mixing up my javascript code inside my .aspx file.
这是这个函数在我的外部.js file:
This is how the function looks like in my external .js file:
function Refresh() {
var btn = $(document).getElementById('btnRefresh');
if (btn) {
btn.click();
}
else {
alert('false');
}
}
我也尝试过其他在互联网上看到的方式如下所有没有运气:
I have also tried other ways I saw on the internet as follows all of which with no luck:
document.getElementById('btnRefresh');
__doPostBack('btnRefresh', '');
__doPostBack('btnRefresh', 'Click');
$('#btnRefresh').click();
有人可以告诉我一个好办法吗?非常感谢!
Can someone please point me a good way to do it? Thank you!
编辑:
我在UpdatePanel内有一个gridview。我有一个触发器,当点击btnRefresh时触发。所以基本上,它刷新我的gridview。因此,我不能使用按钮的OnClick属性来调用我的javascript函数,因为我不会在第一时间点击按钮。我需要能够从我的外部javascript调用 btnRefresh_Click
事件,以便我的触发器将被触发更新我的GridView。
What I want to do is this:
I have a gridview inside an UpdatePanel. I have a trigger which is fired when btnRefresh is clicked. So basically, it refreshes my gridview. Therefore, I cannot use the button's OnClick Property to call my javascript functions since I will not be clicking the button in the first place. I need to be able to call the btnRefresh_Click
event from my external javascript so that my Trigger would be fired to update my GridView.
编辑2:
我尝试在网页上定义我的 btn
.aspx页面本身使用下面的行:
I tried to define my btn
on the .aspx page itself using the line below:
var btn = $(#);
var btn = $("#");
我也改变了我的JS代码,如下:
and I also changed my JS Code as follows:
function Refresh(){
if(btn){
btn.click();
}
else {
alert('false');
}}
function Refresh() { if (btn) { btn.click(); } else { alert('false'); } }
js能够看到 btn
它事件经过 btn.click()
然而,我的 btnRefresh_Click
仍然没有向上。
The js was able to see btn
and it event went through the line btn.click()
However, my btnRefresh_Click
still didn't fire up. Am I missing something here?
推荐答案
因为你引用你的JS文件,你有两个选择。可以传递按钮的 ClientID
或将按钮的 ClientIDMode
设置为 Static
。
Since you reference your JS file, you have two options. either pass in the button's ClientID
or set button's ClientIDMode
to Static
.
我建议你将id传递给JS函数。
I suggest you pass in the id to the JS function.
function Refresh(id) {
var btn = $(document).getElementById(id);
if (btn) {
btn.click();
}
else {
alert('false');
}
}
当你想在另一个JS函数中调用它时,传递 ClientID
。
function doSomthing(){
//do this
//do that
Refresh('<%=btnRefresh.ClientID%>');
}
但是正如你所说,如果你不喜欢把JS放在你的aspx页面,然后只需设置按钮的 ClientIDMode = Static
。
But as you said, if you don't like to put JS in your aspx page, then just set your button's ClientIDMode = Static
.
<asp:Button ID="btnRefresh" ClientIDMode="Static" name="btnRefresh" runat="server" Text="btnRefresh" onclick="btnRefresh_Click" />
这篇关于使用javascript触发一个asp:button点击事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!