In ASP.NET upon making a button click I need one JavaScript to make its action. I tried OnClientClick function but the problem with this event is first OnClientClick function takes place and then OnServerClick function takes place i.e. first JavaScript function runs and then C# code executes but I need in reverse order first I need to execute C# code and then I have to run the JavaScript. I am using AJAX.NET and ScriptManager in my code. Do scriptmanager helps me anyway? else how to run JavaScript on server side.
I need in the below manner.
Button1_Click(object sender, EventArgs e)
{
ListBox1.Items.Add(TextBox1.Text); //for example
` here I have to run JavaScript `
}
Is there any way to implement this?
Edit 1:
I need a div to be scrolled down onbuttonclick(on click adds new data to div using databinder and repeater). As said above clientclick is running first but I need to run it after server action.
javascript is..
<script>
var objDiv = document.getElementById("div1");
objDiv.scrollTop = objDiv.scrollHeight;
</script>
and
<div id="div1"> //css properties height 200 and width 200 overflow:auto
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePartialRendering="true" />
<asp:Timer ID="Timer1" runat="server" Interval="200" />
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Repeater id="Repeater1" runat="server" >
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "Message") %> <br />
</ItemTemplate>
</asp:Repeater>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
</Triggers>
</asp:UpdatePanel>
</div>
<asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<div id="div2">
<asp:TextBox ID="tb_message" runat="server" Width="250px" EnableViewState="false" />
<br />
<asp:Button ID="btn_send" runat="server" Width="250px" Text="Send"
onclick="btn_send_Click" />
</div>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btn_send" EventName="click" />
</Triggers>
</asp:UpdatePanel>
EDIT
Based on the code provided this should work if you would like to register the script on the server side:
You can use the code provided by James as well if you would like to register the script on the client side, but keep in mind that the script will be executed each time the AJAX request will be completed (The script will be executed each time any of the update panels is refreshed). If you are going to scroll down the
div1element each time the UpdatePanel1 is refreshed and there are no more update panels that are refreshed using theTimercontrol on the same page the code provided by James would be more useful, while my code will scroll down thediv1only of the button send is pressed.UPDATED
i’m sorry – it seems that the code is executed after the server click event handler is executed, but before the html inside the first update panel is reloaded (you can simply set a breakpoint inside the server click event handler, and an alert in the registered script). So at the point when the script is executed the div element still contains old html.
As a hack you can use a setTimeout function with a small timeout (in my sample 100 ms is quite enough). I’m looking for a proper way to check if the html inside update panel is reloaded and will update my answer as fast as I find it.