Solved

Execute javascript from code behind in asp.net 3.5

Posted on 2008-10-29
5
3,181 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 to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

758 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

18 Experts available now in Live!

Get 1:1 Help Now