I have the following code in C#:
if (function.Equals("Larger50"))
{
Request req = new Request();
string result = req.doRequest("function=" + function + "&num=" + number, "http://localhost:4000/Handler.ashx");
if (result.Equals("True") || result.Equals("true"))
{
Page.ClientScript.RegisterStartupScript(Page.GetType(), null, "window.open('http://localhost:4000/Larger.aspx?num=" + number + "', '_newtab')", true);
}
if(result.Equals("False") || result.Equals("false"))
{
Page.ClientScript.RegisterStartupScript(Page.GetType(), null, "window.open('http://localhost:4000/Smaller.aspx', '_newtab')", true);
}
if(result.Equals("Error") || result.Equals("error"))
{
Page.ClientScript.RegisterStartupScript(Page.GetType(), null, "window.open('http://localhost:4000/ErrorPage.htm', '_newtab')", true);
}
Session["result"] = result;
Page.ClientScript.RegisterStartupScript(Page.GetType(), null, "window.location.href = 'Results.aspx'", true);
}
The result variable can have any one of three values (if the server responds):
i) true
ii) false
iii) error
The main problem with this code is that the new tab script in each of the three if statements work as they should. However, the last script which opens the Results.aspx page is not executing for some reason or another. The script is written well as it executed perfectly if all the other code is commented out. How should I solve the problem?
I tried replacing it with Response.Redirect("Results.aspx") however then this exeuctes and all the other three scripts never execute.
You should register these all at once, rather than in two separate statements:
if (function.Equals("Larger50"))
{
Request req = new Request();
string result = req.doRequest("function=" + function + "&num=" + number, "http://localhost:4000/Handler.ashx");
string scriptVal = "";
if (result.Equals("True") || result.Equals("true"))
{
scriptVal = "window.open('http://localhost:4000/Larger.aspx?num=" + number + "', '_newtab');";
}
if(result.Equals("False") || result.Equals("false"))
{
scriptVal = "window.open('http://localhost:4000/Smaller.aspx', '_newtab');";
}
if(result.Equals("Error") || result.Equals("error"))
{
scriptVal = "window.open('http://localhost:4000/ErrorPage.htm', '_newtab');";
}
Session["result"] = result;
scriptVal += "window.location.href = 'Results.aspx';";
Page.ClientScript.RegisterStartupScript(Page.GetType(), null, scriptVal, true);
}
See the docs on ClientScriptManager.RegisterStartupScript, specifically:
A startup script is uniquely identified by its key and its type.
Scripts with the same key and type are considered duplicates. Only one
script with a given type and key pair can be registered with the page.
Attempting to register a script that is already registered does not
create a duplicate of the script.
In your case, the type and key are the same in both of the scripts you register.
You could uniquely identify them with a key, and then register them separately. But you have to keep in mind that the order of execution is not guaranteed:
The script blocks are not guaranteed to be output in the order they
are registered.
Related
I am using a WebDialogResult defined on a SmartPanel by calling AskExt() that is used to prompt for a password.
Unfortunately, it caches the password that's entered, so it keeps pre-filling the dialog and I want it to be blank every time the dialog is called.
I've tried a variety of different things, such as calling with .AskExt(true), which is supposed to refresh the dialog and invoked .ClearDialog() before calling .AskExt(), and numerous other things including .Cache.Clear(), .Cache.ClearQueryCache(), .Reset(), .Update(new PwdFields()), .Delete(.Current) as well as the more direct .Current.Pwd = null and .Current.Pwd = "", but nothing works.
That is browser problem. In order to solve it I propose you to find id of control with help of firefox or chrome developer toolbar, and use javascript which will clean values.
Then you can add jquery like this:
$( document ).ready(function() {
$("#ctl00_phG_PXSPanelVerify2_PXFormViewVerify2_CustEdPwd2").focus()
{
$("#ctl00_phG_PXSPanelVerify2_PXFormViewVerify2_CustEdPwd2").text("");
}
});
Eventually the combination below consistently cleared the dialog of the password whenever it was re-displayed.
if (this.PasswordLookup1Status.AskExt(true) == WebDialogResult.OK)
{
string currentPassword = this.PasswordLookup1Filter.Current.Pwd;
this.PasswordLookup1Filter.Current.Pwd = "";
this.PasswordLookup1Filter.Update(this.PasswordLookup1Filter.Current);
if (currentPassword == "1234")
{
Base.Transactions.Ask("Information", "Password [" + currentPassword + "] is correct.", MessageButtons.OK);
Base.Transactions.ClearDialog();
}
else
{
throw new PXException("Password [" + currentPassword + "] is incorrect.");
}
this.PasswordLookup1Filter.ClearDialog();
}
this is my code and i want to access this method from another project file, another project is added and everything is working fine except this method, when i created a new class and pasted that code on it, it says:
'iEndRecord'is a fields but is used like a type
private void GetNoOfRows()
{
int iTotalRecords = ((DataTable)(grdCrew.DataSource)).Rows.Count;
int iEndRecord = grdCrew.PageSize * (grdCrew.PageIndex + 1);
int iStartsRecods = iEndRecord - grdCrew.PageSize;
if (iEndRecord > iTotalRecords)
{
iEndRecord = iTotalRecords;
}
if (iStartsRecods == 0)
{
iStartsRecods = 1;
}
if (iEndRecord == 0)
{
iEndRecord = iTotalRecords;
}
lblCount.Text = iStartsRecods + " to " + iEndRecord.ToString() + " of " + iTotalRecords.ToString();
DataBind();
}
P.S. this is complete code, this is just a method to call a function for counting rows, its working fine inside file, but i was trying to put it on one external file so that i don't have to write the same code in all files again and again
and the namespaces?
Im pretty sure that you forget add they
Good Day Every one here is my code
if (type.Contains("Loan Date"))
{
prenda.LoanMonth = year.ToString() + "/" + month.ToString();
string a = servs.CheckLM(prenda);
if (Convert.ToInt32 (a) != 0)
{
Page.ClientScript.RegisterStartupScript(Page.GetType(), "key", "myFunction();", true);
//create a popup if yes process if no exit
//if yes
prenda.LoanMonth = year.ToString() + "/" + month.ToString();
servs.DeletePrendaLM(prenda);
foreach (DataRow row1 in table.Rows)
{
prenda.Bcode = row1["Bcode"].ToString();
prenda.Jprincipal = Convert.ToDecimal(row1["JPrincipal"].ToString());
prenda.Aprincipal = Convert.ToDecimal(row1["APrincipal"].ToString());
prenda.Cprincipal = Convert.ToDecimal(row1["CPrincipal"].ToString());
prenda.LoanMonth = year.ToString() + "/" + month.ToString();
user.UserID = Session["UserID"].ToString();
servs.UploadPrendaLM(prenda, user);
}
}
the porblem is that the Page.ClientScript.RegisterStartupScript(Page.GetType(), "key", "myFunction();", true); doesnt execute as soon as the process pass over it, it executes myFunction() after finishing the button click proceses, i cant create a if clause below it because it finishes first all of the process before i can use the myFunction() i need that process first before continuing the proceses below //
so can you help me find a work around?
Few things that might help
The script block added by the RegisterStartupScript method executes when the page finishes loading but before the page's OnLoad event is raised. >> Where are you adding script in the page (Which event?) try it in OnInit or init complete events.
You can check whether script added or not through IsStartupScriptRegistered method (Page.ClientScript namespace)
I'm registering a custom protocol handler on my computer, which calls this application:
string prefix = "runapp://";
// The name of this app for user messages
string title = "RunApp URL Protocol Handler";
// Verify the command line arguments
if (args.Length == 0 || !args[0].StartsWith(prefix))
{
MessageBox.Show("Syntax:\nrunapp://<key>", title); return;
}
string key = args[0].Remove(0, "runapp://".Length);
key.TrimEnd('/');
string application = "";
string parameters = "";
string applicationDirectory = "";
if (key.Contains("~"))
{
application = key.Split('~')[0];
parameters = key.Split('~')[1];
}
else
{
application = key;
}
applicationDirectory = Directory.GetParent(application).FullName;
ProcessStartInfo psInfo = new ProcessStartInfo();
psInfo.Arguments = parameters;
psInfo.FileName = application;
MessageBox.Show(key + Environment.NewLine + Environment.NewLine + application + " " + parameters);
// Start the application
Process.Start(psInfo);
What it does is that it retrieves the runapp:// request, split it into two parts: application and the parameters passed, according to the location of the '~' character. (This is probably not a good idea if I ever pass PROGRA~1 or something, but considering I'm the only one using this, it's not a problem), then runs it.
However, a trailing '/' is always added to the string: if I pass
runapp://E:\Emulation\GameBoy\visualboyadvance.exe~E:\Emulation\GameBoy\zelda4.gbc, it will be interpreted as
runapp://E:\Emulation\GameBoy\visualboyadvance.exe E:\Emulation\GameBoy\zelda4.gbc/.
Why would it do this ? And why can't I get rid of this trailing slash ? I tried TrimEnd('/'), Remove(key.IndexOf('/'), 1), Replace("/", ""), yet the slash stays. What is happening ?
You need to assign the result of the TrimEnd:
key = key.TrimEnd('/');
Strings in C# are immutable; therefore string methods which alter the string return a new string with the alterations, rather than changing the original string.
I am writing an event handler that, on ItemAdded, checks to see if a site exists, then creates a site with the given URL or with an alternate URL. I already wrote something similar but I was attempting to clean up my code for the site exist check into the method below.
private string CheckSiteExists(SPWeb web, string siteURL, string webURL)
{
//Counter for our alternate URL
int i = 0;
//Open original URL
SPWeb tempweb = web.Site.OpenWeb(webURL + "/" + siteURL);
//Check if site exists
if (tempweb.Exists == false)
{
do
{
i++;
tempweb = web.Site.OpenWeb(webURL + "/" + siteURL + "_" + i);
}
while (tempweb.Exists == false);
//Dispose of our web
tempweb.Dispose();
}
else
{
tempweb.Dispose();
//If site does not exist, return original URL
return siteURL;
}
//If site does exist, return original url plus counter
return siteURL + "_" + i;
}
I decided to test what I have and found that w3wp went from 0% CPU usage to 50-80% and stayed there until I killed it manually. I'm guessing that my do while statement isn't acting as I think it should and it's just looping to infinity.
This code seems to be finding the first URL that matches a web that does exist, not the first matching a web that doesn't exist:
You're checking tempweb.Exists == false rather than == true
You're only disposing, and returning the URL, after tempweb.Exists is true.
If no web exists, this will get stuck in a very long loop.