Solved

Execute javascript from code behind in asp.net 3.5

Posted on 2008-10-29
5
3,190 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Name Space error VS2015 1 37
VS 2017 18 105
C# class library debugging - Breakpoint will not hit. 8 32
How to filter by key press ? 6 57
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…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

733 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