Archive

Archive for May, 2013

Redirect to Another Page by Click on LinkButton in GridView

Hi all,

try this example to Redirect to Another Page by Click on LinkButton in GridView

1) Open VS 2008 and create a new web site

2) Add New Web Page called “Default.aspx”  and from Toolbox drag and drop Gridview to be like this


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
 <title></title>
</head>
<body>
 <form id="form1" runat="server">
 <div>
 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" >
 <Columns>
 <asp:TemplateField>
 <ItemTemplate>
 <asp:LinkButton runat="server" CommandArgument='<%# Eval("Id") %>' OnCommand="LinkButton_Click" Text="View Details"> </asp:LinkButton>
 </ItemTemplate>
 </asp:TemplateField>
 </Columns>
 </asp:GridView>
 </div>
 </form>
</body>
</html>

In code behind :


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;


public partial class _Default : System.Web.UI.Page
{
 protected void Page_Load(object sender, EventArgs e)
 {
 string con = ConfigurationManager.ConnectionStrings["connection"].ConnectionString;
 SqlConnection conn = new SqlConnection(con);
 conn.Open();
 string str = "select id from flatfile_logged";
 SqlCommand cmd = new SqlCommand(str, conn);
 SqlDataAdapter da = new SqlDataAdapter(cmd);
 DataTable dt = new DataTable();
 da.Fill(dt);
 GridView1.DataSource = dt;
 GridView1.DataBind();
 }
 protected void LinkButton_Click(Object sender, CommandEventArgs e)
 {
 if (e.CommandArgument != null)
 {
 Response.Redirect("Details.aspx?Id=" + e.CommandArgument.ToString());
 }
 }
}

3) Add another web page to display Details called “Details.aspx”


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="yourpage.aspx.cs" Inherits="yourpage" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
 <title></title>
</head>
<body>
 <form id="form1" runat="server">
 <div>
 <asp:GridView ID="GridView1" runat="server">
 </asp:GridView>
 </div>
 </form>
</body>
</html>

In code behind :


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 yourpage : System.Web.UI.Page
{
 protected void Page_Load(object sender, EventArgs e)
 {
 int passedId = Convert.ToInt32(Request.QueryString["Id"]);
 string con = ConfigurationManager.ConnectionStrings["connection"].ConnectionString;
 SqlConnection conn = new SqlConnection(con);
 conn.Open();
 string str = "select * from flatfile_logged where id="+Id+"";
 SqlCommand cmd = new SqlCommand(str, conn);
 SqlDataAdapter da = new SqlDataAdapter(cmd);
 DataTable dt = new DataTable();
 da.Fill(dt);
 GridView1.DataSource = dt;
 GridView1.DataBind();
 }
}

Hope this helps

Good Luck.

 

Advertisements
Categories: ASP.Net

Month year day DropDownList for Date Selection in ASP.NET

Hi all,

try this example to display Month year day DropDownList for Date Selection in ASP.NET


<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
 <title></title>
</head>
<body>
 <form id="form1" runat="server">
 <asp:ScriptManager ID="sc" runat="server">
 </asp:ScriptManager>
 <table>
 <tr>
 <td>
 <asp:UpdatePanel ID="updpnlMonth" runat="server" ChildrenAsTriggers="false" UpdateMode="Conditional">
 <ContentTemplate>
 <asp:DropDownList ID="ddlMonth" runat="server" AutoPostBack="true" DataTextField="MonthName"
 DataValueField="MonthNumber" OnSelectedIndexChanged="ddlMonth_SelectedIndexChanged">
 </asp:DropDownList>
 </ContentTemplate>
 </asp:UpdatePanel>
 </td>
 <td>
 <asp:UpdatePanel ID="updpnlDay" runat="server" UpdateMode="Conditional">
 <ContentTemplate>
 <asp:DropDownList ID="ddlday" runat="server">
 </asp:DropDownList>
 </ContentTemplate>
 <Triggers>
 <asp:AsyncPostBackTrigger ControlID="ddlMonth" EventName="SelectedIndexChanged" />
 </Triggers>
 </asp:UpdatePanel>
 </td>
 <td>
 <asp:DropDownList ID="ddlYear" runat="server">
 </asp:DropDownList>
 </td>
 </tr>
 </table>
 </form>
