2 datagrids on one page

P

Philip Rayne

I have 2 datagrids, both with databound columns that I have dynamically
added. I have an EditCommandColumn on both grids. EnableViewState is
enabled on both of the grids.

When I click the edit link on the grids I need to repopulate/rebind the
grids which I presume is correct.

When I click the update button I cannot find my databound columns that I
have edited. I have searched google and tried all the obvious examples, but
I am just not getting it. Is there perhaps a bug, or a limitation with
having 2 grids on one page. What exactly does EnableViewState do anyway if
it doesn't retain the data or already loaded in the grids? Here is my code
for populating one of the grids - the other is very similiar. If anyone has
any idea then please let me know as I am really stuck now...

Page Load....

dgPhotos.AutoGenerateColumns = False
If dgPhotos.Columns.Count = 5 Then

' Create ID column & add to DataGrid
Dim col1 As New BoundColumn
col1.HeaderText = "Description"
col1.DataField = "PhotoText"
dgPhotos.Columns.Add(col1)
col1 = Nothing

Dim col2 As New BoundColumn
col2.HeaderText = "Filename"
col2.DataField = "Filename"
col2.ReadOnly = True
dgPhotos.Columns.Add(col2)
col2 = Nothing

Dim col3 As New BoundColumn
col3.HeaderText = "Order"
col3.DataField = "OrderPos"
col3.ReadOnly = True
col3.Visible = False
dgPhotos.Columns.Add(col3)
col3 = Nothing
End If

dsPhotos.ReadXml(filePath)

dgPhotos.Visible = False
If dsPhotos.Tables.Count > 0 Then

dgPhotos.Visible = True

Dim lngID As Long
lngID = GetAlbumID(dsPhotos, strRoleID)

dvPhotos.Table = dsPhotos.Tables(1)
dvPhotos.RowFilter = "AlbumRoleID_Id = '" & lngID & "'"
dvPhotos.Sort = "OrderPos"

With dgPhotos
.DataSource = dvPhotos
.DataMember = "PhotoItem"
.DataBind()
End With
End If

Editing Code:

Private Sub dgPhotos_EditCommand(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataGridCommandEventArgs) Handles
dgPhotos.EditCommand
dgPhotos.EditItemIndex = e.Item.ItemIndex

Dim strRoleID As String
PopulateGrid() 'Rebinds also

End Sub

Update Code so far :

Private Sub dgPhotos_UpdateCommand(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataGridCommandEventArgs) Handles
dgPhotos.UpdateCommand

Dim txtDescription As New TextBox
txtDescription = CType(e.Item.Cells(1).FindControl("PhotoText"),
TextBox)


End Sub

aspx...
<asp:datagrid id="dgPhotos" runat="server" CssClass="aspTable"
ForeColor="Black" Font-Size="XX-Small"
AutoGenerateColumns="False" PageSize="15" Width="100%"
CellSpacing="2" AllowPaging="True" BorderColor="#999999"
BorderStyle="Solid" BorderWidth="3px" BackColor="#CCCCCC"
CellPadding="4">
<SelectedItemStyle Font-Size="XX-Small" Font-Bold="True"
ForeColor="White" BackColor="#000099"></SelectedItemStyle>
<EditItemStyle Font-Size="XX-Small"></EditItemStyle>
<AlternatingItemStyle Font-Size="XX-Small"></AlternatingItemStyle>
<ItemStyle Font-Size="XX-Small" BackColor="White"></ItemStyle>
<HeaderStyle Font-Size="XX-Small" Font-Bold="True" ForeColor="White"
BackColor="Black"></HeaderStyle>
<FooterStyle Font-Size="XX-Small" BackColor="#CCCCCC"></FooterStyle>
<Columns>
<asp:ButtonColumn Text="Up" ButtonType="PushButton"
CommandName="Up"></asp:ButtonColumn>
<asp:ButtonColumn Text="Down" ButtonType="PushButton"
CommandName="Down"></asp:ButtonColumn>
<asp:EditCommandColumn ButtonType="LinkButton" UpdateText="Update"
CancelText="Cancel" EditText="Edit">
<HeaderStyle Font-Size="XX-Small"></HeaderStyle>
<ItemStyle Font-Size="XX-Small"></ItemStyle>
<FooterStyle Font-Size="XX-Small"></FooterStyle>
</asp:EditCommandColumn>
<asp:ButtonColumn Text="Delete" CommandName="Delete">
<HeaderStyle Font-Size="XX-Small"></HeaderStyle>
<ItemStyle Font-Size="XX-Small"></ItemStyle>
<FooterStyle Font-Size="XX-Small"></FooterStyle>
</asp:ButtonColumn>
<asp:TemplateColumn HeaderText="Photo">
<ItemTemplate>
<asp:Image runat=server
ImageUrl='<%#ReturnSmallURL(DataBinder.Eval(container.dataitem, "filename"),
dgAlbums.Items(dgAlbums.SelectedIndex).Cells.Item(7).Text)%>' ID="Image2"/>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
<PagerStyle Font-Size="XX-Small" HorizontalAlign="Left"
ForeColor="Black" BackColor="#CCCCCC"
Mode="NumericPages"></PagerStyle>
</asp:datagrid>
 
T

Thomas Dodds

the Page Load fires again BEFORE the UpdateCommand event ... be sure to only
'fill' the grid if the page is not posted back ...

use:

if not ispostback then
'do stuff
end if
 
P

Philip Rayne

Hi, thanks for the info. I was already doing that.

I have now sorted the problem, but I don't really understand why it didn't
work in the first place.
Basically I have now removed the code that adds the columns in the code and
created them in the asp instead :

DataField="PhotoText" />
<asp:BoundColumn HeaderText="Filename" SortExpression="Filename"
DataField="Filename" ReadOnly="True" />
<asp:BoundColumn HeaderText="Order" SortExpression="OrderPos"
DataField="OrderPos" ReadOnly="True"

This now seems to work when I try and access the value of the edited cell.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,708
Latest member
SherleneF1

Latest Threads

Top