Reputation: 2175
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>
Upvotes: 4
Views: 10010
Reputation: 1677
EDIT
Based on the code provided this should work if you would like to register the script on the server side:
Button1_Click(object sender, EventArgs e)
{
ListBox1.Items.Add(TextBox1.Text); //for example
ScriptManager.RegisterStartupScript(UpdatePanel2, UpdatePanel2.GetType(), "scriptKey", "var objDiv = document.getElementById('div1'); objDiv.scrollTop = objDiv.scrollHeight;", true);
}
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 div1
element each time the UpdatePanel1 is refreshed and there are no more update panels that are refreshed using the Timer
control on the same page the code provided by James would be more useful, while my code will scroll down the div1
only 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.
Upvotes: 1
Reputation: 579
Javascript will not run on the server, however you can inject Javascript into the HTTP response stream using Response.Write("<script>..</script>)
at a place of your choosing in the server code.
Response.Write("<script>..</script>)
will write the script to the page before it has started rendering which can cause side effects see Difference between Response.Write() and ClientScript.RegisterStartupScript()?
So as @dannyloid posted have a look at ClientScript.RegisterStartupScript
Upvotes: 1
Reputation: 61793
JavaScript is a client-side language and therefore cannot run on the server.
EDIT
Based on your updated question, something like this should work well:
JavaScript
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(endRequestHandler);
function endRequestHandler(sender, args)
{
var objDiv = document.getElementById("div1");
objDiv.scrollTop = objDiv.scrollHeight;
}
Upvotes: 1
Reputation: 41040
You could use node.js Check the answers here:
Upvotes: 0