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" ContentPlaceHol derID="ContentP laceHolder1"
runat="Server">
<br />
<br />
<asp:CheckBox ID="AllCheck" runat="server" AutoPostBack="T rue" />
<br />
<br />
<asp:GridView ID="RouteGridVi ew" runat="server"
AllowPaging="Tr ue" AllowSorting="T rue"
AutoGenerateCol umns="False" DataKeyNames="d estId"
SkinID="GridVie w"
DataSourceId="s 1" OnRowDeleting=" myrowdeleting"
OnRowDeleted="m yrowdeleted">
<Columns>
<asp:CommandFie ld ButtonType="Ima ge"
ShowDeleteButto n="true" ShowSelectButto n="True"
SelectImageUrl= "../App_Themes/WinXP_Silver/Images/edit.gif"
DeleteImageUrl= "../App_Themes/WinXP_Silver/Images/delete.gif"/>
<asp:BoundFie ld DataField="Plan Code"
ItemStyle-Width="20" HeaderText="Pla n Code" SortExpression= "Plancode"
/>
<asp:BoundFie ld DataField="Dest Code"
ItemStyle-Width="20" HeaderText="Des t Code" SortExpression= "routeCode"
/>
<asp:BoundFie ld DataField="rout eCode"
ItemStyle-Width="20" HeaderText="Rou te Code"
SortExpression= "routeCode" />
</Columns>
</asp:GridView>
<asp:SqlDataSou rce ID="s1" runat="server"
ConnectionStrin g="<%$ ConnectionStrin gs:RTEConnectio nString %>"
SelectCommand=" select * from gen_routeinfo_v w where
routecode = 'JCP'"
DeleteCommand=" delete route where destid=@destId; delete
destination where destid=@destid"
>
<DeleteParamete rs>
<asp:Paramete r Name="destId" />
</DeleteParameter s>
</asp:SqlDataSour ce>
<asp:SqlDataSou rce ID="s2" runat="server"
ConnectionStrin g="<%$ ConnectionStrin gs:RTEConnectio nString %>"
SelectCommand=" select * from gen_routeinfo_v w where
routecode = 'JCP'"
DeleteCommand=" delete route where destid=@destId; delete
destination where destid=@destid" >
<DeleteParamete rs>
<asp:Paramete r Name="destId" />
</DeleteParameter s>
</asp:SqlDataSour ce>
<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.P age
Protected Overrides Sub OnPreRender(ByV al e As EventArgs)
If IsPostBack Then
RouteGridView.D ataSourceID = Nothing
RouteGridView.D ataSource = Nothing
If AllCheck.Checke d Then
RouteGridView.D ataSource = s2
Else
RouteGridView.D ataSource = s1
End If
RouteGridView.D ataBind()
End If
End Sub
Protected Sub Allcheck_Checke dChanged(ByVal sender As Object, ByVal
e As System.EventArg s) Handles AllCheck.Checke dChanged
RouteGridView.D ataBind()
End Sub
Protected Sub myRowDeleted(By Val sender As Object, ByVal e As
GridViewDeleted EventArgs) Handles RouteGridView.R owDeleted
If e.Exception IsNot Nothing Then
StatusLabel.Tex t = e.Exception.ToS tring.Substring (0, 250) +
"<br>" + StatusLabel.Tex t
e.ExceptionHand led = True
Else
StatusLabel.Tex t = "Record Deleted"
RouteGridView.D ataBind()
End If
End Sub
Protected Sub myRowDeleting(B yVal sender As Object, ByVal e As
GridViewDeleteE ventArgs) Handles RouteGridView.R owDeleting
'RouteGridView. DataBind()
End Sub
End Class