Monday, August 29, 2011

Image alignment in C#



<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Default_aspx" %>
html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >"server"> Image Control<body> <form id="form1" runat="server">

Image Control

"img1" runat="server" AlternateText="Dan" ImageUrl="http:// />

"server" Text="Sample Button" /> "img2" runat="server" AlternateText="Dan" ImageUrl="http://www." />
"ddl" runat="server" AutoPostBack="True"> "NotSet" /> "AbsBottom" /> "AbsMiddle" /> "Top" /> "Bottom" /> "BaseLine" /> "TextTop" /> "Left" /> "Right" />
form>body>html>
File: Default.aspx.cs
using System;using System.Data;using System.Configuration;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Web.UI.HtmlControls;
public partial class Default_aspx : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { switch (ddl.SelectedIndex) { case 0: img1.ImageAlign = ImageAlign.NotSet; img2.ImageAlign = ImageAlign.NotSet; break; case 1: img1.ImageAlign = ImageAlign.AbsBottom; img2.ImageAlign = ImageAlign.AbsBottom; break; case 2: img1.ImageAlign = ImageAlign.AbsMiddle; img2.ImageAlign = ImageAlign.AbsMiddle; break; case 3: img1.ImageAlign = ImageAlign.Top; img2.ImageAlign = ImageAlign.Top; break; case 4: img1.ImageAlign = ImageAlign.Bottom; img2.ImageAlign = ImageAlign.Bottom; break; case 5: img1.ImageAlign = ImageAlign.Baseline; img2.ImageAlign = ImageAlign.Baseline; break; case 6: img1.ImageAlign = ImageAlign.Middle; img2.ImageAlign = ImageAlign.Middle; break; case 7: img1.ImageAlign = ImageAlign.TextTop; img2.ImageAlign = ImageAlign.TextTop; break; case 8: img1.ImageAlign = ImageAlign.Left; img2.ImageAlign = ImageAlign.Left; break; case 9: img1.ImageAlign = ImageAlign.Right; img2.ImageAlign = ImageAlign.Right; break; default: img1.ImageAlign = ImageAlign.NotSet; img2.ImageAlign = ImageAlign.NotSet; break; } }}

Sunday, August 28, 2011

Cookies In Client Side asp.net with C#


protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (Request.Cookies["Username"] != null)
Txtusername.Text = Response.Cookies["Username"].Value;
Txtusername.Attributes.Add("value", Request.Cookies["Username"].Value);
if (Request.Cookies["password"] != null)
Txtpassword.Attributes.Add("value", Request.Cookies["password"].Value);
if ((Request.Cookies["username"] != null) && (Request.Cookies["password"] != null))
Chkrember.Checked = true;
}
}
protected void Btnsubmit_Click(object sender, EventArgs e)
{
if ((this.Chkrember.Checked))
{
if ((Request.Browser.Cookies))
{
Response.Cookies["username"].Value = Txtusername.Text;
Response.Cookies["username"].Expires = DateTime.Now.AddYears(20);
Response.Cookies["password"].Value = Txtpassword.Text;
Response.Cookies["password"].Expires = DateTime.Now.AddYears(20);
}
else
{
Response.Cookies["username"].Expires = DateTime.Now.AddYears(-20);
Response.Cookies["password"].Expires = DateTime.Now.AddYears(-20);
} }
}



How can I bind to a DataSet?
DataSet ds = GetDataSet();
DataView view = ds.Tables[0].DefaultView;
SmoothLineChart chart = new SmoothLineChart();
chart.Legend = "Value 1";
chart.DataSource = view;
chart.DataXValueField = "Description";
chart.DataYValueField = "Value1";
chart.DataBind();
ChartControl1.Charts.Add(chart);
ChartControl1.RedrawChart();

protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
LoadChartData();
}
}


private void LoadChartData()
{
string constr = @"Server=TestServer; Database=SampleDatabase; uid=sa; pwd=123;";
string query = "SELECT c.CategoryID, c.CategoryName, " +
" Count(p.UnitPrice) AS 'TotalProducts' " +
" FROM Categories c " +
" INNER JOIN Products p ON c.CategoryID = p.CategoryID " +
" GROUP BY c.CategoryID, c.CategoryName";

SqlDataAdapter da = new SqlDataAdapter(query, constr);
DataTable table = new DataTable();
da.Fill(table);

DataView dv = table.DefaultView;

Chart1.Series["Series1"].Points.DataBindXY(dv, "CategoryName", dv, "TotalProducts");
Chart2.Series["Series1"].Points.DataBindXY(dv, "CategoryName", dv, "TotalProducts");
}

Drop Down List in Multiple Selected Items

For example if u select in country and it will come state and city
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

public partial class _Default : System.Web.UI.Page
{
//Class1 obj = new Class1();
private string strconnection = "Data Source=NCORE-8;Initial Catalog=dropdown;Trusted_Connection=true;Integrated Security=True ";
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindCountryDropdown();
}
}
protected void BindCountryDropdown()
{
SqlConnection con = new SqlConnection(strconnection);
con.Open();

SqlCommand cmd = new SqlCommand("select * from Country", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
con.Close();
ddlCountry.DataSource = ds;
ddlCountry.DataTextField = "CountryName";
ddlCountry.DataValueField = "CountryID";
ddlCountry.DataBind();
ddlCountry.Items.Insert(0, new ListItem("--Select--", "0"));
ddlState.Items.Insert(0, new ListItem("--Select--", "0"));
ddlCity.Items.Insert(0, new ListItem("--Select--", "0"));

}

protected void ddlCountry_SelectedIndexChanged(object sender, EventArgs e)
{
int CountryID = Convert.ToInt32(ddlCountry.SelectedValue);
SqlConnection con = new SqlConnection(strconnection);
con.Open();


SqlCommand cmd = new SqlCommand("select * from State where CountryID=" + CountryID, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();

da.Fill(ds);
con.Close();

ddlState.DataSource = ds;
ddlState.DataTextField = "StateName";
ddlState.DataValueField = "StateID";
ddlState.DataBind();
ddlState.Items.Insert(0, new ListItem("--Select--", "0"));

if (ddlState.SelectedValue == "0")
{
ddlCity.Items.Clear();
ddlCity.Items.Insert(0, new ListItem("--Select--", "0"));
}
}


protected void ddlState_SelectedIndexChanged(object sender, EventArgs e)
{
int StateID = Convert.ToInt32(ddlState.SelectedValue);
SqlConnection con = new SqlConnection(strconnection);
con.Open();

SqlCommand cmd = new SqlCommand("select * from City where StateID=" + StateID, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();

da.Fill(ds);
con.Close();

ddlCity.DataSource = ds;
ddlCity.DataTextField = "CityName";
ddlCity.DataValueField = "CityID";
ddlCity.DataBind();
ddlCity.Items.Insert(0, new ListItem("--Select--", "0"));
}
}

Webconfig




App code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;

///


/// Summary description for Class1
///

public class Class1
{
public string CountryName, StateName, CityName , cs;
public int CountryId, StateID, CityID;
public Class1()
{
cs = System.Configuration.ConfigurationManager.AppSettings["Connection"].ToString();
}


}