unruly DataList

Discussion in 'C#' started by doKtorhallam, Dec 11, 2007.

  1. #1
    to try and explain quickly, i have a datalist that cannot be populated (to my knowledge) with a SqlDataSource because i have it display a rendered image as well as a set of data from a SQL Server 05 db table. I have used VB code behind to dynamically create a dataset and sqldataadapter to populate the datalist. i then had to write item and edititem templates, and then i wrote code for the edit, cancel, and update command events for the datalist. they are working, but very "wierd-ish".
    i hit the edit button once, page postsback, still in item mode. hit edit 2nd time, page postsback this time in edit mode. i have to go through the same twice thru with the cancel command. if i enter new data and hit update, it postsback and still in edit mode. hit update 2nd time, postsback, item mode, no updated fields. close the app, reopen it, and the fields are now updated. can anybody help me on this one please? below is .aspx page code for the datalist as well as entire VB code behind page.
    thanks all.

    <asp:DataList ID="photoList" runat="server" DataKeyField="photoID" >
    <ItemTemplate>
    <br />
    <br />
    Photo ID:
    <asp:Label ID="lblPhotoID" runat="server" Text='<%# Bind("photoID") %>'></asp:Label>
    <br />
    Photo Order:
    <asp:Label ID="lblPhotoOrder" runat="server" Text='<%# Bind("photoOrder") %>'></asp:Label>
    <br />
    Photo Visible:
    <asp:CheckBox ID="cbxPhotoVisible1" runat="server" Checked='<%# Bind("photoVisible") %>'
    Enabled="false" />
    <br />
    Photo Thumbnail:
    <asp:CheckBox ID="cbxThumbnail1" runat="server" Checked='<%# Bind("photoThumbnail") %>'
    Enabled="false" />
    <br />
    Photo Comment:
    <asp:Label ID="lblComment" runat="server" Text='<%# Bind("photoComment") %>'></asp:Label>
    <br />
    <asp:Button ID="btnEdit" runat="server" CommandName="edit" Text="Edit" />
    &nbsp; &nbsp; &nbsp;
    <asp:Button ID="btnDelete" runat="server" CommandName="delete" Text="Delete" />
    <br />
    <asp:Image ID="imgPhoto" runat="server" ImageUrl='<%# Eval("photoFile") %>' Width="400px" />
    </ItemTemplate>
    <EditItemTemplate>
    <br />
    <br />
    Photo ID:
    <asp:Label ID="lblPhotoID" runat="server" Text='<%# Bind("photoID") %>'></asp:Label>
    <br />
    Photo Order:
    <asp:TextBox ID="txtPhotoOrder" runat="server" Text='<%# Bind("photoOrder") %>'></asp:TextBox>
    <br />
    Photo Visible:
    <asp:CheckBox ID="cbxPhotoVisible2" runat="server" Checked='<%# Bind("photoVisible") %>'
    Enabled="true" />
    <br />
    Photo Thumbnail:
    <asp:CheckBox ID="cbxPhotoThumbnail2" runat="server" Checked='<%# Bind("photoThumbnail") %>'
    Enabled="true" />
    <br />
    Photo Comment:
    <asp:TextBox ID="txtPhotoComment" runat="server" Text='<%# Bind("photoComment") %>'></asp:TextBox>
    <br />
    <asp:Button ID="btnUpdate" runat="server" CommandName="update" Text="Update" />
    &nbsp; &nbsp; &nbsp;
    <asp:Button ID="btnCancel" runat="server" CommandName="cancel" Text="Cancel" />
    <br />
    <asp:Image ID="imgPhoto" runat="server" ImageUrl='<%# Eval("photoFile") %>' Width="400px" />
    </EditItemTemplate>
    </asp:DataList>


    Partial Class AdminPages_AdminPhotos
    Inherits System.Web.UI.Page
    Protected Function ftnFillDataSet(ByVal ds As DataSet, ByVal strQuery As String) As DataSet
    Dim da As SqlClient.SqlDataAdapter
    Dim strConnection As New SqlConnection()
    strConnection.ConnectionString = ConfigurationManager.ConnectionStrings("CustomTintsConnectionString").ConnectionString.ToString()
    da = New SqlClient.SqlDataAdapter(strQuery, strConnection)
    da.Fill(ds)
    ds.Tables(0).Columns.Add("photoFile")
    For Each tempRow As DataRow In ds.Tables(0).Rows
    tempRow.Item("photoFile") = ("~/PublicPages/PhotoGalleries/photoGrab.aspx?id=" & tempRow.Item("photoID"))
    Next
    Return ds
    End Function
    Protected Sub btnViewPhotos_Command(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.CommandEventArgs)
    Dim intID As Integer = Val(e.CommandName)
    Dim dsPhoto As New DataSet
    Dim strSQLJob As String = "SELECT * FROM tblPhotos WHERE jobID=" & intID & "ORDER BY photoOrder"
    ftnFillDataSet(dsPhoto, strSQLJob)
    photoList.DataSource = dsPhoto
    photoList.DataBind()
    End Sub
    Protected Sub photoList_EditCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataListCommandEventArgs) Handles photoList.EditCommand
    photoList.EditItemIndex = e.Item.ItemIndex
    End Sub
    Protected Sub photoList_CancelCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataListCommandEventArgs) Handles photoList.CancelCommand
    photoList.EditItemIndex = -1
    End Sub
    Protected Sub photoList_UpdateCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataListCommandEventArgs) Handles photoList.UpdateCommand
    Dim strConnection As New SqlConnection()
    strConnection.ConnectionString = ConfigurationManager.ConnectionStrings("CustomTintsConnectionString").ConnectionString.ToString()
    strConnection.Open()
    Dim strSQLPhoto As New SqlCommand("UPDATE tblPhotos SET photoComment = @photoComment, photoOrder = @photoOrder, photoThumbnail = @photoThumbnail, photoVisible = @photoVisible WHERE photoID = @oldphotoID", strConnection)
    With strSQLPhoto
    .Parameters.AddWithValue("@oldphotoID", CType(e.Item.FindControl("lblPhotoID"), Label).Text)
    .Parameters.AddWithValue("@photoComment", CType(e.Item.FindControl("txtPhotoComment"), TextBox).Text)
    .Parameters.AddWithValue("@photoOrder", CType(e.Item.FindControl("txtPhotoOrder"), TextBox).Text)
    .Parameters.AddWithValue("@photoThumbnail", CType(e.Item.FindControl("cbxPhotoThumbnail2"), CheckBox).Checked)
    .Parameters.AddWithValue("@photoVisible", CType(e.Item.FindControl("cbxPhotoVisible2"), CheckBox).Checked)
    .ExecuteNonQuery()
    End With
    photoList.EditItemIndex = -1
    End Sub
    Protected Sub photoList_DeleteCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataListCommandEventArgs) Handles photoList.DeleteCommand
    'blah blah
    End Sub
    End Class

    obviously i have not done the deletecommand sub yet.... i also had photoList.dataBind() following reseting the ItemEditIndex statement in each command sub, but the datalist would not render on postback, so i took the databinds out.

    any help is much appreciated.
     
    doKtorhallam, Dec 11, 2007 IP
  2. AstarothSolutions

    AstarothSolutions Peon

    Messages:
    2,680
    Likes Received:
    77
    Best Answers:
    0
    Trophy Points:
    0
    #2
    By rendering an image I assume that you mean the <asp:Image ID="imgPhoto" runat="server" ImageUrl='<%# Eval("photoFile") %>' Width="400px" /> ?

    You can achieve this fine with a sqldatasource with:
    
    <asp:Image ID="imgPhoto" runat="server" ImageUrl='<%# "~/PublicPages/PhotoGalleries/photoGrab.aspx?id=" & Eval("photoFile") %>' Width="400px" />
    
    Code (.Net):
     
    AstarothSolutions, Dec 12, 2007 IP