It is "not possible" to redirect into a new window, because a redirect on the server causes a special HTTP response to be sent to the users browser, the client. The browsers native implementation interprets the special response code and sends the user off to the destination. The only way to open a new window is for it to be initiated on the client side, whether it be through script or clicking on a link.
So the solution always proposed to this problem is to instead write out some script that opens the window, rather than using Response.Redirect:
public static void Redirect(string url, string target, string windowFeatures)
{
HttpContext context = HttpContext.Current;
if ((String.IsNullOrEmpty(target) target.Equals("_self", StringComparison.OrdinalIgnoreCase)) && String.IsNullOrEmpty(windowFeatures))
{
context.Response.Redirect(url);
}
else
{
System.Web.UI.Page page = (System.Web.UI.Page)context.Handler;
if (page == null)
{
throw new InvalidOperationException("Cannot redirect to new window outside Page context.");
}
url = page.ResolveClientUrl(url);
string script;
if (!String.IsNullOrEmpty(windowFeatures))
{
script = @"window.open(""{0}"", ""{1}"", ""{2}"");";
}
else
{
script = @"window.open(""{0}"", ""{1}"");";
}
script = String.Format(script, url, target, windowFeatures);
ScriptManager.RegisterStartupScript(page, typeof(System.Web.UI.Page), "Redirect", script, true);
}
}
Than, use this method like:
Redirect("MyPage.aspx", "_blank", "status=no,toolbar=no,menubar=no,location=no,resizable=yes,titlebar=no");
18 octobre 2007
17 octobre 2007
Concurrent access in ASP.NET
I want to know how many people are connected on my Web application
A user is in a group and we want to know if there is a user of the group who's inthe Site. But, it's possible to have 2 guys who use the same account. So i found this solution, with the Session and the Application in DotNet 2.0.
int ConnectedCount = 0;
if (System.Web.HttpContext.Current.User.Identity.IsAuthenticated)
{
//Dynamic string if you have more than 1 group of users
string HashTableName = "NameOfMyHashtable"
if (System.Web.HttpContext.Current.Session != null)
{
//Create new Hashtable if null:
Application[HashTableName] = Application[HashTableName] ?? new Hashtable();
//region Refresh information about current User
if (((Hashtable)Application[HashTableName]).Contains
(System.Web.HttpContext.Current.Session.SessionID))
{
((Hashtable) Application[HashTableName]).Remove
(System.Web.HttpContext.Current.Session.SessionID);
}
((Hashtable) Application[HashTableName]).Add(System.Web.HttpContext.Current.Session.SessionID,
DateTime.Now.AddMinutes(System.Web.HttpContext.Current.Session.Timeout));
//region clean Hashtable
foreach (string pKeys in ((Hashtable) Application[HashTableName]).Keys)
{
if (((DateTime) ((Hashtable) Application[HashTableName])[pKeys]).CompareTo(DateTime.Now) <
connectedcount =" ((Hashtable)"> 0 )
{
lblUsersConnected.Attributes.Add("class", "fontMessageError fade");
lblUsersConnected.Text = "Attention, il y a déjà " + ConnectedCount + " utilisateur connecté";
}
}
A user is in a group and we want to know if there is a user of the group who's inthe Site. But, it's possible to have 2 guys who use the same account. So i found this solution, with the Session and the Application in DotNet 2.0.
int ConnectedCount = 0;
if (System.Web.HttpContext.Current.User.Identity.IsAuthenticated)
{
//Dynamic string if you have more than 1 group of users
string HashTableName = "NameOfMyHashtable"
if (System.Web.HttpContext.Current.Session != null)
{
//Create new Hashtable if null:
Application[HashTableName] = Application[HashTableName] ?? new Hashtable();
//region Refresh information about current User
if (((Hashtable)Application[HashTableName]).Contains
(System.Web.HttpContext.Current.Session.SessionID))
{
((Hashtable) Application[HashTableName]).Remove
(System.Web.HttpContext.Current.Session.SessionID);
}
((Hashtable) Application[HashTableName]).Add(System.Web.HttpContext.Current.Session.SessionID,
DateTime.Now.AddMinutes(System.Web.HttpContext.Current.Session.Timeout));
//region clean Hashtable
foreach (string pKeys in ((Hashtable) Application[HashTableName]).Keys)
{
if (((DateTime) ((Hashtable) Application[HashTableName])[pKeys]).CompareTo(DateTime.Now) <
connectedcount =" ((Hashtable)"> 0 )
{
lblUsersConnected.Attributes.Add("class", "fontMessageError fade");
lblUsersConnected.Text = "Attention, il y a déjà " + ConnectedCount + " utilisateur connecté";
}
}
Libellés :
Application,
Code Analysis,
DotNet 2.0,
Hashtable
Collection was modified; enumeration operation may not execute.
If we want to change a value in a Hashtable, there is a problem with the Foreach loop:
Hashtable MyHt=new Hashtable();
MyHt.Add("my key", "my value");
.....
foreach (DictionaryEntry entry in MyHt)
{
MyHt[entry.Key] = "my value";
}
The is the error message :
"Collection was modified; enumeration operation may not execute."
So for resolve this, we juste do an
ArrayList arrayList = new ArrayList(((Hashtable)Application[AppName]).Keys);
IEnumerator listEnumerator = arrayList.GetEnumerator();
while (listEnumerator.MoveNext())
{
((Hashtable)Application[AppName])[Session.SessionID.ToString()] = DateTime.Now.AddMinutes(10);
Hashtable ht = (Hashtable)Application[AppName];
DateTime dateOfSession = Convert.ToDateTime(ht[listEnumerator.Current].ToString());
if (dateOfSession.CompareTo(DateTime.Now) < 0)
{
((Hashtable)Application[AppName]).Remove(Session.SessionID.ToString());
}
}
Hashtable MyHt=new Hashtable();
MyHt.Add("my key", "my value");
.....
foreach (DictionaryEntry entry in MyHt)
{
MyHt[entry.Key] = "my value";
}
The is the error message :
"Collection was modified; enumeration operation may not execute."
So for resolve this, we juste do an
ArrayList arrayList = new ArrayList(((Hashtable)Application[AppName]).Keys);
IEnumerator listEnumerator = arrayList.GetEnumerator();
while (listEnumerator.MoveNext())
{
((Hashtable)Application[AppName])[Session.SessionID.ToString()] = DateTime.Now.AddMinutes(10);
Hashtable ht = (Hashtable)Application[AppName];
DateTime dateOfSession = Convert.ToDateTime(ht[listEnumerator.Current].ToString());
if (dateOfSession.CompareTo(DateTime.Now) < 0)
{
((Hashtable)Application[AppName]).Remove(Session.SessionID.ToString());
}
}
Inscription à :
Articles (Atom)