in ASP.NET 3.5 web page, a GridView control can have both SqlDataSource and code-behind datasource (with a DataTable for example)?
Both datasource are equal (the same stored procedure).
Thanks a lot.
Luis
It is possible only under one condition.That you set the DataSourceID to nothing of your gridview before re-assigning its datasource to a datatable.Otherwise visual Studio will scream loudly at you in these lines
'Both DataSource and DataSourceID are defined on 'yourGrid'. Remove one definition.'
yourgrid.DataSourceID=null;
yourGrid.DataSource=yourDataTable;
Related
I am setting up a Gridview to display data from a SQL database. I know I can set it up in the aspx page or in the code behind. Is there a best practice to follow on deciding the best approach? Do you normally set it up in the code behind?
The SqlDataSource control is a server control just like the GridView control is, so to answer your question there really is no "better" (declaratively only versus imperatively only) way to use server controls.
The few times I have used the SqlDataSource control, I have always declared the control in the markup and set the properties for the data retrieval inside of the markup as well.
Reality usually dictates that you will declare your server controls in your markup for both the GridView and the SqlDataSource and then apply/update the properties of the server controls in your code behind, but probably you will write more code-behind for a GridView than a SqlDataSource, because the GridView needs to react to events on the rows within the grid. If you do not have any dynamic queries, then most of the time your SqlDataSource will have static queries that can be applied in the markup (decaratively).
I have never used any of the datasource objects in my aspx pages. I set the datasource for a gridview and databind() in the page's code behind. I am looking at some samples which use a sqldatasource and datasourceid in a gridview.
My question is when a sqldatasource is used as a data source for a gridview, does the databinding happen in page's init lifecycle? Is it the same as setting the datasource and databind in init event when not using a sqldatasource?
1- yes , in Prerender event a method EnsureDataBound() is fired that calls gridview Databind() method
2- yes it is
you can check these posts :
http://blog.tylerholmes.com/2008/06/datasource-vs-datasourceid-internals.html
ASP.NET - What is the difference of DataSourceID and DataSource?
I have multiple TabPanels with one GridView each. I successfully used the GUI to set the datasource for each GridView. After adding the latest TabPanel and GridView I created the datasource and finished, but it didn't create any datasource. The datasource dropdownlist in GridView is empty. There are no errors, it just doesn't do anything when I click "finish". Now I can't create a new datasource for any new GridView on any tab. Existing GridViews and datasources work fine. What could cause this?
May be u did copy & paste your code, Or any panel, VS give unique name to sqldatasource always.
What is the efficient way to make a gridview from 2 tables (one of them is using the primary key of the other as a foreign key) using visual studio 2005?
thanks
Nested gridview (or datalist/repeater) is what you need. You need to create TemplateField in parent GridView to add another Gridview in it. Handle the RowDataBound event of parent gridview to bind its child elements/controls.
PS: You may read MSDN pages/tutorial about GridView and its events.
MSDN Link : Walkthrough: Creating a Nested GridView Control
I have a Repeater inside the TemplateField for a GridView. GridView is bound to datasource1 and the repeater to datasource2. How can i access datasource1 from the ItemTemplate of repeater in data binding syntax (<%# %>) of repeater itemtemplate?
See this question. That question was relevant to Repeaters but you get the idea. The GridView exposes the RowDataBound event which you can handle in the same way.
Of course, I don't believe there is a way to access it via the declarative Databinding syntax.