How can I fire an asp.net button event via jquery or java script?
I want when I click on "Doit" string it fire my asp.net button .
This is my "DoIt开发者_运维百科"
<a id="" href="#"">Doit</a>
And my asp.net button event is :
protected void Button1_Click(object sender, EventArgs e)
{
Response.Redirect("rightclick.aspx");
}
How can I do this ? Please help me ..
If Button1
already exists, have such code:
<a id="myLink" href="#" onclick="document.getElementById('<%=Button1.ClientID%>').click(); return false;">Doit</a>
This will "auto click" the button thus trigger any server side code handling its click.
use the LinkButton ASP control to fire that event
<asp:LinkButton id="something" onClick="Button1_Click" Text="DoIt" runat="server" />
Try:
<asp:LinkButton ID="MyLink" runat="server" onclick="Button1_Click">Doit</asp:LinkButton>
To call a server side method on a client side event you need to do the following:
1- Create the server side method:
void DoSomething(...) { ... }
2- Implement the System.Web.UI.IPostBackEventHandler.RaisePostBackEvent
which take one string argument (You can assign the name to the value of this argument).:
public void RaisePostBackEvent(string eventArgument)
{
DoSomething(...);
}
3- Write a script to trigger post back:
function TriggerPostBack(control, arg){
__doPostBack(control, arg);
}
4- Call the PostBack trigger function when needed:
<a .... onclick="TriggerPostBack('control', 'arg')" .. />
Above answers are generating HTML, if you want to invoke from JavaScript use:
__doPostBack('<your controls UniqueID>','');
But you will need to have an asp control on your page either way...
<script>
function GO(){
$("#<%=btnGo.ClientID=%>").click();
}
</script>
<a id="" href="GO();"">Doit</a>
<asp:Button id="btnGo" onClick="Button1_Click">
i didn't test this, but, this is the basic ideia, if you don't wanna use the LinkButton (that is the best option)
PS: If you are using framework 4.0, you can use the clientid static. PS2: if want, you can hide the button, but again, the best way to do this is with LinkButton.
EDIT: Are you only trying redirect to another page ??? o.O ???
精彩评论