Click here to Skip to main content
15,118,172 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
See more:
Hi everyone ,
Now I'm creating a website in ASP.Net with framework 4.0 . I have to slide down a div when I click a asp:Button , i wrote script as

JavaScript
<script type="text/javascript">
    $(document).ready(function () {
        $('#<%= Button1.ClientID %>').click(function () {
            $('div#content').show('slow');
        }
        );
    });
   </script>

and button is created as
ASP.NET
<asp:Button id="Button1" runat="server" text="Button" style ="width :auto" />

and the div is
HTML
<div id ="content" style ="display :none "  > this is a div</div>

The problem is after clicking the button the div shows and hides automatically. But when i use the button as
ASP.NET
<input type ="button"  id="Button1"  runat="server" text="Button" style ="width :auto" />


then it works properly ... I dont understand what is happening .. Please solve my problem


Thank you all
Posted
Updated 9-Aug-12 22:34pm
v3
Comments
Sandeep Mewara 10-Aug-12 1:09am
   
What do you see in View Source for first case? I believe the HTML equivalent will be the same.
Geo Jackson 10-Aug-12 1:44am
   
I need to write some code in CS for the Button1
Abdul Quader Mamun 10-Aug-12 5:15am
   
where is you complete code?
Volynsky Alex 10-Aug-12 5:52am
   
Good question!

This behavior is correct.

When you use asp.net button it makes postback so your div get hided though you dont add onclick event(server side).
But when you use html button & add attribute runat="server" it will not make postback.

So you should use html button or if you want to add asp.net button then add onclineclick like
C#
<asp:button id="Button1" runat="server" text="Button" onclientclick="return false;" style="width :auto"  />


Add debug point add page load you will get idea.
   
v2
Comments
Geo Jackson 10-Aug-12 5:25am
   
Thank you so much . It Worked
Geo Jackson 10-Aug-12 5:25am
   
I got a new problem. After adding a OnClientClick the code in csharp for the button doesnt execute
Volynsky Alex 10-Aug-12 5:48am
   
Nice answer
pradiprenushe 10-Aug-12 5:56am
   
Thanks Volynsky Alex
hiih dude

if you observe your page source you will see that

<asp:button id="Button1" runat="server" text="Button" style="width :auto" xmlns:asp="#unknown">

is Equivalent to

<input type="submit" name="Button1" value="Button" id="Button1" />

which is different than

<input type ="button" id="Button1" runat="server" text="Button" style ="width :auto" />
   
Comments
Geo Jackson 10-Aug-12 6:25am
   
is it possible to write csharp code for the button <input type="submit" name="Button1" value="Button" id="Button1" />
Ganesh Nikam 10-Aug-12 7:47am
   
yes when you added id and runat attribute it will be server side control
Geo Jackson 10-Aug-12 8:05am
   
ok then when i click which code executes first
Ganesh Nikam 10-Aug-12 8:37am
   
obvious client side
see the below sigle line code for your que..

VB
<div id="content"> this is a div</div>

<input type ="button"  id="Button1"  runat="server" text="Button" onclick="  $('#content').slideDown(100);" />
   
Comments
Geo Jackson 10-Aug-12 6:15am
   
thanks but i need a asp:Button

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900