469,364 Members | 2,398 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,364 developers. It's quick & easy.

Datalist with Edit template and custom paging

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

I can get <EditItemTemplate> 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?
Feb 28 '07 #1
4 2183
kenobewan
4,871 Expert 4TB
What error do get? Please post some relevant code.
Mar 1 '07 #2
1333
8
I know the Protected Sub AllPendingImages_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:

Expand|Select|Wrap|Line Numbers
  1. <asp:button id="cmdFirst" runat="server" text="&laquo; First " onclick="cmdFirst_Click" CssClass="button"></asp:button>
  2. <asp:button id="cmdPrev" runat="server" text="&laquo; Back " onclick="cmdPrev_Click" CssClass="button"></asp:button>
  3. <asp:button id="cmdNext" runat="server" text=" Next &raquo; " onclick="cmdNext_Click" CssClass="button"></asp:button>
  4. <asp:button id="cmdLast" runat="server" text=" Last &raquo; " onclick="cmdLast_Click" CssClass="button"></asp:button>
  5.  
  6. <asp:DataList DataKeyField="id" ID="AllPendingImages" runat="server" RepeatColumns="1" RepeatDirection="Horizontal" EnableViewState="false">
  7. <ItemTemplate>
  8. ...
  9. </ItemTemplate>
  10. <EditItemTemplate>
  11. ...
  12. </EditItemTemplate>
  13. </asp:DataList>
Code Behind:

