WallyAnchor
Programmer
(Reposted from Visual Basic(Microsoft) -VB.NET/VB2005 Forum, as this forum seems a better place for it...)
Hi.
I have a DataList that shows a number of GridViews. The datasource for the DataList returns a Name/title for each GridView, along with the Select/Delete commands and comma seperated list of datakeys.
Each GridView potentially has a different number of columns, and the column names differ. For this reason I've set AutoGenerateColumns="True", and am adding a delete commandfield. The select/delete commands are being set in the codebehind.
My problem is, although the select & delete commands are being set in the datasource when the page is displayed, they are lost on postback, after clicking a delete button in a gridview, and I get the error "Deleting is not supported by data source 'dsInner' unless DeleteCommand is specified."
What am I doing wrong? How can I get the datasource to retain the DeleteCommand that I'm setting in the codebehind?
Here's a rather simplified version of my page:
and the codebehind:
Hi.
I have a DataList that shows a number of GridViews. The datasource for the DataList returns a Name/title for each GridView, along with the Select/Delete commands and comma seperated list of datakeys.
Each GridView potentially has a different number of columns, and the column names differ. For this reason I've set AutoGenerateColumns="True", and am adding a delete commandfield. The select/delete commands are being set in the codebehind.
My problem is, although the select & delete commands are being set in the datasource when the page is displayed, they are lost on postback, after clicking a delete button in a gridview, and I get the error "Deleting is not supported by data source 'dsInner' unless DeleteCommand is specified."
What am I doing wrong? How can I get the datasource to retain the DeleteCommand that I'm setting in the codebehind?
Here's a rather simplified version of my page:
Code:
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">[/URL]
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml"[/URL] >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DataList ID="dlItems" runat="server" DataSourceID="dsOuter" RepeatDirection="Horizontal" RepeatLayout="Flow" ItemStyle-Width="200">
<ItemTemplate>
<asp:Label ID="lblItemHeader" runat="server" Text='<%#Container.DataItem("ItemName") %>' />
<asp:GridView ID="gvInfo" runat="server" GridLines="None" DataSourceID="dsInner" AutoGenerateColumns="True">
<Columns>
<asp:CommandField DeleteText="delete" ShowDeleteButton="True" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="dsInner" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString1 %>" ProviderName="<%$ ConnectionStrings:ConnectionString1.ProviderName %>" />
</ItemTemplate>
</asp:DataList>
<asp:SqlDataSource ID="dsOuter" runat="server" SelectCommand="GetOuterItems" SelectCommandType="StoredProcedure" ConnectionString="<%$ ConnectionStrings:ConnectionString1 %>" ProviderName="<%$ ConnectionStrings:ConnectionString1.ProviderName %>" />
</div>
</form>
</body>
</html>
Code:
Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub dlItems_ItemCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataListItemEventArgs) Handles dlItems.ItemCreated
If Not IsPostBack() Then
Dim dataItems As Object() = CType(e.Item.DataItem, Data.DataRowView).Row.ItemArray
Dim tmpItem As DataListItem = e.Item
Dim datakeys As String() = CType(dataItems(4), String).Split(",")
Dim dsInnerItem As SqlDataSource = tmpItem.FindControl("dsInner")
Dim tmpGV As GridView = tmpItem.FindControl("gvInfo")
tmpGV.DataKeyNames = datakeys
dsInnerItem.SelectCommand = dataItems(1)
dsInnerItem.SelectCommandType = SqlDataSourceCommandType.StoredProcedure
If dataItems(2) Then 'Show OK/Hide
dsInnerItem.DeleteCommand = dataItems(3)
dsInnerItem.DeleteCommandType = SqlDataSourceCommandType.StoredProcedure
For Each key As String In datakeys
dsInnerItem.DeleteParameters.Add(key, 0)
Next
End If
End If
End Sub
End Class