gridview switching datasources in the codebehind. ... Delete no longer happens.

Discussion in 'ASP .Net' started by jobs, Nov 21, 2006.

  1. jobs

    jobs Guest

    I've got a gridview that does not have a datasourceid assigned in the
    markup.

    I'd like to switch between two datasources in the codebehind. when I do
    switch, I first reset the the
    gridviewx.datasource = nothing and gridviewx.datasourceid = nothing
    before setting it to gridviewx.datasource = newdatasoruceid and then
    rebinding.

    All appears to work well until I try to excute a delete command that
    otherwise works great when the datasource is set fixed in the markup.
    Except now, nothing happens.

    I've tried adding ondeleting and ondeleted, the code never gets to
    ondeleted when I switch datasources in the codebehind.

    I there something else I need to set, or are things broken beyond
    repair at that point. If so, shame as the potential of muliple
    datasources offers a lot of options that help simplify sql.

    Thanks for any help or information.
     
    jobs, Nov 21, 2006
    #1
    1. Advertising

  2. jobs

    Mohsin Rizvi Guest

    Re: gridview switching datasources in the codebehind. ... Delete no longer happens.

    Hello jobs!

    Thanks for posting a topic.

    It could be difficult to understand what actually you are trying to
    accomplish.

    Kindly paste the code here so that the problem could be resolved.

    Regards,
    Mohsin Rizvi
     
    Mohsin Rizvi, Nov 21, 2006
    #2
    1. Advertising

  3. jobs

    jobs Guest

    Re: gridview switching datasources in the codebehind. ... Delete no longer happens.

    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
     
    jobs, Nov 21, 2006
    #3
  4. jobs

    jobs Guest

    Re: gridview switching datasources in the codebehind. ... Delete no longer happens.

    Apparently switching datasources in codebehind messes up other things
    like sorting and paging too...

    The GridView 'RateGridView' fired event Sorting which wasn't handled.

    Unless somebody knows how to do this I will need to duplicate the
    grids and have one for every source.
     
    jobs, Nov 21, 2006
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. jeaves
    Replies:
    0
    Views:
    359
    jeaves
    Jul 2, 2006
  2. jobs
    Replies:
    0
    Views:
    376
  3. =?Utf-8?B?TGFkaXNsYXYgTXJua2E=?=

    FormView and GridView with DataSources

    =?Utf-8?B?TGFkaXNsYXYgTXJua2E=?=, Feb 1, 2007, in forum: ASP .Net
    Replies:
    0
    Views:
    460
    =?Utf-8?B?TGFkaXNsYXYgTXJua2E=?=
    Feb 1, 2007
  4. NM
    Replies:
    6
    Views:
    473
    Default User
    Sep 20, 2006
  5. Shanksluvsuall
    Replies:
    0
    Views:
    374
    Shanksluvsuall
    Mar 14, 2008
Loading...

Share This Page