Expand|Select|Wrap|Line Numbers
  1.     Protected WithEvents DDPending As System.Web.UI.WebControls.DropDownList
  2.     Public Property CurrentPage() As Integer
  3.         Get
  4.             Dim o As Object = ViewState("_CurrentPage")
  5.             If (o = Nothing) Then
  6.                 Return 0
  7.             End If
  8.             Return CType(o, Integer)
  9.         End Get
  10.         Set(ByVal value As Integer)
  11.             ViewState("_CurrentPage") = value
  12.         End Set
  13.     End Property
  14.  
  15.     Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  16.         ItemsGet()
  17.     End Sub
  18.  
  19.     Protected Sub ItemsGet()
  20.         Dim ConnStr As String = System.Configuration.ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString
  21.         Dim mySqlConn As New SqlClient.SqlConnection(ConnStr)
  22.         Dim objDA As New SqlDataAdapter("exec sp_GetPendingImages", ConnStr)
  23.         Dim objDS As New DataSet()
  24.         objDA.Fill(objDS)
  25.         mySqlConn.Close()
  26.  
  27.         Dim objPds As PagedDataSource = New PagedDataSource
  28.         objPds.DataSource = objDS.Tables(0).DefaultView
  29.         objPds.AllowPaging = True
  30.         objPds.PageSize = 3
  31.  
  32.         objPds.CurrentPageIndex = CurrentPage
  33.  
  34.         lblCount.Text = ("no of items per page:" + (objPds.Count).ToString + "<br /><br />")
  35.         lblCurrentPageIndex.Text = ("page number:" + (objPds.CurrentPageIndex).ToString + "<br /><br />")
  36.         lblPageCount.Text = ("number of pages:" + (objPds.PageCount).ToString + "<br /><br />")
  37.         lblCurrentPage.Text = ("Page: " + ((CurrentPage + 1).ToString + (" of " + objPds.PageCount.ToString)))
  38.  
  39.         cmdPrev.Enabled = Not objPds.IsFirstPage
  40.         cmdNext.Enabled = Not objPds.IsLastPage
  41.         cmdFirst.Enabled = Not objPds.IsFirstPage
  42.         cmdLast.Enabled = Not objPds.IsLastPage
  43.  
  44.         AllPendingImages.DataSource = objPds
  45.       AllPendingImages.DataBind()
  46.     End Sub
  47.  
  48.     Protected Sub cmdPrev_Click(ByVal sender As Object, ByVal e As System.EventArgs)
  49.         CurrentPage = (CurrentPage - 1)
  50.         ItemsGet()
  51.     End Sub
  52.  
  53.     Protected Sub cmdNext_Click(ByVal sender As Object, ByVal e As System.EventArgs)
  54.          CurrentPage = (CurrentPage + 1)
  55.         ItemsGet()
  56.     End Sub
  57.  
  58.     Protected Sub cmdFirst_Click(ByVal sender As Object, ByVal e As System.EventArgs)
  59.          CurrentPage = 0
  60.         ItemsGet()
  61.     End Sub
  62.  
  63.     Protected Sub cmdLast_Click(ByVal sender As Object, ByVal e As System.EventArgs)
  64.         CurrentPage = (CurrentPage + 2)
  65.         ItemsGet()
  66.     End Sub
  67.  
  68.     Protected Sub AllPendingImages_EditCommand(ByVal source As Object, ByVal e As DataListCommandEventArgs) Handles AllPendingImages.EditCommand
  69.         AllPendingImages.EditItemIndex = e.Item.ItemIndex
  70.         ItemsGet()
  71.     End Sub
  72.  
  73.     Protected Sub AllPendingImages_CancelCommand(ByVal source As Object, ByVal e As DataListCommandEventArgs) Handles AllPendingImages.CancelCommand
  74.         AllPendingImages.EditItemIndex = -1
  75.          ItemsGet()
  76.     End Sub
  77.  
  78.     Protected Sub AllPendingImages_DeleteCommand(ByVal source As Object, ByVal e As DataListCommandEventArgs) Handles AllPendingImages.DeleteCommand
  79. If Page.IsPostBack Then
  80.         Dim id As Integer = Convert.ToInt32(AllPendingImages.DataKeys(e.Item.ItemIndex))
  81.          Dim ConnStr As String = System.Configuration.ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString
  82.         Dim mySqlConn As New SqlClient.SqlConnection(ConnStr)
  83.         mySqlConn.Open()
  84.         Dim SQL As String = "sp_DeleteImageRow"
  85.         Dim mySqlCmd As New SqlClient.SqlCommand(SQL, mySqlConn)
  86.         mySqlCmd.CommandType = CommandType.StoredProcedure
  87.         Dim mySqlParamImageID As New SqlClient.SqlParameter("@ImageID", SqlDbType.Int)
  88.         mySqlCmd.Parameters.Add(mySqlParamImageID).Value = id
  89.         mySqlCmd.ExecuteNonQuery()
  90.         mySqlConn.Close()
  91.         AllPendingImages.EditItemIndex = -1
  92.          ItemsGet()
  93. End if
  94.     End Sub
  95.  
  96.     Protected Sub AllPendingImages_UpdateCommand(ByVal source As Object, ByVal e As DataListCommandEventArgs) Handles AllPendingImages.UpdateCommand  
  97.      'If Page.IsPostBack Then
  98.     Dim id As Integer = Convert.ToInt32(AllPendingImages.DataKeys(e.Item.ItemIndex))
  99.  
  100.     Dim DDLPending As DropDownList = CType(e.Item.FindControl("DDLPending"), DropDownList)
  101.         Dim Pending As String
  102.         Pending = DDLPending.SelectedValue
  103.  
  104.         Select Case DDLPending.SelectedIndex 'Expression
  105.             Case 0 
  106.                 Pending = 1  'Decline
  107.             Case 1
  108.                 Pending = 2  'Accept
  109.         End Select
  110.  
  111.         Dim PendingValue As String = Nothing
  112.         PendingValue = Pending
  113.  
  114.  
  115.         Dim ConnStr As String = System.Configuration.ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString
  116.         Dim mySqlConn As New SqlClient.SqlConnection(ConnStr)
  117.         mySqlConn.Open()
  118.         Dim SQL As String = "sp_UpdatePending"
  119.         Dim mySqlCmd As New SqlClient.SqlCommand(SQL, mySqlConn)
  120.         mySqlCmd.CommandType = CommandType.StoredProcedure
  121.  
  122.         Dim mySqlParamPending As New SqlClient.SqlParameter("@Pending", SqlDbType.Int)
  123.         Dim mySqlParamImageID As New SqlClient.SqlParameter("@ImageID", SqlDbType.Int)
  124.  
  125.         mySqlCmd.Parameters.Add(mySqlParamPending).Value = PendingValue
  126.         mySqlCmd.Parameters.Add(mySqlParamImageID).Value = id
  127.         mySqlCmd.ExecuteNonQuery()
  128.         mySqlConn.Close()
  129.  
  130.         AllPendingImages.EditItemIndex = -1
  131.          ItemsGet()
  132. 'End if
  133.     End Sub
  134.  
  135.  
  136.  
Mar 2 '07 #3
kenobewan
4,871 Expert 4TB
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.
Mar 4 '07 #4
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 "allpendingimages" code
if u get the solution plz forward to me also
my id is

