Wednesday, December 28, 2011

Query string in asp.net

--------------------------------------------------------

Page.ClientScript.RegisterStartupScript(this.GetType(), "ale", "window.open('" + "Result.aspx?id=" + LoginId_lbl.Text + "&Project=" + selected_project + "',null,'height=200,width=200');", true);

id = Request.QueryString["id"];
proj = Request.QueryString["Project"];

Listbox item movement from right to left, Moveup and Movedown

-----------------------------------------------------------

protected void Button2_Click(object sender, EventArgs e)
{
ListItem[] myListItemArray = new ListItem[ListBox1.Items.Count];
ListBox1.Items.CopyTo(myListItemArray, 0);

foreach (ListItem myItem in myListItemArray)
{
ListBox2.Items.Add(myItem);
}
ListBox1.Items.Clear();
}

protected void moveRight_Click(object sender, EventArgs e)
{
for (int i = 0; i <= ListBox1.Items.Count-1; i++)
{
if (ListBox1.Items[i].Selected == true)
{
ListBox2.Items.Add(ListBox1.Items[i]);
}


}
for (int i = ListBox1.Items.Count - 1; i >= 0; i--)
{
if (ListBox1.Items[i].Selected == true)
{
ListItem li = ListBox1.Items[i];
ListBox1.Items.Remove(li);
}

}

}
protected void moveLeft_Click(object sender, EventArgs e)
{
for (int i = 0; i <= ListBox2.Items.Count - 1; i++)
{
if (ListBox2.Items[i].Selected == true)
{
ListBox1.Items.Add(ListBox2.Items[i]);
}


}
for (int i = ListBox2.Items.Count - 1; i >= 0; i--)
{
if (ListBox2.Items[i].Selected == true)
{
ListItem li = ListBox2.Items[i];
ListBox2.Items.Remove(li);
}

}


}
protected void Button3_Click(object sender, EventArgs e)
{
ListItem[] myListItemArray = new ListItem[ListBox2.Items.Count];
ListBox2.Items.CopyTo(myListItemArray, 0);

foreach (ListItem myItem in myListItemArray)
{
ListBox1.Items.Add(myItem);
}
ListBox2.Items.Clear();
}

protected void moveup_btn_Click(object sender, EventArgs e)
{
for (int i = 0; i < ListBox2.Items.Count; i++)
{
if (ListBox2.Items[i].Selected)
{
if (i > 0 && !ListBox2.Items[i - 1].Selected)
{
ListItem bottom = ListBox2.Items[i];
ListBox2.Items.Remove(bottom);
ListBox2.Items.Insert(i - 1, bottom);
ListBox2.Items[i - 1].Selected = true;
}
}
}

}
protected void movedown_btn_Click(object sender, EventArgs e)
{
int startindex = ListBox2.Items.Count - 1;
for (int i = startindex; i > -1; i--)
{
if (ListBox2.Items[i].Selected)
{
if (i < startindex && !ListBox2.Items[i + 1].Selected)
{
ListItem bottom = ListBox2.Items[i];
ListBox2.Items.Remove(bottom);
ListBox2.Items.Insert(i + 1, bottom);
ListBox2.Items[i + 1].Selected = true;
}
}
}
}

Creating Sessions in asp.net

--------------------------------------

if (Session["selected_cols"] != null)
{
datacols = Convert.ToString(Session["selected_cols"]);

}

Session["selected_cols"] = temp;

Highlighting gridview rows when mouseover and mouseout

--------------------------------------------------------------

protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowIndex != -1)
{
e.Row.Attributes.Add("onMouseOver", "this.style.background='#D3D3D3'");
e.Row.Attributes.Add("onMouseOut", "this.style.background='#ffffff';this.style.color='#000066'");
e.Row.Attributes.Add("onClick", "this.style.background='#009ACD';this.style.color='white'");
}

}

Summing up the gridview column values

-------------------------------------------------------------

int total;
bool ck=false;

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{

if (e.Row.RowType == DataControlRowType.Header)
{
if (e.Row.Cells[i].Text == "Field_Name")
{
ck = true;
}
}


if (e.Row.RowType == DataControlRowType.DataRow)
{
if (ck == true)
total += Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "Field_Name"));

}

if (e.Row.RowType == DataControlRowType.Footer)
{
if (ck_Off == true)
e.Row.Cells[GetColumnIndexByHeaderText(GridView1, "Field_Name")].Text = off_Total.ToString();
}

}

Getting Column Index in gridview

------------------------------------------------------------------

static public int GetColumnIndexByHeaderText(GridView aGridView, String ColumnText)
{
TableCell Cell;
for (int Index = 0; Index < aGridView.HeaderRow.Cells.Count; Index++)
{
Cell = aGridView.HeaderRow.Cells[Index];
if (Cell.Text.ToString() == ColumnText)
return Index;
}
return -1;
}

GetColumnIndexByHeaderText(GridView1, "Field_Name");

Class (.cs) file binding data to chart from database

--------------------------------------------

public void chart(string sqlstr, string xval, string yval, Chart chartname)
{
ArrayList yVal_Overall = new ArrayList();
ArrayList xVal_Overall = new ArrayList();

string connectionInfo = ConfigurationManager.AppSettings["ConnectionInfo"];
MySqlConnection con = new MySqlConnection(connectionInfo);
con.Open();

cmd = new MySqlCommand(sqlstr, con);

dr = cmd.ExecuteReader();
while (dr.Read())
{
skillname = dr[xval].ToString();
queuecount = Convert.ToInt32(dr[yval]);
xVal_Overall.Add(skillname);
yVal_Overall.Add(queuecount);
}
dr.Close();
chartname.Series[0].Points.DataBindXY(xVal_Overall, yVal_Overall);

con.Close();

}


Datachart chart_class = new Datachart();
chart_class.chart("call chart", " Field_Name1", " Field_Name2", Chart1);

Class (.cs) file binding data to gridview from database

---------------------------------------------------------


public void fileread(string sqlstr, GridView grdview)
{
try
{
string connectionInfo = ConfigurationManager.AppSettings["ConnectionInfo"];
MySqlConnection con = new MySqlConnection(connectionInfo);
con.Open();

cmd = new MySqlCommand(sqlstr, con);
dr = cmd.ExecuteReader();
grdview.DataSource = dr;
grdview.DataBind();
dr.Close();

con.Close();
}
catch (Exception ex)
{
ex.ToString();
}
}

Dataread drgrd = new Dataread();
drgrd.fileread("select * from table", GridView1);

Database Connection Settings in Web.Config

-----------------------------------------