</body>
</html>

In Code behind :


using System;
using System.Globalization;
using System.Linq;
using System.Web.UI.WebControls;

protected void Page_Load(object sender, EventArgs e)
{
 if (!IsPostBack)
 {
 //Populate DropDownLists
 ddlMonth.DataSource = Enumerable.Range(1, 12).Select(a => new
 {
 MonthName = DateTimeFormatInfo.CurrentInfo.GetMonthName(a),
 MonthNumber = a
 });
 ddlMonth.DataBind();
 ddlYear.DataSource = Enumerable.Range(DateTime.Now.Year - 99, 100).Reverse();
 ddlYear.DataBind();
 ddlday.DataSource = Enumerable.Range(1, DateTime.DaysInMonth(DateTime.Now.Year, Convert.ToInt32(ddlMonth.SelectedValue)));
 ddlday.DataBind();
 }
}

protected void ddlMonth_SelectedIndexChanged(object sender, EventArgs e)
{
 ddlday.DataSource = Enumerable.Range(1, DateTime.DaysInMonth(DateTime.Now.Year, Convert.ToInt32(ddlMonth.SelectedValue)));
 ddlday.DataBind();
}

Hope this helps

Good Luck.

Categories: ASP.Net

Create Chart from Database using Google API

Hi all,

try this example to Create Chart from Database using Google API

1) Open SQL Server and create a new database called “ChartsDB”

2) Add new Table called “tblChart” with the following colomns

tblChart

 

3) Open VS 208 and create a new Website and add new Page, so your page will be like this


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
 <title></title>
 <script type="text/javascript" src="https://www.google.com/jsapi"></script>
</head>
<body>
 <form id="form1" runat="server">
 <div>
 <asp:Literal ID="lt" runat="server"></asp:Literal>
 </div>
 <div id="divLineChart"></div>
 </form>
</body>
</html>

and In code behind add the following :


SqlConnection Conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["conn"].ConnectionString);
 StringBuilder str = new StringBuilder();
 protected void Page_Load(object sender, EventArgs e)
 {
 Conn.Open();
 if (!IsPostBack)
 {
 bindChart();
 }
 }
 private void bindChart()
 {
 SqlDataAdapter da = new SqlDataAdapter("select * from tblChart", Conn);
 DataTable dt = new DataTable();
 try
 {
 da.Fill(dt);
 // This data is coming from the sql server
 str.Append(@"<script type=text/javascript> google.load( *visualization*, *1*, {packages:[*corechart*]});
 google.setOnLoadCallback(drawChart);
 function drawChart() {

 var data = new google.visualization.DataTable();
 data.addColumn('string', 'Year');
 data.addColumn('number', 'Sales');
 data.addColumn('number', 'Expenses');
 data.addRows(" + dt.Rows.Count + ");");

Int32 i;
 for (i = 0; i <= dt.Rows.Count - 1; i++)
 {
 str.Append("data.setValue( " + i + "," + 0 + "," + "'" + dt.Rows[i]["year"].ToString() + "');");
 str.Append("data.setValue(" + i + "," + 1 + "," + dt.Rows[i]["sales"].ToString() + ") ;");
 str.Append(" data.setValue(" + i + "," + 2 + "," + dt.Rows[i]["expences"].ToString() + ");");
 }
 str.Append("var chart = new google.visualization.LineChart(document.getElementById('divLineChart'));");
 str.Append("chart.draw(data, {width: 650, height: 300, legend: 'bottom',is3D: false,title: 'Performance',");
 str.Append("vAxis: {title: 'Year', titleTextStyle: {color: 'green'}}");
 str.Append("}); }");
 str.Append("</script>");

lt.Text = str.ToString().TrimEnd(',').Replace('*', '"');
 Conn.Close();
 }
 catch
 { }
 finally
 { Conn.Close(); }
 }

Don’t forget to use the following Namespace

using System.Data;

using System.Data.SqlClient;
using System.Text;

Hope this helps

Good Luck

 

 

Categories: ASP.Net

Allow User to Login Using UserName or Email

Hi all,

Try this example to allow user to Login using UserName Or Email address using Membership Provider

In Login Page :


<asp:Login runat="server" ID="Login1" DisplayRememberMe="false"
 DestinationPageUrl="/Users/Default.aspx" onloggingin="Login1_LoggingIn" >
</asp:Login>

In Code behind :


protected void Login1_LoggingIn(object sender, LoginCancelEventArgs e)
 {
 if (Login1.UserName.Contains("@") && Membership.GetUser(Login1.UserName) == null)
 {
 string Username = Membership.GetUserNameByEmail(Login1.UserName);
 if (Username == null)
 return;

 if (Membership.ValidateUser(Username, Login1.Password) == true)
 {
 FormsAuthentication.SetAuthCookie(Username, false);
 FormsAuthentication.RedirectFromLoginPage(Username, false);
 e.Cancel = true;
 }
 }
 }

Hope this helps

Good Luck.

Categories: ASP.Net

News Ticker With JQuery

Hi all,

try this example for News Ticker With JQuery

1) Open VS 2008 and create a new Web site

2) Add new Web Page and from ToolBox drag Repeater Control into your page


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
 <script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.5.js" type="text/javascript"></script>
 <script src="http://www.webdesignbooth.com/demo/news-ticker/jcarousellite_1.0.1c4.js"
 type="text/javascript"></script>
 <script type="text/javascript">
 $(function() {
 $(".newsticker").jCarouselLite({
 visible: 3,
 btnNext: ".next",
 btnPrev: ".prev"
 });
 });
 </script>
 <style>
 .newsticker ul li
 {
 display: block;
 list-style: none outside none;
 margin-bottom: 5px;
 margin-left:10px;
 border:solid 2px gray;
 padding-bottom: 1px;
 background-color: #EAF4F5;
 }
 .newsticker .thumbnail
 {
 float: left;
 width: 130px;
 }
 .newsticker .info
 {
 margin-left: 10px;
 float: right;
 width: 190px;
 }
 .newsticker .info span.cat
 {
 color: #808080;
 display: block;
 font-size: 10px;
 }
 .newsticker .info a
 {
 color: #3c7acf;
 text-decoration: none;
 }
 .clear
 {
 clear: both;
 }
 </style>
