Click here to Skip to main content
15,905,414 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello All,

I have a button and a anchor tag.

I want to open a new page on button click which is specified in href of anchor tag.

But I want to open that new page without click on anchor tag but on the click of button.

Thanks & Regards,
Amit
Posted

Hi ,
ASP.NET
<a href="default.aspx" runat="server" id="anchor1"> Click here </a>


C#
protected void Page_Load(object sender, EventArgs e)
{
    anchor1.ServerClick +=new EventHandler(anchor1_Click);
}
protected void anchor1_Click(object sender, EventArgs e)
{
    Response.Write("<script>alert('hello')</script>");
}

Best Regards
M.mitwalli
 
Share this answer
 
Comments
me_pollack 9-Oct-14 12:57pm    
Sorry cannot add runat="server"
common answer for the less experienced
This is absolutely impossible, but you actually don't need it. You can fire some events only in the class or structure where this event is declared, not even in derived class. This is one of important fool-proof features of events as opposed to "regular" delegate instances. For more information, please see my past answer:
Since we have multicast delegates, why do we need events?[^].

In real life, there are no situations where you need to fire an event from outside the declaring type, and this is why .NET events are designed the way they are designed. In fact, you merely need to call some method which would do exactly the same as some event handler of your Click event. Here is the very general recipe: create a separate method with required parameters and call it from two or more different places in you code: one from the event handler (and this is all the code of your event handler should do) and elsewhere, where you wanted to "simulate the click". That's is. Simple, isn't it?

[EDIT]

[EDIT2] By mistake, I though that the comment by Swetha Garlapati (see below) was from OP, so I tried to reply in this wrong assumption. Therefore, the next paragraph is "scratched out". However, the portion of the text below illustrates how this problems looks in JavaScipt..[END EDIT2]

You should have explained that you are talking about HTML and JavaScript. You question is tagged as "C#".

OK, JavaScript has much less limitations, but the idea is the same:
JavaScript
<html>
    <head>
        <title>...</title>
        <script type="text/javascript"><!--
            function clickHandler() {
                window.location.href="http://www.codeproject.com";
            }
            //and call this function elsewhere; it will "simulate" your click
        --></script>
    </head>
<body>

<input type=button onclick="clickHandler()" value="Click here">

</body>
</html>


—SA
 
Share this answer
 
v6
Comments
Sergey Alexandrovich Kryukov 3-Apr-12 17:37pm    
Not exactly. Of course you can do it, but the question was about simulation of event. Please see the JavaScript code sample I've added, after [EDIT].
Please, next time try to ask a question correctly and keep to your question. Why should I spend so much time just because you did not tag it properly or did not explain what you really wanted?
--SA
Sergey Alexandrovich Kryukov 3-Apr-12 18:57pm    
I see, I'm sorry. So, I'm confused and probably confusing OP. Now, I'll need to explain it...

In reply to your comment: the OP's question was about C#, at least it looks so from the tags. What you suggested in JavaScript works, but is not related to the OP's problem of "firing" onClick or Click. I explained the idea as it would be applicable to .NET and JavaScript alike.

--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900