Friday, October 11, 2019

How to make Project in Visual Studio with Repeater Control (Discount, Amount)


Working With Repeater

Before we learnt about templates and one example for Working with Repeater but This article  I will explain How to use Repeater with two projects

Open Visual Studio and make new Project and Add new webform for Repeater 

*Click on website > add new item > webform
*Click on design>toolbox>data>Repeater

# In code file

using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;


namespace WebApplication6
{
    public partial class WebForm2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Rep_Bind();
        }

        private void Rep_Bind()
        {
            SqlDataAdapter adp = new SqlDataAdapter("select * from tbbook", ConfigurationManager.ConnectionStrings["ms"].ConnectionString);
            DataSet ds = new DataSet();
            adp.Fill(ds);
            Repeater1.DataSource = ds;
            Repeater1.DataBind();

        }


# Now open source code

<asp:Repeater ID="Repeater1" runat="server" OnItemCommand="Repeater1_ItemCommand">
                <HeaderTemplate>
                    <table border="1">
                    <tr style="background-color:aqua">
                        <th>Id</th>
                        <th>Title</th>
                        <th>Author</th>
                        <th>Publisher</th>
                        <th>Price</th>
                        <th>Discount</th>
                        <th>Amount</th>
                    </tr>
                </HeaderTemplate>
                <ItemTemplate>
                    <tr style="background-color:burlywood">
                        <td><%#Eval("BookId")%></td>
                        <td><%#Eval("BookTitle")%></td>
                        <td><%#Eval("BookAuthor")%></td>
                        <td><%#Eval("BookPublisher")%></td>
                        <td><%#Eval("BookPrice")%></td>
                        <td></td>
                        <td></td>
                    </tr>   
                   </ItemTemplate>
                <FooterTemplate>
                    </table>
                </FooterTemplate>

*HeaderTemplate – It runs one time only and it runs first
*FooterTemplate – It also run one time only but in last
<th> - table Heading
<td></td>
<td></td> Both are left like this because data of discount and amount is not in database.

 # To add data for discount and amount in html

*Remember only public and protected classes can be fetched in html not private class.
#Open code
private void Rep_Bind()
        {
            SqlDataAdapter adp = new SqlDataAdapter("select * from tbbook", ConfigurationManager.ConnectionStrings["ms"].ConnectionString);
            DataSet ds = new DataSet();
            adp.Fill(ds);
            Repeater1.DataSource = ds;
            Repeater1.DataBind();

        }

        public Int32 CalDis(Int32 prc)
        {
            return prc * 10 / 100;

        }

        public Int32 CalAmt(Int32 prc)
        {
            return prc - CalDis(prc);
        }

# Now Click on html source

Add data in the column
<td><%#Eval("BookId")%></td>
                        <td><%#Eval("BookTitle")%></td>
                        <td><%#Eval("BookAuthor")%></td>
                        <td><%#Eval("BookPublisher")%></td>
                        <td><%#Eval("BookPrice")%></td>
                        <td><%# CalDis (Convert.ToInt32(Eval("BookPrice"))) %></td>
                        <td><%# CalAmt (Convert.ToInt32(Eval("BookPrice"))) %></td>
                    </tr>   

# AlternatingItemTemplate

It works for alternating row

Click on source 

</ItemTemplate>
                <AlternatingItemTemplate>
                    <tr style="background-color:brown">
                        <td><%#Eval("BookId")%></td>
                        <td><%#Eval("BookTitle")%></td>
                        <td><%#Eval("BookAuthor")%></td>
                        <td><%#Eval("BookPublisher")%></td>
                        <td><%#Eval("BookPrice")%></td>
                        <td><%# CalDis (Convert.ToInt32(Eval("BookPrice"))) %></td>
                        <td><%# CalAmt (Convert.ToInt32(Eval("BookPrice"))) %></td>
                    </tr>
                </AlternatingItemTemplate>
                <FooterTemplate>
                    </table>
                </FooterTemplate>

# Repeater 2nd Example

<asp:Repeater ID="Repeater1" runat="server" OnItemCommand="Repeater1_ItemCommand">
                <HeaderTemplate>
                    <table border="1">
                    <tr style="background-color:aqua">
                        <th>Id</th>
                        <th>Title</th>
                        <th>Author</th>
                        <th>Publisher</th>
                        <th>Price</th>
                        <th>Discount</th>
                        <th>Amount</th>
                        <th>Image</th>
                    </tr>
                </HeaderTemplate>
                <ItemTemplate>
                    <tr style="background-color:burlywood">
                        <td><%#Eval("BookId")%></td>
                        <td><%#Eval("BookTitle")%></td>
                        <td><%#Eval("BookAuthor")%></td>
                        <td><%#Eval("BookPublisher")%></td>
                        <td><%#Eval("BookPrice")%></td>
                        <td><%#CalDis (Convert.ToInt32(Eval("BookPrice"))) %></td>
                        <td><%# CalAmt (Convert.ToInt32(Eval("BookPrice"))) %></td>
                        <td>
                            <img src ='<%#Eval("BookImg")%>' height="50" width="50" />
                        </td>
                    </tr>   
                   </ItemTemplate>
                <AlternatingItemTemplate>
                    <tr style="background-color:brown">
                        <td><%#Eval("BookId")%></td>
                        <td><%#Eval("BookTitle")%></td>
                        <td><%#Eval("BookAuthor")%></td>
                        <td><%#Eval("BookPublisher")%></td>
                        <td><%#Eval("BookPrice")%></td>
                        <td><%# CalDis(Convert.ToInt32(Eval("BookPrice"))) %></td>
                        <td><%# CalAmt (Convert.ToInt32(Eval("BookPrice"))) %></td>
                        <td>
                            <img src ='<%#Eval("BookImg")%>' height="50" width="50" />
                        </td>
                    </tr>
                </AlternatingItemTemplate>
                <FooterTemplate>
                    </table>
                </FooterTemplate>
            </asp:Repeater>

using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace WebApplication6
{
    public partial class WebForm3 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Rep_Bind();
        }

        private void Rep_Bind()
        {
            SqlDataAdapter adp = new SqlDataAdapter("select * from tbbook", ConfigurationManager.ConnectionStrings["ms"].ConnectionString);
            DataSet ds = new DataSet();
            adp.Fill(ds);
            Repeater1.DataSource = ds;
            Repeater1.DataBind();

        }
        public Int32 CalDis(Int32 prc)
        {
            return prc * 10 / 100;

        }

        public Int32 CalAmt(Int32 prc)
        {
            return prc - CalDis(prc);
        }




No comments:

Post a Comment