Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Javascript and PostBack

Status
Not open for further replies.

kenbw

Programmer
May 2, 2002
39
US
This may not be possible, but how can I on a button click, disable the button and then cause the postback? I imagine this needs to be done via javascript.

Ken
 
Hey ken just add some javascript that disables the button to the onclick attribute of the button. When you click it the javascript will run and a postback will still occur.

.Attributes.Add("","") That'l do donkey, that'l do
[bravo] Mark
If you are unsure of forum etiquette check here faq796-2540
 
Mark,
Sorry I tried that and it did not work. I did not get my javascript to run. Here is what I have:

In the Code Behind:

Me.btnResubmit.Attributes.Add("onClick", "javascript:DisableButton();")

In the HTML:

<script language=javascript>
function DisableButton()
{
//document.getElementById(&quot;btnResubmit&quot;).enabled = false;
alert(&quot;Hi!&quot;);
}
</script>

Does not seem to run the alert(&quot;Hi!&quot;);

Ken
 
Ken I put this following code into my page_load event handler and things worked just fine.
Only difference I can see is that you have a &quot;;&quot; after you call your javascript function. That and I dynamically loaded the javascript function itself but that shouldn't make a difference.

Dim sb As New StringBuilder()
sb.Append(&quot;<Script Language=javascript>&quot;)
sb.Append(&quot;function DisableButton() {&quot;)
sb.Append(&quot;alert('Hi!');&quot;)
sb.Append(&quot;}&quot;)
sb.Append(&quot;</Script>&quot;)

RegisterStartupScript(&quot;Disable&quot;, sb.ToString)


Button1.Attributes.Add(&quot;onClick&quot;, &quot;javascript:DisableButton()&quot;)

sb = Nothing
That'l do donkey, that'l do
[bravo] Mark
If you are unsure of forum etiquette check here faq796-2540
 
The reason it doesn't work is that there is already an onclick event specified for the button (the one that does the postback). If you examine the html source, you'll see it.

The workaround that I've found for this situation is to add a regularly flavored html input button, and specify an onclick for that which first disables the button, and then
Code:
.submit()
s the form.

penny1.gif
penny1.gif

The answer to getting answered -- faq855-2992
 
Are you sure about that Paul? My code did work. And when I view the source I only see this

<input type=&quot;submit&quot; name=&quot;Button1&quot; value=&quot;Button&quot; id=&quot;Button1&quot; /></P>
<P>
<input type=&quot;submit&quot; name=&quot;Button2&quot; value=&quot;Button&quot; id=&quot;Button2&quot; /></P>


No onclick events specified That'l do donkey, that'l do
[bravo] Mark
If you are unsure of forum etiquette check here faq796-2540
 
Guys,
I actually found away to do it with the asp button. I had to add some code to the Attribute to set the Post Back Event Reference. Also I had to turn Client Validation off on the button. Here is what it looks like now.

Code Behind:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Me.btnResubmit.Attributes.Add(&quot;onclick&quot;, &quot;javascript:DisableButton();&quot; & Page.GetPostBackEventReference(btnResubmit))


End Sub

Javascript:

<script language=javascript>
function DisableButton()
{
document.getElementById(&quot;btnResubmit&quot;).disabled = true;
}
</script>

Button Call:
<asp:button id=&quot;btnResubmit&quot; tabIndex=&quot;26&quot; runat=&quot;server&quot; CssClass=&quot;buttonred&quot; Text=&quot;Resubmit&quot; Width=&quot;70px&quot; CausesValidation=&quot;False&quot;></asp:button>

Thanks for all the help and tips though.

Ken
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top