locked
Ajax Bidding and Time Control RRS feed

  • Question

  • User669521406 posted

    Hello everyone i''m working a project for a bidding auction but i have some bug in my code

    (i) i put the item in repeater but i can't get the timer to display each item their count down it is giving me once only 

    2) How to get the itemid of the item which time finished 

    Below are sample of code that i have work can anyone help please

    <%@ Page Language="VB" AutoEventWireup="false" CodeFile="Bidding.aspx.vb" Inherits="Bidding" %>
    
    <!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">
        
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
         <asp:ScriptManager ID="SM1" runat="server">
            </asp:ScriptManager>
     <asp:HiddenField ID="HiddenField1" runat="server" />
     
         <asp:repeater id="Repeater1" runat="server">
          <HeaderTemplate>
                <ul>
          </HeaderTemplate>
          <ItemTemplate>
    
              <li> <a href="ProductDetails.aspx?ItemID=<%# Eval("ItemID") %>"> <asp:Image id="Image1" runat="server" ImageUrl='<%#Eval("Image1") %>' /></a>
             
                <div class="product-info">
                   <h3> <%# Eval("Name")%></h3>
                  <div class="product-desc">
                    <h4>Bid</h4>
                      <asp:TextBox ID="txtbid" runat="server"></asp:TextBox><asp:Button ID="btnSave"
                          runat="server" Text="btnSave" />
                    
                    <strong class="price"><%# Eval("Price")%></strong>
                    <br/> <p>
                   
                     
    
                        <asp:UpdatePanel id="updPnl" runat="server" UpdateMode="Conditional">
      <ContentTemplate>
                       
                    <asp:Label ID="lblTimer"   runat="server"></asp:Label>
                         </ContentTemplate>
                                <Triggers>
                        <asp:AsyncPostBackTrigger ControlID="timer1" EventName ="tick" />
                        </Triggers>
                </asp:UpdatePanel>
                           </div>
                        
                        
                         </p>
                      
                   </div>
                    
                </div>
              </li>
              </ItemTemplate>
          <FooterTemplate>
                </ul>
          </FooterTemplate>
         </asp:repeater>
            
    
           <asp:Timer ID="timer1" Interval="1000" OnTick="timer1_tick" runat ="server">
                       </asp:Timer>
            
            
            
        </div>
        </form>
    
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
        <script type="text/javascript" src="http://cdn.jsdelivr.net/json2/0.1/json2.js"></script>
        <script type="text/javascript">
            $(function () {
                $("[id*=btnSave]").bind("click", function () {
                    var user = {};
                    user.BiddingPrice = $("[id*=txtbid]").val();
    
                    $.ajax({
                        type: "POST",
                        url: "Bidding.aspx/SaveUser",
                        data: '{user: ' + JSON.stringify(user) + '}',
                        contentType: "application/json; charset=utf-8",
                        dataType: "json",
                        success: function (response) {
                            alert("BID has been added successfully.");
                            window.location.reload();
                        }
                    });
                    return false;
                });
            });
        </script>
    </body>
    </html>
    
    Imports System.Data
    Imports System.Data.SqlClient
    Imports System.Configuration
    Imports System.Net.Mail
    Imports System.Web.Configuration
    Imports System.Web.Script.Services
    Imports System.Web.Services
    
    Partial Class Bidding
    
    
        Inherits System.Web.UI.Page
        Dim con As New SqlConnection(ConfigurationManager.ConnectionStrings("VirgoPlaza").ToString())
        Dim EndDate As DateTime
        Private ReadOnly _start As String
        Public Sub New()
            _start = WebConfigurationManager.ConnectionStrings("VirgoPlaza").ConnectionString
        End Sub
    
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            If Not Page.IsPostBack Then
    
                Using con1 As New SqlConnection(_start)
    
    
                    Dim sql1 As String = "SELECT Item.ItemID, Item.Name,Item.Image1, Item.Description, Item.Price FROM Item INNER JOIN Auction ON Item.ItemID = Auction.ItemID Where Auction.Status='Valid' AND Auction.EndDate>=@endate "
                    Dim myCommand1 As New SqlCommand(sql1, con1)
                    myCommand1.Parameters.AddWithValue("@endate", DateTime.Now)
    
                    myCommand1.CommandType = CommandType.Text
    
                    con1.Open()
    
                    Dim category As SqlDataReader
                    category = myCommand1.ExecuteReader()
    
                    'binding data from category table to ddlcategory
                    Repeater1.DataSource = category
                    Repeater1.DataBind()
    
                End Using
              
            End If
        End Sub
    
        <WebMethod()> _
        <ScriptMethod()> _
        Public Shared Sub SaveUser(ByVal user As BID)
            Dim constr As String = ConfigurationManager.ConnectionStrings("VirgoPlaza").ConnectionString
            Using con As New SqlConnection(constr)
                Using cmd2 As New SqlCommand("Select BiddingPrice From Bid")
                    cmd2.CommandType = CommandType.Text
    
                    con.Open()
                    cmd2.Connection = con
                    Dim result As Double = cmd2.ExecuteScalar
    
                    Dim result2 As Integer = Convert.ToInt32(result)
    
                    If result2 = 0 Then
                        Using cmd As New SqlCommand("INSERT INTO BID VALUES(@Date, @BiddingPrice,@Status,@AuctionID,@BuyerID)")
                            cmd.CommandType = CommandType.Text
                            cmd.Parameters.AddWithValue("@Date", DateTime.Now)
                            cmd.Parameters.AddWithValue("@BiddingPrice", user.BiddingPrice)
                            cmd.Parameters.AddWithValue("@Status", "Available")
                            cmd.Parameters.AddWithValue("@AuctionID", 11)
                            cmd.Parameters.AddWithValue("@BuyerID", 1)
    
                            cmd.Connection = con
                            con.Open()
                            cmd.ExecuteNonQuery()
                            con.Close()
                        End Using
                    Else
                        If result2 >= user.BiddingPrice Then
    
                            MsgBox("Bid should be greather")
    
                        Else
    
                            Dim finalbid As Double = result2
                            Using cmd As New SqlCommand("UPDATE  BID  SET Date =@Date,BiddingPrice= @BiddingPrice,Status=@Status,AuctionID=@AuctionID,BuyerID =@BuyerID")
                                cmd.CommandType = CommandType.Text
                                cmd.Parameters.AddWithValue("@Date", DateTime.Now)
                                cmd.Parameters.AddWithValue("@BiddingPrice", user.BiddingPrice)
                                cmd.Parameters.AddWithValue("@Status", "Available")
                                cmd.Parameters.AddWithValue("@AuctionID", 11)
                                cmd.Parameters.AddWithValue("@BuyerID", 1)
    
                                cmd.Connection = con
    
                                cmd.ExecuteNonQuery()
                                con.Close()
                            End Using
    
    
    
    
                        End If
    
    
                    End If
    
    
                End Using
            End Using
        End Sub
    
        Public Class BID
    
            Public Property BiddingPrice As Integer
                Get
                    Return _BiddingPrice
    
                End Get
    
                Set(ByVal value As Integer)
                    _BiddingPrice = value
    
                End Set
            End Property
            Private _BiddingPrice As String
    
        End Class
    
        Protected Sub timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles timer1.Tick
            For Each item As RepeaterItem In Repeater1.Items
                Using con As New SqlConnection(_start)
                    Using cmd2 As New SqlCommand("Select Auction.EndDate FROM Item INNER JOIN Auction ON Item.ItemID = Auction.ItemID Where Auction.Status='Valid' ")
                        cmd2.CommandType = CommandType.Text
    
                        con.Open()
                        cmd2.Connection = con
                        EndDate = Convert.ToDateTime(cmd2.ExecuteScalar)
    
                    End Using
    
                End Using
    
    
    
                If Not SM1.IsInAsyncPostBack Then
                    Dim timeout As String = Convert.ToString(EndDate)
                    Session("timeout") = timeout
                End If
    
    
                Dim label2 = TryCast(item.FindControl("lblTimer"), Label)
                If 0 > DateTime.Compare(DateTime.Now, DateTime.Parse(Session("timeout").ToString())) Then
                    label2.Text = String.Format("Time Left: 00:{0}:{1}",
                    Convert.ToInt32(DateTime.Parse(Session("timeout").ToString()).Subtract(DateTime.Now).TotalMinutes).ToString(), Convert.ToInt32(DateTime.Parse(Session("timeout").ToString()).Subtract(DateTime.Now).Seconds).ToString())
                End If
            Next
    
        End Sub
    End Class
    
    



    Friday, January 10, 2014 11:00 AM

