Run server side javascript

In ASP.NET, when I click a button, I need one JavaScript to make it action. I tried the OnClientClick function, but the problem with this event is the first OnClientClick function, and then the OnServerClick function is OnServerClick , i.e. the first JavaScript function is executed, and then the C # code is executed, but I need to in reverse order, first I need to execute the C # code, and then I have to run JavaScript. I use AJAX.NET and ScriptManager in my code. Does the script maker help me anyway? else how to run server side javascript.

I need below.

 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?

Change 1:

I need the div to be scrolled on the onbuttonclick button (clicking on the data adds new data to the div using the databinder and repeater). As said above, the client click works first, but I need to start it after the 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> 
+4
source share
4 answers

EDIT

Based on the code provided, this should work if you want 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 also use the code provided by James if you want to register the script on the client side, but keep in mind that the script will be executed every time the AJAX request is completed (The script will be executed every time any update panel is updated). If you are going to scroll div1 every time UpdatePanel1 is updated, and there are no more update panels updated with the Timer control on the same page, the code provided by James would be more useful while my code would scroll down div1 just a click div1 .

UPDATED

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 reboots (you can simply set a breakpoint inside the server click event handler and a warning in the registered script). So, at the time of the script, the div element still contains the old html.

As a hack, you can use the setTimeout function with a small timeout (in my example, 100 ms is enough). I am looking for a suitable way to check if the html update panel is reloaded inside, and will update my response as quickly as I find it.

+1
source

JavaScript is a client language, therefore it cannot work on a 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; } 
+1
source

Javascript will not run on the server, however you can include Javascript in the HTTP response stream using Response.Write("<script>..</script>) at the location of your choice in the server code.

Response.Write("<script>..</script>) will write the script to the page before it starts rendering, which can cause side effects, see The difference between Response.Write () and ClientScript.RegisterStartupScript ()?

Since the posted @dannyloid look at ClientScript.RegisterStartupScript

+1
source

Source: https://habr.com/ru/post/1384106/


All Articles