</head>
<body>
 <form id="form1" runat="server">
 <div class="title">
 Latest News</div>
 <asp:Repeater ID="dlNews" runat="server">
 <HeaderTemplate>
 <div class="newsticker">
 <input value="Previous" class="prev" type="button" />
 <ul>
 </HeaderTemplate>
 <ItemTemplate>
 <li>
 <div class="thumbnail">
 <img src='<%#Eval("ThumbNail")%>'>
 </div>
 <div class="info">
 <a href='<%#Eval("URL")%>'>
 <%#Eval("Title")%></a> <span class="cat">Category:
 <%#Eval("Category")%></span>
 </div>
 <div class="clear">
 </div>
 </li>
 </ItemTemplate>
 <FooterTemplate>
 </ul>
 <input value="Next" class="next" type="button" />
 </div>
 </FooterTemplate>
 </asp:Repeater>
 </form>
</body>
</html>

and in code behind add the following code


protected void Page_Load(object sender, EventArgs e)
 {
 var list = (new[] {
 new {
 ThumbNail ="http://www.gallery2c.com/admin/Upload/ThumbNail/roan12.jpg",
 URL="http://www.vladstudio.com/wallpaper/?knight_lady",
 Title="The Knight and the Lady",
 Category="Illustrations"
 },
 new {
 ThumbNail ="http://www.gallery2c.com/admin/Upload/ThumbNail/roan15.jpg",
 URL="http://www.vladstudio.com/wallpaper/?rainbow_butterfly",
 Title="Rainbow Butterfly",
 Category="Abstract art"
 },
 new {
 ThumbNail ="http://www.gallery2c.com/admin/Upload/ThumbNail/roan16.jpg",
 URL="http://www.vladstudio.com/wallpaper/?teddybear_mac",
 Title="Teddy Bear and MacBook",
 Category="Photos"
 },
 new {
 ThumbNail ="http://www.gallery2c.com/admin/Upload/ThumbNail/roan17.jpg",
 URL="http://www.vladstudio.com/wallpaper/?family_of_colors",
 Title="The Family of Colors",
 Category="Creatures"
 }
 }).ToList();
 dlNews.DataSource = list;
 dlNews.DataBind();
 }

Hope this helps

Good Luck.

Categories: ASP.Net, Jquery