Compilation error on this string - c#

I'm trying to create the following string:
<script runat="server" type="text/C#">
protected void Page_Load(object sender, EventArgs e)
{
Parent.Page.ClientScript.RegisterStartupScript(typeof(Page), "test", "<script type='text/javascript' langauage='javascript' src='test.js'></script>");
}
</script>
yet i get a compilation error in VS saying "Newline in constant"

"<script type='text/javascript' langauage='javascript' src='test.js'></script>"
Well yeah, you've got string containing </script> inside a <script> element. That closes the outer <script>, so the code that appears to be inside your runat-server script is just:
protected void Page_Load(object sender, EventArgs e)
{
Parent.Page.ClientScript.RegisterStartupScript(typeof(Page), "test", "<script type='text/javascript' langauage='javascript' src='test.js'>
And as the error says, that contains a "string with no terminating double-quote.
Try escaping the characters so the other script block doesn't see them as markup:
"\x3Cscript type='text/javascript' src='test.js'>\x3C/script>"

Your problem is the end script tag
http://support.microsoft.com/kb/827420
Solve it wit:
".....<"+"/SCRIPT>"
or maybe
".....<\/script>"
Use ClientScriptManager instead and use RegisterClientScriptInclude. This way you only need to have the file name in a string.
ClientScriptManager.RegisterClientScriptInclude
ClientScriptManager scriptManager = new ClientScriptManager(); scriptManager.RegisterClientScriptInclude("filename.js");

#"<script language="Javascript" src="/utility/thickbox/thickbox-custom.js"></script>"

Related

call jQuery function from code behind at master page on page load

I am calling a jQuery function from code behind in my master page on page load, and it is returning this error:
ReferenceError: $ is not defined
Code Behind:-
protected void Page_Load(object sender, EventArgs e)
{
string script = "$(document).ready(function () {alert('hello'); });";
Page.ClientScript.RegisterStartupScript(this.GetType(), "load", script, true);
}
I also tried this jquery function :-
ScriptManager.RegisterStartupScript(Page, typeof(Page), "ShowProgressBar", "ShowProgressBar();", true);
but then getting the error "ShowProgressBar is not defined".
Try this,
ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "Function1", "yourFunction();", true);
You miss to load your jquery file add following script tag in header.
<script src="https://code.jquery.com/jquery-3.0.0.min.js"></script>

Scriptmanager not loading properly

I want to show a div after an action.
For now i will just use a button click event.
this is my jquery function:
function hideMessageBlock() {
$('.alert').delay(5000).fadeOut('slow');
}
i also tried this one:
$(document).ready(function(){
console.log('READY to animate');
function hideMessageBlock() {
$('.alert').delay(5000).fadeOut('slow');
}
});
in my code behind i have the following code:
protected void Button1_Click(object sender, EventArgs e)
{
alertSuccessBlock.Visible = true;
lbl_alertSuccessBlock.Text = "Animate this block with timeout!";
ScriptManager.RegisterStartupScript(this, this.GetType(), "ScriptManager1", "hideMessageBlock();", true);
}
in the aspx i have declared a scriptmanager right after the scripts i use (bottom of the page before form and body closure):
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
The Error that i am receiving is : ReferenceError: hideMessageBlock is not defined
The function is called before the whole jquery script is loaded (just guessing)
How can i resolve this issue?
I have already solved the issue. I had to move the function definition outside of the document.ready callback

Alert message on paging in gridview

I want to show an alert when user tries to do paging in gridview. I am using this script, but the alert pops up even if any of those buttons in the page is clicked in the page. I need to alert only when paging is clicked.
<script type="text/javascript">
function closeEditorWarning() {
return 'It looks like you have been editing something -- if you leave before submitting your changes will be lost.'
}
window.onbeforeunload = closeEditorWarning
</script>
Really appreciate any help on this.
Write an java script alert inside gridview_pageindexchanging() event to do this:
You can do this by using Script Manager as follows:
You have double script tags. Add the script tags yourself:
protected void grid_pageindexchanging(object sender, GridViewPageEventArgs e) {
string script = "<script type=\"text/javascript\">alert('abc');</script>";
ClientScript.RegisterClientScriptBlock(this.GetType(), "Alert", script);
}
Or let the method add it:
protected void grid_pageindexchanging(object sender, GridViewPageEventArgs e) {
string script = "alert('abc');";
ClientScript.RegisterClientScriptBlock(this.GetType(), "Alert", script, true);
}
Hope this helps..
Updated:
try including the following:
using System.Web.Script.Serialization
Updated 2:
Can you try this now:
string script = "alert('its working now!')";
ScriptManager.RegisterStartupScript(Page, Page.GetType(), "its working",script,true);
If jQuery is an option, set a CSS class on the pager and associate your script with the click event for that class.
Untested but probably in the right direction:
$('.myClass').click(function () { closeEditorWarning; });

asp.net textbox focus problems

I am trying to set focus on textbox on page load in asp.net as follows
protected void Page_Load(object sender, EventArgs e)
{
try
{
if (!IsPostBack)
{
// fillUnitType();
//
fillLastCode();
txt_Grn_Date.Text = System.DateTime.Now.ToString("dd-MM-yyyy");
setinitialrow_lvl();
txt_Po_No.Focus();
}
}
catch (Exception ex)
{
lblMessage.Text = ex.Message;
}
}
But the textbox is not getting focused. What is that I am missing. I have used update panel is it because of that.? Or my css is slightly faulty.
Write following function in your codebehind and for every control call this function
private void Set_Focus(string controlname)
{
string strScript;
strScript = "<script language=javascript> document.all('" + controlname + "').focus() </script>";
RegisterStartupScript("focus", strScript);
}
Set
tapindex = 0
TextBox1.Focus();
or
textBox1.Select();
or
protected override void OnShown(EventArgs e)
{
textBox1.Focus();
base.OnShown(e);
}
or
setTimeout("myFocusFunction()", 500);
function myFocusFunction(){
$("#myTextBoxID").focus();
}
try this in javascript
<script language=javascript>
function fnLoad(){
document.getElementById("<%= txt_Po_No.ClientID %>").focus();
}
</script>
call "fnLoad()" function on "onLoad" event of body..
You need to add this function in body tag : Like
<body onload="fnLoad()">........</body>
Update:
try another way
<script language=javascript>
$(document).ready(function(){ document.getElementById("<%= txt_Po_No.ClientID %>").focus();})
</script>
or
<script language=javascript>
$(window).load(function(){ document.getElementById("<%= txt_Po_No.ClientID %>").focus();})
</script>
I have tried this with updatepanel and textbox inside it.
CodeBehind
.aspx
output
Try this code..
string jsCode= "<script language=javascript>document.getElementById('<%= TEXTBOX.ClientID%>').focus();</script>";
ClientScript.RegisterClientScriptBlock(GetType(), "txtbox",jsCode, false);

Inject Javascript from asp.net code behind files

Am I injecting this correctly?
string myScriptName = "EventScriptBlock";
string myScript = string.Empty;
//Verify script isn't already registered
if (!ClientScript.IsClientScriptBlockRegistered(myScriptName))
{
Response.Write('b');
myScript = "\n<script type=\"text/javascript\" language=\"Javascript\" id=\"EventScriptBlock\">\n";
myScript += "alert('hi');";
myScript += "\n\n </script>";
ClientScript.RegisterClientScriptBlock(this.GetType(), myScriptName, myScript);
}
This is in my Page_Load, but I never see an alert and I have no JavaScript errors either.
You can use registerstartupscript instead of registerclientscriptblock!
RegisterStartupScript
When you use RegisterStartupScript, it will render your script after all the elements in the page (right before the form's end tag). This enables the script to call or reference page elements without the possibility of it not finding them in the Page's DOM
RegisterClientScriptBlock
When you use RegisterClientScriptBlock, the script is rendered right after the Viewstate tag, but before any of the page elements. Since this is a direct script (not a function that can be called, it will immediately be executed by the browser. But the browser does not find the label in the Page's DOM at this stage and hence you should receive an "Object not found" error
Difference between registerstartupscript and registerclientscriptblock
protected void Page_Load(object sender, System.EventArgs e)
{
string myScript = "\n<script type=\"text/javascript\" language=\"Javascript\" id=\"EventScriptBlock\">\n";
myScript += "alert('hi');";
myScript += "\n\n </script>";
Page.ClientScript.RegisterStartupScript(this.GetType(), "myKey", myScript, false);
}
I have a feeling this is related to your asp.net/html markup.
Do you have a form tag like so in your .aspx file?
<form id="form1" runat="server">
....
</form>
Both RegisterStartupScript and RegisterClientScriptBlock will work.
Problem lies in myScript (string variable).In myScript variable you need to use alert variable only, as whenever you use this, script tag will be added automatically to your page's HTML at runtime. To check this right on your page and see the source of the page.
protected void Page_Load(object sender, EventArgs e)
{
string myScript = string.Empty;
//myScript = "\n<script type=\"text/javascript\" language=\"Javascript\" id=\"EventScriptBlock\">\n";
string registerKey = "alert('RegisterClientScriptBlock');";
myScript = "alert('RegisterStartupScript');";
Page.ClientScript.RegisterStartupScript(this.GetType(), "RegisterStartupScript", myScript, true);
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "RegisterClientScriptBlock", registerKey, true);
}
Note: I have executed RegisterStartupScript first and than RegisterClientScriptBlock.But RegisterStartupScript alert will be executed at last, as it will be added at the end of the page.
RegisterClientScriptBlock will always be added at the starting of the page.
You should use RegisterStartupScript.

Categories