Thursday, July 8, 2010

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

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); }
}

Forms Login C#

create admin folder

protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
{
//if(database validates user then run this code)
if (FormsAuthentication.Authenticate("admin", "pass"))
FormsAuthentication.RedirectFromLoginPage("admin", true);
}

<configuration>
<system.web>
<authentication mode="Forms">
<forms defaulturl="~Admin/admin.aspx" loginurl="~/default.aspx" slidingexpiration="true" timeout="20">
<credentials passwordformat="Clear">
<user name="admin" password="pass">
</user></credentials>
</forms>
</authentication>
</system.web>
<location path="admin">
<system.web>
<authorization>
<allow users="admin">
<deny users="*">
</deny></allow></authorization>
</system.web>
</location>
</configuration>

Create word with C# Open XML

http://www.iamraf.net/Articles/Creating-Word-docx-document-using-Office-Open-XML-SDK-20

Create word with C# Open XML

http://www.iamraf.net/Articles/Creating-Word-docx-document-using-Office-Open-XML-SDK-20

Create Dynamic Controls

//pnlTransferRequestOutside is only control needed on aspx page
protected void Page_Load(object sender, EventArgs e)
{
//recreate Textboxes
int count = Convert.ToInt32(ViewState["Count"]) + 1;
for (int i = 1; i < count; i++) {
CreateTextBox(i.ToString());
}
}

protected void btnAddTransfer_Click(object sender, EventArgs e)
{
if (ViewState["Count"] != null)
ViewState["Count"] = Convert.ToInt32(ViewState["Count"].ToString()) + 1;
else
ViewState["Count"] = 1;

CreateTextBox(ViewState["Count"].ToString());
}

protected void CreateTextBox(string id)
{
//create new textbox
Label lbl = new Label();
lbl.Text = "ID:";
pnlTransferRequestOutside.Controls.Add(lbl);
TextBox tb = new TextBox();
tb.ID = "tb" + id;
pnlTransferRequestOutside.Controls.Add(tb);
tb.Text = Request.Form[tb.ClientID];
RangeValidator rv = new RangeValidator();
rv.ControlToValidate = tb.ID;
rv.ErrorMessage = "Numbers Only";
rv.Type = ValidationDataType.Integer;
rv.MaximumValue = "999999999";
rv.MinimumValue = "0";
rv.SetFocusOnError = true;
pnlTransferRequestOutside.Controls.Add(rv);
}