change the TypeName property during run-time?

4 posts, 0 answers
  1. MWM
    MWM avatar
    64 posts
    Member since:
    Jul 2007

    Posted 08 Dec 2008 Link to this post

    When I create an OA datasource through the wizard, and use it as the datasource for a grid, I am forced to select a particular table from my DB.

    During run-time however, I'd like to be able to change the table to show its data in the grid.  Can i do this by setting properties of the 

    OpenAccessDataSource in the code-behind?


    I tried something like:

     

    OpenAccessDataSource1.TypeName =

    "DataClasses.Person";

     

    OpenAccessDataSource1.DataBind();

    But this didn't work.

    Suggestions?

  2. Jan Blessenohl
    Admin
    Jan Blessenohl avatar
    707 posts

    Posted 09 Dec 2008 Link to this post

    Hello MWM,
    This should work. What is happening? Did you define grid columns that are not matching the new objects? Can you send me some more info?

    All the best,
    Jan Blessenohl
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  3. DevCraft banner
  4. Stephen
    Stephen avatar
    15 posts
    Member since:
    May 2007

    Posted 12 May 2009 Link to this post

    Hi

    Was this fixed please as having same problem? - if so could you post solution found

    Rgds
    Stephen
  5. PetarP
    Admin
    PetarP avatar
    754 posts

    Posted 14 May 2009 Link to this post

    Hello Stephen,
    to accomplish this you should firstly let the gird generate the columns automatically instead of specifying them in advance. This way when you change the TypeName of the DataSource the grid wont search for properties that are not members of the newly set class. Here is an example of how a grid should look like:
    <asp:GridView ID="GridView1" runat="server"  
                DataSourceID="OpenAccessDataSource1" AllowPaging="True"
            </asp:GridView> 
    Another problem that comes from trying to set the TypeName dynamically(or any other property) is that  the one that is already written in the aspx page will override the one set from code behind on each PostBack. Having this in mind you can set the TypeName from the code behind and display the new class in the grid, but if you have let say paging on,  and you change a page than the code from the aspx page will be applied and it will override the one set from the code behind. A way to go around this is to save the TypeName in the session and applay it on every post back of the page. This way your TypeName will be saved even after reloading a page. Here is an example on implementing this in the code behind:
    public partial class _Default : System.Web.UI.Page 
        { 
            protected void Page_Load(object sender, EventArgs e) 
            { 
     
                if (!Page.IsPostBack) 
                { 
                    Session["TypeName"] = OpenAccessDataSource1.TypeName; 
                    OpenAccessDataSource1.TypeName = (string)Session["TypeName"]; 
                } 
                OpenAccessDataSource1.TypeName = (string)Session["TypeName"]; 
            } 
     
            protected void Button1_Click(object sender, EventArgs e) 
            { 
                OpenAccessDataSource1.TypeName = "OpenAccessData.Order"
                Session["TypeName"] = OpenAccessDataSource1.TypeName; 
            } 
        } 

    And here is how the data source contol looks like:
     <telerik:OpenAccessDataSource ID="OpenAccessDataSource1" runat="server"  
                ContextTypeName="" EnableDelete="True" EnableInsert="True" EnableUpdate="True"  
                ObjectContextProvider="OpenAccessData.ObjectScopeProvider1, OpenAccessData"  
                OrderBy="" TypeName="OpenAccessData.Category" Where="" >            
            </telerik:OpenAccessDataSource> 
    Note that the TypeName is initially set to Category but when you click on a button it is set to Order in the code behind and its saved in the session. Later on each Post Back the TypeName is applied so its not lost.

    All the best,
    PetarP
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
Back to Top