views:

70

answers:

2

I'm trying to raise a button_click event in my code behind for a asp:Button, however it only seems to postback when I click it the SECOND time, rather than the first, am I missing something? Below is my code

<asp:Button AccessKey="Y" EnableViewState="true"
    Height="25px" runat="server" ID="myButton"
    Text="hello" OnClick="clickit"  />
<asp:TextBox runat="server" AutoPostBack="true" 
    ID="sddf">
</asp:TextBox>

My code behind looks like this:

protected void clickit(Object sender, EventArgs e)
{
    Unit myUnit = new Unit(50, UnitType.Pixel);
    myButton.Height = myUnit;
}

Now techincally when I click the button it should post back and increase the height of the button to 50px from 25px, but I have to click it twice for it to do this? What am I doing wrong?

+4  A: 

When you click the button, it sends a postback to the server, which reloads the entire page. This can take some time. I suspect that you simply aren't waiting long enough for the postback to finish, and clicking it again while the page is loading.

Try clicking it once, then waiting until the browser is completely finished loading.


In general, it's better to do things like this on the client in Javascript. That way, you avoid the need to make a postback, making it much faster. In your case, if you include the jQuery library in the page, you can write the following (untested):

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" />
<button onclick="$(this).height(50);">Button1</button>
SLaks
+1  A: 

I hope it should solve your problem.

function ChangeSize() { var submitButton = document.getElementById('<%= button1.ID %>'); submitButton.height = 50; }

Zinx