1

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 ..

6 Answers 6

2

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.

Sign up to request clarification or add additional context in comments.

13 Comments

Cheers @Persian feel free to Accept the answer by clicking the V icon to the left. :)
hi, will it load the page??
@Neel load what page? When clicked, it will simply be the same as clicking Button1, which in the OP's example redirects to a different page when clicked. Not sure what you're asking.
suppose i have a button in my aspx page which is hidden and i trigger its click event from js file and it has server side method but i dnt want to load whole page..i guess i need update panel. anyother approach?
@Neel so having it triggered like that is totally not relevant. You need AJAX, and there are many ways to use AJAX, UpdatePanel is one way, jQuery also got good support for AJAX.
|
1

use the LinkButton ASP control to fire that event

<asp:LinkButton id="something" onClick="Button1_Click" Text="DoIt" runat="server" />

2 Comments

I don't wanna to the "DoIt" string be a asp.net control .
What exactly do you want? If you want to make an Ajax call through jQuery in ASP.NET Webforms then you need to following this step by step tutorial > encosia.com/2008/05/29/… .
1

Try:

<asp:LinkButton ID="MyLink" runat="server" onclick="Button1_Click">Doit</asp:LinkButton>

4 Comments

last time I checked, you can't post server side using ASP.NET Hyperlink control
@Nee, thanks, you are right, you have to use a Linkbutton control. Answer is now fixed.
I don't wanna to the "DoIt" string be a asp.net control .. It's between <a> tag .
@Persian, the control will render as an anchor tag.
0

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')" .. /> 

Comments

0

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...

1 Comment

Have you an (invisible) "Button1" Control in your page, passing its .UniqueID property as the first argument?
0
<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 ???

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.