Answers

  • User1734617369 posted

    Hi,

    I have made a simple example to show a conceptual way on how I would do it, maybe you can find a valid approach to use in your solution. The page:

    <%@ Page Language="vb" AutoEventWireup="false" CodeBehind="WebForm1.aspx.vb" Inherits="WebApplication10.WebForm1" %>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
            <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
            <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                <ContentTemplate>
                    <asp:Repeater runat="server" ID="Auctions">
                        <ItemTemplate>
                            <asp:Label ID="AuctionID" runat="server" Text='<%#Eval("Id")%>' />
                            Ends at: <asp:Label ID="lblTimer" runat="server" Text='<%#Eval("EndTime")%>'></asp:Label>
                            <br />
                        </ItemTemplate>
                    </asp:Repeater>
                    <asp:Timer Interval="1000" runat="server" ID="timer1" OnTick="timer1_Tick" Enabled="true"></asp:Timer>
                </ContentTemplate>
            </asp:UpdatePanel>
        </form>
    </body>
    </html>

    and the code:

    Public Class WebForm1
        Inherits System.Web.UI.Page
    
    
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            If Not IsPostBack Then
                Dim list As New List(Of Auction)()
    
                For index = 1 To 5
                    list.Add(New Auction With {.Id = index, .EndTime = Date.Now.AddSeconds(5 * index)})
                Next
    
                Auctions.DataSource = list
                Auctions.DataBind()
            End If
        End Sub
    
        Protected Sub timer1_Tick(sender As Object, e As EventArgs)
            For Each Item As RepeaterItem In Auctions.Items
                Dim lbl As Label = DirectCast(Item.FindControl("lblTimer"), Label)
                Dim time As Date
                If Not lbl.Text.Equals("Closed") Then
                    time = Date.Parse(lbl.Text)
                    If time <= Date.Now Then
                        lbl.Text = "Closed"
    
                        Dim ItemId As String = DirectCast(Item.FindControl("AuctionID"), Label).Text
                        DoSomeStuff()
                    End If
                End If
            Next
        End Sub
    
        Private Sub DoSomeStuff()
    
        End Sub
    
    End Class
    
    Public Class Auction
        Public Property Id As Integer
        Public Property EndTime As Date
    End Class

    There is no error checking and such, just a concept approach.

    Best regards
    Johan

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, January 10, 2014 3:10 PM