This is a problem i've tried so solve before but gived up.
Basicly i'm using ModalPopupExtenders (from AJAX.NET) to display Panels with a few content (text, controls, etc). And i'm calling it from codebehind. And it works well.
But now i want to replace the ModalPopup with some jQuery dialog box. The problem is calling it from codebehind.
As far as i know, i have to register the jQuery libraries on RegisterStartup event, but i've tried it and call the jQuery from codebehind but with no sucess.
Can someone help me? I really want to replace the ModalPopup, they gives me to much trouble.
Thanks in advance.
protected void Page_Load(object sender, EventArgs e)
{
ScriptManager.RegisterStartupScript(this, GetType(), "registerDialog",
"$(function() { $('#dialog').dialog({autoOpen:false, show:'blind'}); });", true);
}
protected void Button1_Click(object sender, EventArgs e)
{
ScriptManager.RegisterStartupScript(this, GetType(), "openDialog",
"$('#dialog').dialog('open');", true);
}
Is this the correct way? I've to register it first to stay hidden.
Thanksю
If you're using a ScriptManager, use RegisterStartupScript(), like this:
ScriptManager.RegisterStartupScript(this, GetType(), "modalscript",
"$(function() { $('#dialog').dialog(); });", true);
If you're not using a ScriptManager/UpdatePanels, use the equivalent ClientScriptManager version.
It's important to remember to wrap your code in a document.ready handler (IE has the most issues without it), so your elements (in my example, id="dialog") are in the DOM and ready.
You do not actually call jQuery from code behind, you just write some extra javascript code that's run on page load (after the PostBack).
In this start up code you make the jQuery calls.
Related
I made researching about this subject I could not find proper answer.
In my default.aspx page, I have a treeview. Codes are in default.aspx like below:
protected void Page_Load(object sender, EventArgs e)
{
}
protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
{
Control ucont;
if (TreeView1.SelectedNode.Value == "Yeni Dönem")
{
ucont = LoadControl("usercontrols/yenidonem.ascx");
PlaceHolder1.Controls.Add(ucont);
}
else
{
ucont = LoadControl("usercontrols/tabloktar.ascx");
PlaceHolder1.Controls.Add(ucont);
}
}
I load user controls dnynmicaly. User controls are have button control. I can not fire user control's button click when I load it dynamcally. How can I solve this ?
Thanks.
First of all, I would not recommend adding control dynamically later than in Page_Load event. Other things to remember is that You should add it on each page load and assign unique ID value the control that does not change between postbacks.
In this case, the easiest way would be to always add both controls to the page and show appropriate one using Visibility property.
If that's not suitable for You, try to move the code from TreeView1_SelectedNodeChanged to the Page_Load event and load appropriate control on each postback until it should be changed to another one.
I haven't tested this, so if You have any issues when using thise answer, let me know in the comments and I'll try to help.
i am The use of following code:
protected void Button1_Click(object sender, EventArgs e)
{
Page.ClientScript.RegisterOnSubmitStatement(GetType(),"message","ok");
Response.Redirect(Request.Url.AbsoluteUri);
}
Why are not shown the message ?
Please show me the right way
Page.ClientScript.RegisterOnSubmitStatement adds a piece of javascript on your page. This javascript is not executed directly. It is executed when your page is submitted.
Response.Redirect(Request.Url.AbsoluteUri) is executed direcly and will send the client to the new page before the javascript is executed.
You should probably register your javascript in Page_Load instead of in the Button1_Click event.
See this page for more info on Page.ClientScript.RegisterOnSubmitStatement.
Because of Page.ClientScript.RegisterOnSubmitStatement expects some script to be invoked. In your case "OK" is not script. Try something like:
Page.ClientScript.RegisterOnSubmitStatement(this.GetType(), "message", "alert('OK');");
Where is the best part of asp.net page or code behind to register RegisterClientScriptBlock.
You have a bunch of options.
Register script includes in your <head> section or do inline <script> tags. I prefer to have my scripts at the bottom of the page though.
You can also register it at the Page level in your Page_Load (or any other event) by calling ClientScript.RegisterClientScriptBlock and passing it the script you want. Remember that if you do go with RegisterClientScriptBlock, you will need to make sure that you register the code with every page load so that is why I would recommend the Page_Load event if you want to use this method.
For example:
protected void Page_Load(object sender, EventArgs e)
{
AddClientSideJavascript();
// Do other stuff
}
private void AddClientSideJavascript()
{
// Register some client script code
Type someType = this.GetType();
if (!ClientScript.IsClientScriptBlockRegistered(someType, "TESTSCRIPT"))
{
string script = "function ShowAlert() { alert('Test'); }";
ClientScript.RegisterClientScriptBlock(someType, "TESTSCRIPT", script, true);
}
// Register more here... etc...
}
Just make sure you don't include it the portion of your Page_Load that is wrapped with the if (!IsPostBack) check or else your scripts will not get registered after any postbacks.
The correct answer is - at any point within page_load.
I am using ASP.Net and jQuery + jQuery UI. Everything works fine with the jQuery on any other page, however when I create a popup window with window.open(...) jQuery seems to no longer function.
I have all of the script files included on the Popup's Master page, so am not sure why it won't fire.
Any Thoughts?
I am not sure if you use window.open you are creating a page class that inherits from the master page.
So JQuery is probabaly not included in that pop up. In addition popups aren't nice. people download all kind of things so they don't open. but it can be done like this:
protected void Button1_Click(object sender, EventArgs e)
{
string queryString =
"http://localhost:39208/TreeView.aspx?param1="
+ TextBox1.Text.Trim();
string newWin =
"window.open('" + queryString + "');";
ClientScript.RegisterStartupScript
(this.GetType(), "pop", newWin, true);
}
ClientScript.RegisterStartupScript will bring your jquery in the pop up
I have a LinkButton that has to postback to perform some logic.
Once it is finished, instead of loading the page back up in the browser, I want to leave it alone and pop open a new window.
So far, the best idea I've had is to put the LinkButton in an UpdatePanel, and have it render some JavaScript out when it reloads, yet I think that is totally hacky. Also, if I recall right, JavaScript within a update panel won't run anyways.
Any other ideas?
Use LinkButton.PostBackUrl to set a different page to POST to, and some client script to get a new window (and the old target restored so that future postbacks work normally). The 2nd page can use PreviousPage to get access to any needed state from the original page.
<script runat="server">
void lnk_Click(object sender, EventArgs e) {
// Do work
}
</script>
<script type="text/javascript">
var oldTarget, oldAction;
function newWindowClick(target) {
var form = document.forms[0];
oldTarget = form.target;
oldAction = form.action;
form.target = target;
window.setTimeout(
"document.forms[0].target=oldTarget;"
+ "document.forms[0].action=oldAction;",
200
);
}
</script>
<asp:LinkButton runat="server" PostBackUrl="Details.aspx" Text="Click Me"
OnClick="lnk_Click"
OnClientClick="newWindowClick('details');" />
Here is the code:
protected void Button1_Click(object sender, EventArgs e)
{
// Do some server side work
string script = "window.open('http://www.yahoo.com','Yahoo')";
if (!ClientScript.IsClientScriptBlockRegistered("NewWindow"))
{
ClientScript.RegisterClientScriptBlock(this.GetType(),"NewWindow",script, true);
}
}
One thing you could try is to have your LinkButton OnClick event do its processing, then register a Page.ClientScript.RegisterStartupScript with the popup code, which will put some Javascript into the tag to fire off after the page loads. This should launch your new window after the processing completes.
EDIT: Reading your comment, I believe you can still use this approach, have your results stored in a session variable, and then have the popup page pull the results from there.