Results 1 to 4 of 4

Thread: Call button click Event in href tag

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Apr 2009
    Posts
    116

    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.

  2. #2
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    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.
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

  3. #3
    Frenzied Member avrail's Avatar
    Join Date
    Mar 2006
    Location
    Egypt, Cairo
    Posts
    1,221

    Re: Call button click Event in href tag

    hay,
    why you didn't use a asp link button?
    You Don't Have to Rate Me.
    I'm Not a Civilized Man I'm the Civilization it self
    White or Black, Living or Dieing and 0 or 1 that's MY life
    iam an Object in Object Oriented Life
    my blog : http://refateid.blogspot.com/
    twitter :@avrail
    010011000111010101110110001000000100110101111001001000000101000001100011

  4. #4
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width