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?