Solved

Execute javascript from code behind in asp.net 3.5

Posted on 2008-10-29
5
3,189 Views
Last Modified: 2013-12-17
Scenario:

I have an <asp:repeater> filled with <asp:LinkButton> items.

I add .command and .commandargument to each linkbutton to execute am asp.net method, which will do som LINQ quering in a cached sqlobject based on the content of the commandargument, filll out some <asp:label> and <asp:textbox> items in a hidden DIV and then I need to call a javascript function to open then hidden DIV in a modal window.

My problem is: How can I  call the javascript function ?

In the "RepeaterItem_Command(Object sender, CommandEventArgs e)" method

I have tried to use ClientScript.RegisterStartUpScript(...)  but the code is never entered on the page
and I have tried to use Respons.Write(<SCRIPT> .....</SCRIPT); didn't work either

Tha page_load has been fired already before it gets to the command method, and there is no postback envolved.

0
Comment
Question by:fc_curling
  • 4
5 Comments
 
LVL 8

Expert Comment

by:rpkhare
ID: 22829874
I have tried this way and it is working.
Response.Write("<script language=\"javascript\">")
Response.Write("alert(\"User logged in\");");
Response.Write("</" + "script>");

Open in new window

0
 

Author Comment

by:fc_curling
ID: 22829924
If I do that I get a Script Error in IE : "Error parsing near '<script language="ja'.  Code: 0

samen as when I tried myself
0
 

Author Comment

by:fc_curling
ID: 22829935
From the linenumber it seems that Respons.Write is inserting the tekst right after the <table> tag holdig the <asp:repater> items.
0
 

Author Comment

by:fc_curling
ID: 22829949
This is the steps involved.

1. rendering page and the repeater items.
2. when user clicks one one of the <asp:LinkButton> items, the page_load is fired and after that it calls the command method
3. in the command method I do some LINQ queries and fill out som items in a hidden DIV on the page,
4. at the end of the command methods I want to call a javascript function to handle the modalwindow show of the items in the hidden DIV

it's the last step I'm having trouble with.

0
 

Accepted Solution

by:
fc_curling earned 0 total points
ID: 22872157
I found the solution myself.

It is NOT possible to use ClientScript.RegisterStartUpScript(...)  or response.write when handling the .command event for the <asp:linkbutton control. The response is written before the <html> tag (or so it seems) .

solution were to add the javascript code to call the javascript function in the..OnClientClick property for the linkbutton. When the link is clicked it will fire the .command delegate and the javascript will be executed by the client as well.
0

Featured Post

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question