locked
Dynamically change ObjectDataSource SelectMethod RRS feed

  • Question

  • User-2010267424 posted

    Is it possible to change the SelectMethod of an ObjectDataSource Dynamically?

    I have this ObjectDataSource:

        <asp:ObjectDataSource ID="odsBikersList" runat="server" SelectMethod="FindAll"

            TypeName="BikersDAO" DeleteMethod="DeleteOne" SortParameterName="SortExpression" >

            <DeleteParameters>

                <asp:Parameter Name="BikerID" Type="String" />

            </DeleteParameters>

        </asp:ObjectDataSource>

    I want to be able to on a OnClick event change the SelectMethod from "FindAll" to "FindAllCaptains".

    Is that possible?

    Thanks

     

    Sunday, May 15, 2011 12:26 AM

Answers

  • User3866881 posted

    Of course you can!

    I did a very simple but useful demo to proove what I say——

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="TestWebApp.WebForm1" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
                DataSourceID="ObjectDataSource1">
                <Columns>
                    <asp:TemplateField>
                   
                        <ItemTemplate>
                       
                            <%#Container.DataItem %>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>
        </div>
        <asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
            SelectMethod="Select1" TypeName="TestWebApp.Class1"></asp:ObjectDataSource>
        <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
        </form>
    </body>
    </html>

      protected void Button1_Click(object sender, EventArgs e)
            {
                ObjectDataSource1.SelectMethod = "Select2";
            }

    In Class1——

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;

    namespace TestWebApp
    {
        public static class Class1
        {
            public static string[] Select1()
            {
                return new string[] { "1","2","4"};
            }

            public static string[] Select2()
            {
                return new string[] { "2", "3", "4" };
            }
        }
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, May 16, 2011 11:25 PM