Datalist with Edit template and custom paging

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • 1333
    New Member
    • Feb 2007
    • 8

    Datalist with Edit template and custom paging

    Is it possible to have a Datalist with an Edit mode (<EditItemTempl ate />) and custom paging?

    I can get <EditItemTempla te> mode and custom paging to work seperately but not together. I can delete a row from the datalist, but once I go into edit mode the UpdateCommand fails. Is this possible because I am going around in circles with postback or not Postback?
  • kenobewan
    Recognized Expert Specialist
    • Dec 2006
    • 4871

    #2
    What error do get? Please post some relevant code.

    Comment

    • 1333
      New Member
      • Feb 2007
      • 8

      #3
      I know the Protected Sub AllPendingImage s_UpdateCommand works because when I remove the PAGING code the code fires and the DB gets updated.
      When I re-add the paging code and enter edit mode on my page, I think that the id value gets changed to _Page (don't know why) and the update command doesn't fire because the value is not an integer. I have wrapped the code in a Try/Catch but that doesn't throw up any errors - so I'm stuck. Any ideas would be really appreciated, I'm on day 4 looking at it. Thinking about redoing it in a GridView but I prefer the flexibility with the html in a datalist. txs

      my aspx page:

      Code:
      <asp:button id="cmdFirst" runat="server" text="&laquo; First " onclick="cmdFirst_Click" CssClass="button"></asp:button>
      <asp:button id="cmdPrev" runat="server" text="&laquo; Back " onclick="cmdPrev_Click" CssClass="button"></asp:button>
      <asp:button id="cmdNext" runat="server" text=" Next &raquo; " onclick="cmdNext_Click" CssClass="button"></asp:button>
      <asp:button id="cmdLast" runat="server" text=" Last &raquo; " onclick="cmdLast_Click" CssClass="button"></asp:button>
      
      <asp:DataList DataKeyField="id" ID="AllPendingImages" runat="server" RepeatColumns="1" RepeatDirection="Horizontal" EnableViewState="false">
      <ItemTemplate>
      ...
      </ItemTemplate>
      <EditItemTemplate>
      ...
      </EditItemTemplate>
      </asp:DataList>
      Code Behind:

      Code:
          Protected WithEvents DDPending As System.Web.UI.WebControls.DropDownList
          Public Property CurrentPage() As Integer
              Get
                  Dim o As Object = ViewState("_CurrentPage")
                  If (o = Nothing) Then
                      Return 0
                  End If
                  Return CType(o, Integer)
              End Get
              Set(ByVal value As Integer)
                  ViewState("_CurrentPage") = value
              End Set
          End Property
      
          Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
              ItemsGet()
          End Sub
      
          Protected Sub ItemsGet()
              Dim ConnStr As String = System.Configuration.ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString
              Dim mySqlConn As New SqlClient.SqlConnection(ConnStr)
              Dim objDA As New SqlDataAdapter("exec sp_GetPendingImages", ConnStr)
              Dim objDS As New DataSet()
              objDA.Fill(objDS)
              mySqlConn.Close()
      
              Dim objPds As PagedDataSource = New PagedDataSource
              objPds.DataSource = objDS.Tables(0).DefaultView
              objPds.AllowPaging = True
              objPds.PageSize = 3
      
              objPds.CurrentPageIndex = CurrentPage
      
              lblCount.Text = ("no of items per page:" + (objPds.Count).ToString + "<br /><br />")
              lblCurrentPageIndex.Text = ("page number:" + (objPds.CurrentPageIndex).ToString + "<br /><br />")
              lblPageCount.Text = ("number of pages:" + (objPds.PageCount).ToString + "<br /><br />")
              lblCurrentPage.Text = ("Page: " + ((CurrentPage + 1).ToString + (" of " + objPds.PageCount.ToString)))
      
              cmdPrev.Enabled = Not objPds.IsFirstPage
              cmdNext.Enabled = Not objPds.IsLastPage
              cmdFirst.Enabled = Not objPds.IsFirstPage
              cmdLast.Enabled = Not objPds.IsLastPage
      
              AllPendingImages.DataSource = objPds
            AllPendingImages.DataBind()
          End Sub
      
          Protected Sub cmdPrev_Click(ByVal sender As Object, ByVal e As System.EventArgs)
              CurrentPage = (CurrentPage - 1)
              ItemsGet()
          End Sub
      
          Protected Sub cmdNext_Click(ByVal sender As Object, ByVal e As System.EventArgs)
               CurrentPage = (CurrentPage + 1)
              ItemsGet()
          End Sub
      
          Protected Sub cmdFirst_Click(ByVal sender As Object, ByVal e As System.EventArgs)
               CurrentPage = 0
              ItemsGet()
          End Sub
      
          Protected Sub cmdLast_Click(ByVal sender As Object, ByVal e As System.EventArgs)
              CurrentPage = (CurrentPage + 2)
              ItemsGet()
          End Sub
      
          Protected Sub AllPendingImages_EditCommand(ByVal source As Object, ByVal e As DataListCommandEventArgs) Handles AllPendingImages.EditCommand
              AllPendingImages.EditItemIndex = e.Item.ItemIndex
              ItemsGet()
          End Sub
      
          Protected Sub AllPendingImages_CancelCommand(ByVal source As Object, ByVal e As DataListCommandEventArgs) Handles AllPendingImages.CancelCommand
              AllPendingImages.EditItemIndex = -1
               ItemsGet()
          End Sub
      
          Protected Sub AllPendingImages_DeleteCommand(ByVal source As Object, ByVal e As DataListCommandEventArgs) Handles AllPendingImages.DeleteCommand
      If Page.IsPostBack Then
              Dim id As Integer = Convert.ToInt32(AllPendingImages.DataKeys(e.Item.ItemIndex))
               Dim ConnStr As String = System.Configuration.ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString
              Dim mySqlConn As New SqlClient.SqlConnection(ConnStr)
              mySqlConn.Open()
              Dim SQL As String = "sp_DeleteImageRow"
              Dim mySqlCmd As New SqlClient.SqlCommand(SQL, mySqlConn)
              mySqlCmd.CommandType = CommandType.StoredProcedure
              Dim mySqlParamImageID As New SqlClient.SqlParameter("@ImageID", SqlDbType.Int)
              mySqlCmd.Parameters.Add(mySqlParamImageID).Value = id
              mySqlCmd.ExecuteNonQuery()
              mySqlConn.Close()
              AllPendingImages.EditItemIndex = -1
               ItemsGet()
      End if
          End Sub
      
          Protected Sub AllPendingImages_UpdateCommand(ByVal source As Object, ByVal e As DataListCommandEventArgs) Handles AllPendingImages.UpdateCommand  
       	'If Page.IsPostBack Then
      	Dim id As Integer = Convert.ToInt32(AllPendingImages.DataKeys(e.Item.ItemIndex))
              
      	Dim DDLPending As DropDownList = CType(e.Item.FindControl("DDLPending"), DropDownList)
              Dim Pending As String
              Pending = DDLPending.SelectedValue
      
              Select Case DDLPending.SelectedIndex 'Expression
                  Case 0 
                      Pending = 1  'Decline
                  Case 1
                      Pending = 2  'Accept
              End Select
      
              Dim PendingValue As String = Nothing
              PendingValue = Pending
      
             
              Dim ConnStr As String = System.Configuration.ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString
              Dim mySqlConn As New SqlClient.SqlConnection(ConnStr)
              mySqlConn.Open()
              Dim SQL As String = "sp_UpdatePending"
              Dim mySqlCmd As New SqlClient.SqlCommand(SQL, mySqlConn)
              mySqlCmd.CommandType = CommandType.StoredProcedure
      
              Dim mySqlParamPending As New SqlClient.SqlParameter("@Pending", SqlDbType.Int)
              Dim mySqlParamImageID As New SqlClient.SqlParameter("@ImageID", SqlDbType.Int)
      
              mySqlCmd.Parameters.Add(mySqlParamPending).Value = PendingValue
              mySqlCmd.Parameters.Add(mySqlParamImageID).Value = id
              mySqlCmd.ExecuteNonQuery()
              mySqlConn.Close()
      
              AllPendingImages.EditItemIndex = -1
               ItemsGet()
      'End if
          End Sub

      Comment

      • kenobewan
        Recognized Expert Specialist
        • Dec 2006
        • 4871

        #4
        I recall a similar problem I had a year ago. I believe that I copied the paging code into the update command and it worked. So I believe I settled on having a separate sub for paging and referencing that as necessary.

        Hope that this helps.

        Comment

        • archu007
          New Member
          • May 2007
          • 28

          #5
          Hi
          Thank u ..
          I was searching for datalist paging,i tried ur code its working and solved my problem .
          I have tried only paging not the "allpendingimag es" code
          if u get the solution plz forward to me also
          my id is

          chotu_91@rediff mail.com

          Thank u
          Archu





          Originally posted by 1333
          I know the Protected Sub AllPendingImage s_UpdateCommand works because when I remove the PAGING code the code fires and the DB gets updated.
          When I re-add the paging code and enter edit mode on my page, I think that the id value gets changed to _Page (don't know why) and the update command doesn't fire because the value is not an integer. I have wrapped the code in a Try/Catch but that doesn't throw up any errors - so I'm stuck. Any ideas would be really appreciated, I'm on day 4 looking at it. Thinking about redoing it in a GridView but I prefer the flexibility with the html in a datalist. txs

          my aspx page:

          Code:
          <asp:button id="cmdFirst" runat="server" text="&laquo; First " onclick="cmdFirst_Click" CssClass="button"></asp:button>
          <asp:button id="cmdPrev" runat="server" text="&laquo; Back " onclick="cmdPrev_Click" CssClass="button"></asp:button>
          <asp:button id="cmdNext" runat="server" text=" Next &raquo; " onclick="cmdNext_Click" CssClass="button"></asp:button>
          <asp:button id="cmdLast" runat="server" text=" Last &raquo; " onclick="cmdLast_Click" CssClass="button"></asp:button>
          
          <asp:DataList DataKeyField="id" ID="AllPendingImages" runat="server" RepeatColumns="1" RepeatDirection="Horizontal" EnableViewState="false">
          <ItemTemplate>
          ...
          </ItemTemplate>
          <EditItemTemplate>
          ...
          </EditItemTemplate>
          </asp:DataList>
          Code Behind:

          Code:
              Protected WithEvents DDPending As System.Web.UI.WebControls.DropDownList
              Public Property CurrentPage() As Integer
                  Get
                      Dim o As Object = ViewState("_CurrentPage")
                      If (o = Nothing) Then
                          Return 0
                      End If
                      Return CType(o, Integer)
                  End Get
                  Set(ByVal value As Integer)
                      ViewState("_CurrentPage") = value
                  End Set
              End Property
          
              Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
                  ItemsGet()
              End Sub
          
              Protected Sub ItemsGet()
                  Dim ConnStr As String = System.Configuration.ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString
                  Dim mySqlConn As New SqlClient.SqlConnection(ConnStr)
                  Dim objDA As New SqlDataAdapter("exec sp_GetPendingImages", ConnStr)
                  Dim objDS As New DataSet()
                  objDA.Fill(objDS)
                  mySqlConn.Close()
          
                  Dim objPds As PagedDataSource = New PagedDataSource
                  objPds.DataSource = objDS.Tables(0).DefaultView
                  objPds.AllowPaging = True
                  objPds.PageSize = 3
          
                  objPds.CurrentPageIndex = CurrentPage
          
                  lblCount.Text = ("no of items per page:" + (objPds.Count).ToString + "<br /><br />")
                  lblCurrentPageIndex.Text = ("page number:" + (objPds.CurrentPageIndex).ToString + "<br /><br />")
                  lblPageCount.Text = ("number of pages:" + (objPds.PageCount).ToString + "<br /><br />")
                  lblCurrentPage.Text = ("Page: " + ((CurrentPage + 1).ToString + (" of " + objPds.PageCount.ToString)))
          
                  cmdPrev.Enabled = Not objPds.IsFirstPage
                  cmdNext.Enabled = Not objPds.IsLastPage
                  cmdFirst.Enabled = Not objPds.IsFirstPage
                  cmdLast.Enabled = Not objPds.IsLastPage
          
                  AllPendingImages.DataSource = objPds
                AllPendingImages.DataBind()
              End Sub
          
              Protected Sub cmdPrev_Click(ByVal sender As Object, ByVal e As System.EventArgs)
                  CurrentPage = (CurrentPage - 1)
                  ItemsGet()
              End Sub
          
              Protected Sub cmdNext_Click(ByVal sender As Object, ByVal e As System.EventArgs)
                   CurrentPage = (CurrentPage + 1)
                  ItemsGet()
              End Sub
          
              Protected Sub cmdFirst_Click(ByVal sender As Object, ByVal e As System.EventArgs)
                   CurrentPage = 0
                  ItemsGet()
              End Sub
          
              Protected Sub cmdLast_Click(ByVal sender As Object, ByVal e As System.EventArgs)
                  CurrentPage = (CurrentPage + 2)
                  ItemsGet()
              End Sub
          
              Protected Sub AllPendingImages_EditCommand(ByVal source As Object, ByVal e As DataListCommandEventArgs) Handles AllPendingImages.EditCommand
                  AllPendingImages.EditItemIndex = e.Item.ItemIndex
                  ItemsGet()
              End Sub
          
              Protected Sub AllPendingImages_CancelCommand(ByVal source As Object, ByVal e As DataListCommandEventArgs) Handles AllPendingImages.CancelCommand
                  AllPendingImages.EditItemIndex = -1
                   ItemsGet()
              End Sub
          
              Protected Sub AllPendingImages_DeleteCommand(ByVal source As Object, ByVal e As DataListCommandEventArgs) Handles AllPendingImages.DeleteCommand
          If Page.IsPostBack Then
                  Dim id As Integer = Convert.ToInt32(AllPendingImages.DataKeys(e.Item.ItemIndex))
                   Dim ConnStr As String = System.Configuration.ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString
                  Dim mySqlConn As New SqlClient.SqlConnection(ConnStr)
                  mySqlConn.Open()
                  Dim SQL As String = "sp_DeleteImageRow"
                  Dim mySqlCmd As New SqlClient.SqlCommand(SQL, mySqlConn)
                  mySqlCmd.CommandType = CommandType.StoredProcedure
                  Dim mySqlParamImageID As New SqlClient.SqlParameter("@ImageID", SqlDbType.Int)
                  mySqlCmd.Parameters.Add(mySqlParamImageID).Value = id
                  mySqlCmd.ExecuteNonQuery()
                  mySqlConn.Close()
                  AllPendingImages.EditItemIndex = -1
                   ItemsGet()
          End if
              End Sub
          
              Protected Sub AllPendingImages_UpdateCommand(ByVal source As Object, ByVal e As DataListCommandEventArgs) Handles AllPendingImages.UpdateCommand  
           	'If Page.IsPostBack Then
          	Dim id As Integer = Convert.ToInt32(AllPendingImages.DataKeys(e.Item.ItemIndex))
                  
          	Dim DDLPending As DropDownList = CType(e.Item.FindControl("DDLPending"), DropDownList)
                  Dim Pending As String
                  Pending = DDLPending.SelectedValue
          
                  Select Case DDLPending.SelectedIndex 'Expression
                      Case 0 
                          Pending = 1  'Decline
                      Case 1
                          Pending = 2  'Accept
                  End Select
          
                  Dim PendingValue As String = Nothing
                  PendingValue = Pending
          
                 
                  Dim ConnStr As String = System.Configuration.ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString
                  Dim mySqlConn As New SqlClient.SqlConnection(ConnStr)
                  mySqlConn.Open()
                  Dim SQL As String = "sp_UpdatePending"
                  Dim mySqlCmd As New SqlClient.SqlCommand(SQL, mySqlConn)
                  mySqlCmd.CommandType = CommandType.StoredProcedure
          
                  Dim mySqlParamPending As New SqlClient.SqlParameter("@Pending", SqlDbType.Int)
                  Dim mySqlParamImageID As New SqlClient.SqlParameter("@ImageID", SqlDbType.Int)
          
                  mySqlCmd.Parameters.Add(mySqlParamPending).Value = PendingValue
                  mySqlCmd.Parameters.Add(mySqlParamImageID).Value = id
                  mySqlCmd.ExecuteNonQuery()
                  mySqlConn.Close()
          
                  AllPendingImages.EditItemIndex = -1
                   ItemsGet()
          'End if
              End Sub

          Comment

          Working...