This is a migrated thread and some comments may be shown as answers.

change the TypeName property during run-time?

3 Answers 97 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
MWM
Top achievements
Rank 1
MWM asked on 08 Dec 2008, 10:14 PM
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?

3 Answers, 1 is accepted

Sort by
0
Jan Blessenohl
Telerik team
answered on 09 Dec 2008, 02:05 PM
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.
0
Stephen
Top achievements
Rank 1
answered on 12 May 2009, 05:56 PM
Hi

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

Rgds
Stephen
0
PetarP
Telerik team
answered on 14 May 2009, 07:25 AM
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.
Tags
General Discussions
Asked by
MWM
Top achievements
Rank 1
Answers by
Jan Blessenohl
Telerik team
Stephen
Top achievements
Rank 1
PetarP
Telerik team
Share this question
or