locked
Why doesn't this work??? RRS feed

  • Question

  • User-1974093247 posted
    Why does this line ERROR on me? <asp:Button id="Button5" OnClick="javascript:MoveRight()" style="Z-INDEX: 105; LEFT: 192px; POSITION: absolute; TOP: 32px" runat="server" Text="Move Right"></asp:Button> How do I associate a button with a javascript function???
    Monday, January 31, 2005 9:08 AM

All replies

  • User-1804824805 posted
    Can you tell us EXACTLY what the error is?
    Monday, January 31, 2005 9:10 AM
  • User-1974093247 posted
    Compilation Error Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately. Compiler Error Message: CS1026: ) expected Source Error: Line 133: <asp:listbox id="rightBox" style="Z-INDEX: 102; LEFT: 272px; POSITION: absolute; TOP: 16px" runat="server" Line 134: SelectionMode="Multiple" Height="112px" Width="128px"></asp:listbox> Line 135: <asp:Button id="Button5" OnClick="javascript:MoveRight()" style="Z-INDEX: 105; LEFT: 192px; POSITION: absolute; TOP: 32px" runat="server" Line 136: Text="Move Right"></asp:Button> Line 137: <asp:Button id="Button4" style="Z-INDEX: 104; LEFT: 192px; POSITION: absolute; TOP: 72px" runat="server" Source File: http://localhost/dlist/WebForm4.aspx Line: 135
    Monday, January 31, 2005 9:11 AM
  • User-1974093247 posted
    I've tried several possible combinations. I thought it was OnClick="javascript:MoveRight()". I've tried: OnClick="javascript:MoveRight()" OnClick="javascript:MoveRight();" OnClick="javascript:MoveRight" OnClick="MoveRight()" OnClick="MoveRight" etc... It works for html buttons and regular hrefs...
    Monday, January 31, 2005 9:19 AM
  • User1341174311 posted
    <color="black;width:500px;"> In this case, where you are dealing with a Button control from the WebControls namespace, the OnClick attribute refers to a server-side event handler. If you want to add a client-side function call, you can do so at runtime in code, in the Page_Load event for example: myButton.Attributes.Add("onclick", "javascript: MoveRight();") Does that help? </color>
    Monday, January 31, 2005 10:20 AM
  • User-1974093247 posted
    Wow, thats definately something I never would have guess. The onclick on my ASPX page is referring to the serverside event and to add a clientside onclick you have to add an attribute called onclick. I sincerely thank you, I never would have known that, thinking about it, it seems a little odd. I set causesvalidation to false is there anything I can do to keep the button from causing the page to post back?
    Monday, January 31, 2005 10:49 AM
  • User1341174311 posted
    <color="black;width:500px;"> You can return false after you call the MoveRight() function (or you could have the MoveRight() function return false): myButton.Attributes.Add("onclick", "javascript: MoveRight();return false;") But why not just use an HTML Input Button for this? </color>
    Monday, January 31, 2005 12:47 PM
  • User-1974093247 posted
    I wondered when someone might ask. :) Normally I would however they have created special button controls (which are panels with text and image (instead of an image with text in it) that role over correctly) the panels look exactly like buttons and implement the IPostBack interface. They want all Buttons to be these custom server controls for ease of reuse so I have no choice... I used an asp:button as my example because it acts the same way.
    Monday, January 31, 2005 1:06 PM
  • User-1974093247 posted
    Also I just wanted to take a moment to say thanks NetProfit. Without your guidance on this one I fear I may have wandered aimlessly for some time.
    Monday, January 31, 2005 1:10 PM