Call button click Event in href tag
CODE BEHIND PAGE CODE
Code:
protected void Button1_Click(object sender, EventArgs e)
{
SHow("MANSI");
}
public void SHow(string error)
{
Page page1 = HttpContext.Current.Handler as Page;
if (page1 != null)
{
error = error.Replace("'", "\'");
ScriptManager.RegisterStartupScript(page1, page1.GetType(), "err_msg", "alert('" + error + "');", true);
}
}
SOURCE CODE
Code:
<ul>
<li><a href="#" onClick='Button1_Click'>Home</a></li>
<li><a href="#">News</a></li>
<li><a href="#">Blog</a></li>
<li><a href="#">Gallery</a></li>
<li><a href="#">About CWS</a></li>
<li><a href="#">Contact</a></li>
</ul>
When I click on Home,I am getting Javascript error Button1_Click is undefined,but it is defined in the code behind,you can see above.
Re: Call button click Event in href tag
Try using an asp:Button, or at least an input type='button', in either case you need the runat="server" part so it knows it's not a JS call but a postback to the server. If you're using VB then you'll need the Handles clause on your event handler sub, or for VB/C# you specify the sub's name like you have already shown in your post.
Re: Call button click Event in href tag
hay,
why you didn't use a asp link button?
Re: Call button click Event in href tag
Woah, woah, woah...
The reason that it isn't working i because you don't have anything tying the HTML controls, to the event handler in the server side code.
As JuggaloBrotha has pointed out, you don't have any controls marked with runat="server", so as a result, there is no "tie-up" between the two things.
Bottom line is, I think there is something fundamentally wrong with what you are trying to achieve. Can you describe exactly what you want to do? Once we know that, I am sure we will be able to point you in the right direction.
For instance, you might not even have to do the work on the server side at all. If all you are doing is trying to show an alert message when the user clicks a link, then all this can be done on the client side, without the need to postback to the server.
Gary