Share Blog

Wednesday, January 28, 2015

How to use Repeater In Asp.net

-----------------------------------------------------Create Table----------------------------------------------

CREATE TABLE [dbo].[news_headlines](
      [ID] [int] primary key IDENTITY(1,1) NOT NULL,
      [Username] [varchar](50) NULL,
      [Heading] [varchar](max) NULL,
      [Discription] [varchar](max) NULL,
      [Posteddate] [datetime] NULL

)

----------News_Headlines.aspx-----------------

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

<!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 id="Head1" runat="server">
<title>Latest News Example</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr>
<td>Enter Name: </td>
<td><asp:TextBox ID="txtName" runat="server"/></td>
</tr>
<tr>
<td>Enter HeadLines: </td>
<td><asp:TextBox ID="txtHeadlines" runat="server"/></td>
</tr>
<tr>
<td valign="top">Enter Discription:</td>
<td><asp:TextBox ID="txtDiscription" runat="server" Rows="5" Columns="20" TextMode="MultiLine"/></td>
</tr>
<tr>
<td></td>
<td><asp:Button ID="btnSubmit" runat="server" Text="Submit" onclick="btnSubmit_Click" /></td>
</tr>
</table>
</div>
<div>
<asp:Repeater ID="RepDetails" runat="server">

<HeaderTemplate>
<table style=" border:1px solid #df5015; width:500px" cellpadding="0">
<tr style="background-color:Navy; color:White">
<td colspan="2">
<b>Latest News</b>
</td>
</tr>
</HeaderTemplate>

<ItemTemplate>
<tr style="background-color:#EBEFF0">
<td>
<table style="background-color:#EBEFF0;border-top:1px dotted #df5015; width:500px" >
<tr>
<td style="line-height:35px">
Headlines:
<asp:Label ID="lblHeadlines" runat="server" Text='<%#Eval("Heading") %>' Font-Bold="true"/>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td >
<asp:Label ID="lblDiscription" runat="server" Text='<%#Eval("Discription") %>'/>
</td>
</tr>
<tr>
<td>
<table style="background-color:#EBEFF0;border-top:1px dotted #df5015;border-bottom:1px solid #df5015; width:500px" >
<tr>
<td>Post By: <asp:Label ID="lblUser" runat="server" Font-Bold="true" Text='<%#Eval("UserName") %>'/></td>
<td>Post Date:<asp:Label ID="lblDate" runat="server" Font-Bold="true" Text='<%#Eval("PostedDate") %>'/></td>
</tr>
</table>
</td>
</tr>
<tr>
<td colspan="2">&nbsp;</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
</div>
</form>
</body>
</html>

 ----------News_Headlines.aspx.cs----------------
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 News_Headlines : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ConnectionString);
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindData();
        }
    }
    protected void BindData()
    {
        con.Open();
        SqlCommand cmd = new SqlCommand("select * from News_headlines Order By PostedDate desc", con);
        DataSet ds = new DataSet();
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        da.Fill(ds);
        RepDetails.DataSource = ds;
        RepDetails.DataBind();
        con.Close();
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        con.Open();
        SqlCommand cmd = new SqlCommand("insert into News_headlines (UserName,Heading,Discription,PostedDate) values(@userName,@Heading,@Discription,@postedDate)", con);
   cmd.Parameters.AddWithValue("@userName", txtName.Text);
   cmd.Parameters.AddWithValue("@Heading", txtHeadlines.Text);
   cmd.Parameters.AddWithValue("@Discription", txtDiscription.Text);
   cmd.Parameters.AddWithValue("@postedDate", DateTime.Now);
   cmd.ExecuteNonQuery();
   con.Close();
   txtName.Text = string.Empty;
   txtHeadlines.Text = string.Empty;
  txtDiscription.Text = string.Empty;
   BindData();
}

    }




No comments:

Post a Comment