I would like to open a popup window using javascript in my c#.net app. This is the code in the body tag in my webform
<script language=javascript>
function openWindow(strEmail)
{
window.open('CheckEmail.aspx?email=' + strEmail + , 'Check Email','left=100,top=100,toolbar=no,scrollbars=yes,width=680,height=350');
return false;
}
</script>
this is my code in the Page_Load section
this.btnCheck.Attributes.Add("onclick", "return openWindow(" + txtEmail.Text + ");");
right now I'm trying to pass the string from my textbox "txtEmail" so in my popup window i can get the request.querystring but Im a little unsure of how the syntax is.
No need of the last +
window.open('CheckEmail.aspx?email=' + strEmail,'Check Email','left=100,top=100,toolbar=no,scrollbars=yes,width=680,height=350');
and in CheckEmail.aspx page you can get the query string as
Request.QueryString["email"]
Use a ' in the CS side inside the function around the textEmail.Text
this.btnCheck.Attributes.Add("onclick", "return openWindow('" + txtEmail.Text + "');");
Why don't you get the email in the client code if the txtEmail control is visible.
function openWindow()
{
var email = document.getElementById('<%=txtEmail.ClientID%>').value;
window.open('CheckEmail.aspx?email=' + email + , 'Check Email','left=100,top=100,toolbar=no,scrollbars=yes,width=680,height=350');
return false;
}
Related
I am trying to set focus to a page control (Textbox) by using the registerStartupScript method. However, I have been unsuccessful. Here is what I have tried:
ClientScript.RegisterStartupScript(this.GetType(), "SetFocus", "<script>document.getElementById('" + this.tbAdjust.ClientID + "').focus();</script>");
And:
ClientScript.RegisterStartupScript(GetType(), "focus", "<script>$('" + this.tbAdjust.ClientID + "');</script>");
Can't seem to get it. Seems like a pretty straight forward question, if you all need anymore code, let me know. Thanks in advance for any help!
Normally tbAdjust.Focus(); at code behind should work. Here are the scripts.
Without Ajax
ClientScript.RegisterStartupScript(this.GetType(), "focus",
"document.getElementById('" + this.tbAdjust.ClientID + "').focus();", true);
With Ajax
ScriptManager.RegisterStartupScript(Page, Page.GetType(), "focus",
"document.getElementById('" + this.tbAdjust.ClientID + "').focus();", true);
If you want to use jQuery, you need # at the front.
For example, "$('#" + this.tbAdjust.ClientID + "').focus();"
Wouldn't jQuery's .ready() method work?
$(document).ready(function() {
$('#target').focus();
});
Try Page.RegisterClientScriptBlock instead:
Page.RegisterClientScriptBlock("SetFocus", "<script>document.getElementById('" + this.tbAdjust.ClientID + "').focus();</script>");
I have HTML code stored in a database and would like to display (render) it onto a view.
I wanted to show it as a normal text page.
eg: **HTML:** <h3 style="font-weight: bold">Usage:</h3>
<p style="text-decoration: underline">Viewing of Form -</p>
Should be shown as: Usage:
Viewing of Form -
I have a button as view and when clicked, it should show me the page.
I am using entity frame work so i have written,
string url = ("view.aspx?id = " + page.ID + "&Text=" + page.Text);
ScriptManager.RegisterStartupScript(this, typeof(string), "OPEN_WINDOW", "window.open( '" + url + "', null, 'height=500,width=800,status=yes,toolbar=yes,menubar=no,location=no,scrollbars=yes,resizable=no,titlebar=yes' );", true);
and i have created a new page view.aspx and tried to use System.Web.HttpUtility.HtmlDecode(Request.QueryString["Text"]) to render the file.
But i am getting a blank page.
Please help me.
Thanks,
Can you confirm that page.Text has a value when it's being rendered?
If you are certain that the value page.Text is populated correctly (I'm assuming something like "view.aspx?id=1&Text=<h3>example post<h3>") then you may want to try encoding the string for the URL before returning it to the client.
string url = ("view.aspx?id = " + page.ID + "&Text=" + System.Web.HttpUtility.UrlEncode(page.Text));
ScriptManager.RegisterStartupScript(this, typeof(string), "OPEN_WINDOW", "window.open( '" + url + "', null, 'height=500,width=800,status=yes,toolbar=yes,menubar=no,location=no,scrollbars=yes,resizable=no,titlebar=yes' );", true);
Then make sure that you are getting encoded characters in your links on the client.
I am trying to open a new page from the code. For this I used the following code on button click event
string pageURL = UIConstants.Url.PrintUserBRCs + "?" + UIConstants.ReportType + "=" + UIConstants.DetailReport;
string script = string.Format("window.open('{0}', null, 'height=600, width=800, status=yes, location=yes, menubar=yes, resizable=yes, scrollbars=yes, status=yes, toolbar=yes, titlebar=yes')", pageURL);
Page.ClientScript.RegisterClientScriptBlock(GetType(), "LaunchTemplate", script, true);
The new page is opening properly but it has the issue that the new page goes in the background while the old page appears at top.
I need to show the new page opened at top of previous page.
How can I do that.
Thanks in advance.
You can use focus() like this:
string script = string.Format("var newWindow = window.open('{0}', null, 'height=600, width=800, status=yes, location=yes, menubar=yes, resizable=yes, scrollbars=yes, status=yes, toolbar=yes, titlebar=yes');"
+ " newWindow.focus();", pageURL);
C#
HtmlButton btnSave = new HtmlButton();
btnSave.ID = "btnSave" + i.ToString();
btnSave.Attributes.Add("onClick", "javascript:return SubmitSave(" + btnSave.ID + ");");
javascript
function save(e)
{
var getId=e.id;
}
I am not getting id in Mozilla firefox,bout its working fine in IE
You can pass the Client ID instead of the ID btnSave.ClientID as ClientID is the perfect choice to deal with such a situation.
btnSave.Attributes.Add("onClick", "javascript:return SubmitSave(" + btnSave.ClientID + ");");
function save(e)
{
var getId=e; // Now you have Client ID, you can use directly instead getting through e.id
}
That is because you are using the id as a variable. Some browsers add the id:s as properties in the window object so that you can access them directly, but to make it work in anything but those few browsers you should use the getElementByID method to locate the element.
btnSave.Attributes.Add("onClick", "javascript:return SubmitSave(document.getElementByID('" + btnSave.ID + "'));");
Error: missing } in XML expression
source code: http://localhost:3811/Clinic/ScheduleModule/ManageWorkingTime.aspx?ScheduleId=FRXTn%2fX1N8Wy8C%2fdJqQmDjrOEECv%2fRwauMVX6ZTipAM%3d
line: 0, column: 188
code:
<script language='javascript'>$(document).ready(function() {Sexy.alert( "Can not copy files." );});</script>
CODE:
public static void ShowAsync(string sMessage, MessageBoxTypes sType, Control control, Page pPage)
{
StringBuilder sb = new StringBuilder();
sb.Append("<script language='javascript'>");
string sMsg = sMessage;
sMsg = sMsg.Replace("\n", "\\n");
sMsg = sMsg.Replace("\"", "'");
sb.Append(#"$(document).ready(function() {");
sb.Append(#"Sexy." + sType + #"( """ + sMsg + #""" );");
sb.Append(#"});");
sb.Append(#"</" + "script>");
ScriptManager.RegisterClientScriptBlock(pPage, typeof(Page), control.ClientID, sb.ToString(), true);
}
if i change true to false in RegisterClientScriptBlock then i get
error: $ is not defined
source code: http://localhost:3811/Clinic/ScheduleModule/ManageWorkingTime.aspx?ScheduleId=dH0ry1kng6MwGCRgCxXg8N5nCncbzPzn3TAOEI0tAY4%3d
line: 0
i call this popup like:
MessageBox.ShowAsync("Can not copy files.", MessageBoxTypes.alert, this, Page);
What can be wrong. If i copy this (JQUERY)
<script language='javascript'>$(document).ready(function() {Sexy.alert( "Can not copy files." );});</script>
into some .aspx page popup works. But if i call it from code behind and daypilot pro in this updatepanel form then i get this error.
Can be problem that two ajax framewroks mixed themself? How to prevent this?
i try with jquery.noConflict but it is the same
$.noConflict();
jQuery(document).ready(function() { Sexy.alert("Can not copy files."); });
Thx
If you change the last parameter in RegisterClientScriptBlock from true to false it will not add the script tag anymore. Currently with the setting to true, you have the script tag twice. Not sure what happens, but can't be good :-)
$ sounds like jquery? You don't mention what you are using? I mix ASP.NET Ajax with jquery and that works fine. What Version are you on?