Monday, December 14, 2009

ASP.Net button events not firing

I had a unique issue took me about 2 hours to figure out so to help others here it is.
my button events weren't firing in every browser ie8, chrome, firefox i also couldn't load my source file. Got diagnostic connection problem in ie 8.

issue was my viewstate code was huge in the source file thats why i couldn't see it.
because viewstate was so large events wouldn't fire fix is EnableViewState="false"

Thursday, September 03, 2009

Session State for C#

public static class CurrentContext
{
private static T GetFromSession(string key)
{
object obj = HttpContext.Current.Session[key];
if (obj == null)
{
return default(T);
}
return (T)obj;
}

private static void SetInSession(string key, T value)
{
if (value == null)
{
HttpContext.Current.Session.Remove(key);
}
else
{
HttpContext.Current.Session[key] = value;
}
}

private static T GetFromCache(string key)
{
object obj = HttpContext.Current.Cache[key];
if (obj == null)
{
return default(T);
}
return (T)obj;
}

private static void SetInCache(string key, T value)
{
if (value == null)
{
HttpContext.Current.Cache.Remove(key);
}
else
{
HttpContext.Current.Cache[key] = value;
}
}

public static Order CurrentOrder
{
get { return GetFromSession("CurrentOrder"); }
set { SetInSession("CurrentOrder", value); }
}

public static OrderCustomer CurrentOrderCustomer
{
get { return GetFromSession("CurrentOrderCustomer"); }
set { SetInSession("CurrentOrderCustomer", value); }
}

Friday, August 21, 2009

jQuery: grab array foreach through

$("input[name=quantity]").each(function(i) { this.style.background = "#ffffff"; });

Thursday, August 13, 2009

Javascript event anywhere on page calls function

document.onkeypress = stopRKey;
function stopRKey(evt) {
var evt = (evt) ? evt : ((event) ? event : null);
if ((evt.keyCode == 13)) {
return false;
}
}
//not sure how javascript knows what "evt" is but it does.

Tuesday, August 11, 2009

How to do jquery postback using C#

simple and sweet C# function dont forget attribute.


[System.Web.Services.WebMethod]
public static string GetCustomerCreditCardInfo(string accountNumber)
{
System.Data.DataTable dt = Order.GetOrderHistoryByAccountId(int.Parse(acountNumber));
return "";
}

place this text in a jquery function

$.ajax({ type: "POST", url: "PosOrderEntry.aspx/GetCustomerCreditCardInfo", data: "{accountNumber : '" + accountId + "'}", contentType: "application/json; charset=utf-8", dataType: "json",
success: function(msg) {
if (msg.d == "") {
alert('nothing'); //failure
}
alert(msg.d); //success
}
});
//notice that the accountNumber has to have the same name as the c#function argument name

Tuesday, April 29, 2008

Securing website without using default DB but using FormsAuthentication

Ok, Im so excited to have finally figured this out. Many times a programmer is going to want to give access to certain folders in their site but not want to use the default database that comes with VS.

so here's the code

//check if they login to database or xml file, the 1st 2 lines of code will be you connecting to
//DB and then determining what that users Status should be.

Login Login1 = (Login)LoginView1.FindControl("Login1");
if ((Login1.UserName == "test") && (Login1.Password == "pass"))
{

if (Request.QueryString["ReturnUrl"] != null)
{
FormsAuthentication.RedirectFromLoginPage("admin", false);
//if my DB says they are admin then i make them an admin and redirect to page they where
//trying to access
}
else
{
//if my DB says they are admin then i make them an admin and redirect to default.aspx
FormsAuthentication.SetAuthCookie("admin", false);
Response.Redirect("default.aspx");
}
}
else
{
//login from DB or XML failed
}

heres the web.config file placed in folder u want to protect i had to take out tags cause blog doesn't like them. deal with it

system.web
authorization
allow users="admin"/
deny users="*" /
/authorization
/system.web

Monday, April 28, 2008

using roles and logins

protected void CreateUserWizard1_CreatedUser(object sender, EventArgs e)
{
Roles.AddUserToRole(CreateUserWizard1.UserName, "client");
}



protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
{
Login Login1 = (Login)LoginView1.FindControl("Login1");
if (Membership.ValidateUser(Login1.UserName, Login1.Password))
{
if (Request.QueryString["ReturnUrl"] != null)
{
Response.Write("redirect");
FormsAuthentication.RedirectFromLoginPage(Login1.UserName, false);
}
else
{
FormsAuthentication.SetAuthCookie(Login1.UserName, false);
Response.Write("set cookie");
e.Authenticated = true;
}
}
else
{
Response.Write("you're not welcomed");
}
}