chotu_91@rediffmail.com

Thank u
Archu





I know the Protected Sub AllPendingImages_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:

Expand|Select|Wrap|Line Numbers
  1. <asp:button id="cmdFirst" runat="server" text="&laquo; First " onclick="cmdFirst_Click" CssClass="button"></asp:button>
  2. <asp:button id="cmdPrev" runat="server" text="&laquo; Back " onclick="cmdPrev_Click" CssClass="button"></asp:button>
  3. <asp:button id="cmdNext" runat="server" text=" Next &raquo; " onclick="cmdNext_Click" CssClass="button"></asp:button>
  4. <asp:button id="cmdLast" runat="server" text=" Last &raquo; " onclick="cmdLast_Click" CssClass="button"></asp:button>
  5.  
  6. <asp:DataList DataKeyField="id" ID="AllPendingImages" runat="server" RepeatColumns="1" RepeatDirection="Horizontal" EnableViewState="false">
  7. <ItemTemplate>
  8. ...
  9. </ItemTemplate>
  10. <EditItemTemplate>
  11. ...
  12. </EditItemTemplate>
  13. </asp:DataList>
Code Behind:

Expand|Select|Wrap|Line Numbers
  1.     Protected WithEvents DDPending As System.Web.UI.WebControls.DropDownList
  2.     Public Property CurrentPage() As Integer
  3.         Get
  4.             Dim o As Object = ViewState("_CurrentPage")
  5.             If (o = Nothing) Then
  6.                 Return 0
  7.             End If
  8.             Return CType(o, Integer)
  9.         End Get
  10.         Set(ByVal value As Integer)
  11.             ViewState("_CurrentPage") = value
  12.         End Set
  13.     End Property
  14.  
  15.     Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  16.         ItemsGet()
  17.     End Sub
  18.  
  19.     Protected Sub ItemsGet()
  20.         Dim ConnStr As String = System.Configuration.ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString
  21.         Dim mySqlConn As New SqlClient.SqlConnection(ConnStr)
  22.         Dim objDA As New SqlDataAdapter("exec sp_GetPendingImages", ConnStr)
  23.         Dim objDS As New DataSet()
  24.         objDA.Fill(objDS)
  25.         mySqlConn.Close()
  26.  
  27.         Dim objPds As PagedDataSource = New PagedDataSource
  28.         objPds.DataSource = objDS.Tables(0).DefaultView
  29.         objPds.AllowPaging = True
  30.         objPds.PageSize = 3
  31.  
  32.         objPds.CurrentPageIndex = CurrentPage
  33.  
  34.         lblCount.Text = ("no of items per page:" + (objPds.Count).ToString + "<br /><br />")
  35.         lblCurrentPageIndex.Text = ("page number:" + (objPds.CurrentPageIndex).ToString + "<br /><br />")
  36.         lblPageCount.Text = ("number of pages:" + (objPds.PageCount).ToString + "<br /><br />")
  37.         lblCurrentPage.Text = ("Page: " + ((CurrentPage + 1).ToString + (" of " + objPds.PageCount.ToString)))
  38.  
  39.         cmdPrev.Enabled = Not objPds.IsFirstPage
  40.         cmdNext.Enabled = Not objPds.IsLastPage
  41.         cmdFirst.Enabled = Not objPds.IsFirstPage
  42.         cmdLast.Enabled = Not objPds.IsLastPage
  43.  
  44.         AllPendingImages.DataSource = objPds
  45.       AllPendingImages.DataBind()
  46.     End Sub
  47.  
  48.     Protected Sub cmdPrev_Click(ByVal sender As Object, ByVal e As System.EventArgs)
  49.         CurrentPage = (CurrentPage - 1)
  50.         ItemsGet()
  51.     End Sub
  52.  
  53.     Protected Sub cmdNext_Click(ByVal sender As Object, ByVal e As System.EventArgs)
  54.          CurrentPage = (CurrentPage + 1)
  55.         ItemsGet()
  56.     End Sub
  57.  
  58.     Protected Sub cmdFirst_Click(ByVal sender As Object, ByVal e As System.EventArgs)
  59.          CurrentPage = 0
  60.         ItemsGet()
  61.     End Sub
  62.  
  63.     Protected Sub cmdLast_Click(ByVal sender As Object, ByVal e As System.EventArgs)
  64.         CurrentPage = (CurrentPage + 2)
  65.         ItemsGet()
  66.     End Sub
  67.  
  68.     Protected Sub AllPendingImages_EditCommand(ByVal source As Object, ByVal e As DataListCommandEventArgs) Handles AllPendingImages.EditCommand
  69.         AllPendingImages.EditItemIndex = e.Item.ItemIndex
  70.         ItemsGet()
  71.     End Sub
  72.  
  73.     Protected Sub AllPendingImages_CancelCommand(ByVal source As Object, ByVal e As DataListCommandEventArgs) Handles AllPendingImages.CancelCommand
  74.         AllPendingImages.EditItemIndex = -1
  75.          ItemsGet()
  76.     End Sub
  77.  
  78.     Protected Sub AllPendingImages_DeleteCommand(ByVal source As Object, ByVal e As DataListCommandEventArgs) Handles AllPendingImages.DeleteCommand
  79. If Page.IsPostBack Then
  80.         Dim id As Integer = Convert.ToInt32(AllPendingImages.DataKeys(e.Item.ItemIndex))
  81.          Dim ConnStr As String = System.Configuration.ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString
  82.         Dim mySqlConn As New SqlClient.SqlConnection(ConnStr)
  83.         mySqlConn.Open()
  84.         Dim SQL As String = "sp_DeleteImageRow"
  85.         Dim mySqlCmd As New SqlClient.SqlCommand(SQL, mySqlConn)
  86.         mySqlCmd.CommandType = CommandType.StoredProcedure
  87.         Dim mySqlParamImageID As New SqlClient.SqlParameter("@ImageID", SqlDbType.Int)
  88.         mySqlCmd.Parameters.Add(mySqlParamImageID).Value = id
  89.         mySqlCmd.ExecuteNonQuery()
  90.         mySqlConn.Close()
  91.         AllPendingImages.EditItemIndex = -1
  92.          ItemsGet()
  93. End if
  94.     End Sub
  95.  
  96.     Protected Sub AllPendingImages_UpdateCommand(ByVal source As Object, ByVal e As DataListCommandEventArgs) Handles AllPendingImages.UpdateCommand  
  97.      'If Page.IsPostBack Then
  98.     Dim id As Integer = Convert.ToInt32(AllPendingImages.DataKeys(e.Item.ItemIndex))
  99.  
  100.     Dim DDLPending As DropDownList = CType(e.Item.FindControl("DDLPending"), DropDownList)
  101.         Dim Pending As String
  102.         Pending = DDLPending.SelectedValue
  103.  
  104.         Select Case DDLPending.SelectedIndex 'Expression
  105.             Case 0 
  106.                 Pending = 1  'Decline
  107.             Case 1
  108.                 Pending = 2  'Accept
  109.         End Select
  110.  
  111.         Dim PendingValue As String = Nothing
  112.         PendingValue = Pending
  113.  
  114.  
  115.         Dim ConnStr As String = System.Configuration.ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString
  116.         Dim mySqlConn As New SqlClient.SqlConnection(ConnStr)
  117.         mySqlConn.Open()
  118.         Dim SQL As String = "sp_UpdatePending"
  119.         Dim mySqlCmd As New SqlClient.SqlCommand(SQL, mySqlConn)
  120.         mySqlCmd.CommandType = CommandType.StoredProcedure
  121.  
  122.         Dim mySqlParamPending As New SqlClient.SqlParameter("@Pending", SqlDbType.Int)
  123.         Dim mySqlParamImageID As New SqlClient.SqlParameter("@ImageID", SqlDbType.Int)
  124.  
  125.         mySqlCmd.Parameters.Add(mySqlParamPending).Value = PendingValue
  126.         mySqlCmd.Parameters.Add(mySqlParamImageID).Value = id
  127.         mySqlCmd.ExecuteNonQuery()
  128.         mySqlConn.Close()
  129.  
  130.         AllPendingImages.EditItemIndex = -1
  131.          ItemsGet()
  132. 'End if
  133.     End Sub
  134.  
  135.  
  136.  
Aug 17 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by Fabrizio | last post: by
3 posts views Thread by moondaddy | last post: by
3 posts views Thread by Clint | last post: by
3 posts views Thread by Mirek Endys | last post: by
1 post views Thread by Chris | last post: by
3 posts views Thread by Crazy Cat | last post: by
reply views Thread by zhoujie | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.