have a simple gridview that I need to switch datasources on in the
codebehind. Everything works great with it until I switch. For example,
Delete, does not actually delete - it calls the on ondeleting sub, but
never actually deletes. If set the datasource in the markup and don't
switch, delete works fine.
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1"
runat="Server">
<br />
<br />
<asp:CheckBox ID="AllCheck" runat="server" AutoPostBack="True" />
<br />
<br />
<asp:GridView ID="RouteGridView" runat="server"
AllowPaging="True" AllowSorting="True"
AutoGenerateColumns="False" DataKeyNames="destId"
SkinID="GridView"
DataSourceId="s1" OnRowDeleting="myrowdeleting"
OnRowDeleted="myrowdeleted">
<Columns>
<asp:CommandField ButtonType="Image"
ShowDeleteButton="true" ShowSelectButton="True"
SelectImageUrl="../App_Themes/WinXP_Silver/Images/edit.gif"
DeleteImageUrl="../App_Themes/WinXP_Silver/Images/delete.gif"/>
<asp:BoundField DataField="PlanCode"
ItemStyle-Width="20" HeaderText="Plan Code" SortExpression="Plancode"
/>
<asp:BoundField DataField="DestCode"
ItemStyle-Width="20" HeaderText="Dest Code" SortExpression="routeCode"
/>
<asp:BoundField DataField="routeCode"
ItemStyle-Width="20" HeaderText="Route Code"
SortExpression="routeCode" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="s1" runat="server"
ConnectionString="<%$ ConnectionStrings:RTEConnectionString %>"
SelectCommand="select * from gen_routeinfo_vw where
routecode = 'JCP'"
DeleteCommand="delete route where destid=@destId;delete
destination where destid=@destid"
>
<DeleteParameters>
<asp:Parameter Name="destId" />
</DeleteParameters>
</asp:SqlDataSource>
<asp:SqlDataSource ID="s2" runat="server"
ConnectionString="<%$ ConnectionStrings:RTEConnectionString %>"
SelectCommand="select * from gen_routeinfo_vw where
routecode = 'JCP'"
DeleteCommand="delete route where destid=@destId;delete
destination where destid=@destid">
<DeleteParameters>
<asp:Parameter Name="destId" />
</DeleteParameters>
</asp:SqlDataSource>
<br />
<br />
<asp:Label ID="StatusLabel" runat="server" Height="109px"
Text="Label" Width="484px"></asp:Label><br />
<br />
<br />
</asp:Content>
.... codebehind :
Partial Class Delete
Inherits System.Web.UI.Page
Protected Overrides Sub OnPreRender(ByVal e As EventArgs)
If IsPostBack Then
RouteGridView.DataSourceID = Nothing
RouteGridView.DataSource = Nothing
If AllCheck.Checked Then
RouteGridView.DataSource = s2
Else
RouteGridView.DataSource = s1
End If
RouteGridView.DataBind()
End If
End Sub
Protected Sub Allcheck_CheckedChanged(ByVal sender As Object, ByVal
e As System.EventArgs) Handles AllCheck.CheckedChanged
RouteGridView.DataBind()
End Sub
Protected Sub myRowDeleted(ByVal sender As Object, ByVal e As
GridViewDeletedEventArgs) Handles RouteGridView.RowDeleted
If e.Exception IsNot Nothing Then
StatusLabel.Text = e.Exception.ToString.Substring(0, 250) +
"<br>" + StatusLabel.Text
e.ExceptionHandled = True
Else
StatusLabel.Text = "Record Deleted"
RouteGridView.DataBind()
End If
End Sub
Protected Sub myRowDeleting(ByVal sender As Object, ByVal e As
GridViewDeleteEventArgs) Handles RouteGridView.RowDeleting
'RouteGridView.DataBind()
End Sub
End Class