admin管理员组文章数量:1323375
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>
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>
Share
Improve this question
edited Dec 1, 2011 at 13:12
Mad coder.
asked Dec 1, 2011 at 12:57
Mad coder.Mad coder.
2,1759 gold badges38 silver badges53 bronze badges
2
- Is the postback an AJAX postback or standard postback? – James Hill Commented Dec 1, 2011 at 13:01
- 2 please provide some code if possible, so we can see which approach could be applicable in your case – danyloid Commented Dec 1, 2011 at 13:02
4 Answers
Reset to default 1EDIT
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 pleted (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.
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;
}
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
You could use node.js Check the answers here:
- Can you use node.js with IIS?
- and this http://ajaxian./archives/node-net-running-node-on-windows-via-net
- and http://www.hanselman./blog/InstallingAndRunningNodejsApplicationsWithinIISOnWindowsAreYouMad.aspx
本文标签: aspnetRun javascript on server sideStack Overflow
版权声明:本文标题:asp.net - Run javascript on server side - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742122285a2421771.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论