0

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.

Please see my codes below:

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();
}

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.

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!

EDIT:

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.

EDIT 2:

I tried to define my btn on the .aspx page itself using the line below:

var btn = $("#");

and I also changed my JS Code as follows:

function Refresh() { if (btn) { btn.click(); } else { alert('false'); } }

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?

4

5 回答 5

1

由于您引用了 JS 文件,因此您有两个选择。传入按钮ClientID或将按钮设置ClientIDModeStatic.

我建议您将 id 传递给 JS 函数。

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.

<asp:Button ID="btnRefresh" ClientIDMode="Static" name="btnRefresh" runat="server" Text="btnRefresh" onclick="btnRefresh_Click" />
于 2012-09-18T03:25:34.513 回答
0

在这里您可以使用 OnClientClick 属性并提及要调用的外部 js 中的函数名称,因为该属性实际上是在服务器端事件执行之前执行的。

于 2012-09-18T03:04:12.450 回答
0

看看ClientID物业。

您可以使用该属性获取客户端按钮的ID,并使用该ID触发点击。

function Refresh() {
    var btn = $("#<%=btnRefresh.ClientID%>");
    if (btn) {
        btn.click();
    }
    else {
        alert('false');
    }
}

但请注意,<%=btnRefresh.ClientID%>它只能在与 相同的页面上工作btnRefresh,而不是在外部 JS 文件中。ClientID 由 ASP.NET 动态生成。

于 2012-09-18T03:08:18.817 回答
0

这是我过去使用过的“hack”

客户端可见的按钮是

<input type="button" id="btPart1" value="Trigger Phase 1" 
    onclick="clicked()" class="btn btn-default" />

在您的网络表单中定义这样的按钮

<asp:button id="Triggered" runat="server" OnClick="btnRefresh_Click"
    Style="visibility: hidden;"/>

在您的 JavaScript 中,您将拥有

function clicked(){
    document.getElementById("ctl02_Triggered").click();
}

根据您使用的 asp 版本,ID 会发生变化,因此请确保检查按钮的实际 ID,以便您可以通过 js 触发它。我提到这一点是因为在编译时 ASP 实际上会根据它所拥有的父项来转换您提供的 ID。

于 2018-03-23T19:29:15.530 回答
0

实际上,在运行任何 JS 方法之前,您必须确保已加载 DOM。因此,请确保您的 js 文件包含在 DOM (html) 的末尾。

由于按钮是服务器控件,所以需要通过方法btnRefresh引用,clientIDRefresh

var btn= document.getElementById(‘&lt;%=btnRefresh.ClientID=%>’);

但是您的代码没有显示您如何调用该Refresh方法。所以希望以上对你有所帮助。或者您需要显示更多代码。

于 2018-03-23T19:47:06.410 回答