Solved

Execute javascript from code behind in asp.net 3.5

Posted on 2008-10-29
5
3,185 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

911 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now