DataKeys dataList - asp.net

This is my bare DataList definition :-
<asp:DataList runat="server" ID="dtltrial"
ondeletecommand="dtltrial_DeleteCommand" DataKeyField="PhotoId" >
</asp:DataList>
where PhotoId is my primary key of table and so i have given it as datakeyField. However i also want AlbumId along with DataKeyField. How do i specify it in DataKeyField and later on retrieve it in ondeletecommand event of DataList?
Thanks in advance :)

You can specify:
DataKeyNames="PhotoId,AlbumId"
Update:
It looks like the DataList doesn't allow you to specify multiple keys, and then retreive them as normal. From this question, the accepted answer deals with a GridView. Unfortunately the DataList doesn't function the same.
You'll likely have to create a unique identifier yourself at the datasource, or create a hybrid on a databinding event. For example, "PhotoId_AlbumId", which would turn into "837_7826". On the delete event, you'd then have to extract out the two IDs, separated by the underscore.
Consider creating a new single primary key on the datasource instead.

Related

ASP.NET - Trying to implement SortParameterName property to SqlDataSource

I have several Gridviews and Repeaters bound to SqlDataSources using stored procedures. I am trying to implement sorting functionality into some of these but am having a hard time finding concrete instructions and/or examples of what is required on the SqlDataSource side to generate the ORDER BY's needed. Particularly, I do not understand the point of having a SortParameterName property in the SqlDataSource if all it does is manually connect to an ORDER BY clause in the stored procedure. Why define it as such if it is just another parameter in the SelectParameters list like any other, but just so happens to be connected to the ORDER BY clause? When I run the code example below, I am told there are too many arguments specified (obviously, the extra SortParams argument). Do I really need to alter my stored procedures and add "ORDER BY #SortParams" clauses to the end of the existing queries to make this work? I feel like I am missing something.
SqlDataSourceInLine.SelectParameters.Clear()
SqlDataSourceInLine.SelectCommandType = SqlDataSourceCommandType.StoredProcedure
SqlDataSourceInLine.SelectCommand = "ApproverGetApproved"
SqlDataSourceInLine.SelectParameters.Add("CompanyID", ConfigurationManager.AppSettings("Temp_CompanyID"))
SqlDataSourceInLine.SelectParameters.Add("SortParams", "EmpName DESC")
SqlDataSourceInLine.DataSourceMode = SqlDataSourceMode.DataSet
SqlDataSourceInLine.SortParameterName = "SortParams"
Dim dv As DataView = SqlDataSourceInLine.Select(DataSourceSelectArguments.Empty
Any clarification would be appreciated!
I was just trying to figure out how to use the SortParameterName and found this question. After doing some more search I think I have now found the correct answer.
Microsofts page Sorting Data with Data Source Controls says as follows (my emphasis):
The parameter identified by the SortParameterName property is passed to the ObjectDataSource control's SelectMethod or passed as part of the parameter collection to the SqlDataSource control's SelectCommand. The ObjectDataSource control can use the information passed to it in the sort parameter to return the data in sorted order. For the SqlDataSource control, you must supply the name of a stored procedure that can take the sort parameter and return the sorted data, because you cannot pass a parameter as part of an ORDER BY clause.
This indicates that the answer given by Icarus is not correct.
My conclusion is that when the SortParameterName property is set (in combination with an appropriate stored procedure) the Gridview will not do the sorting itself, but will let the datasource do a so called Custom Sorting, which for example would be the necessary way to sort if Custom Paging is used.
Update:
I have now used it in my own programming and confirmed that my conclusion was correct.
I've never used the SortParamter in the past, but what I gather from the documentation is that the purpose of this parameter is to allow you to get the results sorted in the way you want them in case the stored procedure does not do it already. Other than that, you don't need to use it for anything. A GridView whose datasource is of type SqlDataSource already implements sorting out of the box. You simply need to set the AllowSorting property to True and the SortExpression on every column.
Example:
<asp:GridView ID=" productsGridView" Runat="server"
DataSourceID="SqlproductDataSource" AutoGenerateColumns="False"
AllowSorting="True" >
<Columns>
<asp:BoundField HeaderText="Product"
DataField="ProductName" SortExpression="ProductName">
</asp:BoundField>
...
Nothing is required on the SqlDataSource. You need to implement the Sorting event for the Gridview and something else for the repeaters since they don't have sorting built in. What you could do (if you have small datasets coming back) is to use ViewState and store your DataTable of results and then utilize the DataView and the sorting capability of that, then bind the Repeaters/GridViews to the sorted DataView. You still have to keep track of the SortDirection and SortParameter within ViewState, regardless.

GridView not updating when data comes from multiple tables

I have a GridView control that gets data from two tables, the first one contains a primary key, a name (string) and a foreign key to the second table, The second table contains a primary key "referenced by the foreign key mentioned" and a name (string), i was able to display the id, name (first table) and the name (second table) using an inner join but i can't update the data in the tables using the GridView (when pressing update nothing happens at all, or no change occurs).
From what i understand of your question what you have is a situation that you must a apply a nested gridview.
A gridview shows the content of a table (datatable or a collection).
If you want to show other collection that is inside each row of your primary gridview, you will need
to build a second Gridview or listview or repeater to show that information.
with the primary griview you can use the OnItemDataBound to assign the datasource of the nested gridview or what you choose to show that information based in primary key of the row.
Don't bother i found a solution to the problem on Microsoft website, i am sorry i didn't make my question pretty clear and here is a link to what i was looking for: Editing with Template Fields
Maybe after clicking Update you need to call your read method again to refresh your gridview.
something Like:
UpdateMethod()
{
//YOUR UPDATE STUFF
//REBIND DATA WITH UPDATED RECORDS
RefreshMethod(); //YOUR BINDING METHOD TO DATAGRID STUFF
}

How to loop gridviews for each database row?

I am developing an asp.net web form application that displays some info on separate gridviews based on parameters. The second gridview depends of the values in the first one (Salida and Llegada, they work as a time range). This works only when the data displayed on the first GV has just one row.
This is how it works:
But, is there a way to loop the same gridviews for each row stored in the database? something like this:
Or maybe there is an easier option I haven't considered.
Thanks in advance.
Ok it should be simple, first sit your gridview1 paging to fetch only view only one record, Then after you fill your gridview1 i assume that you have putten an ID either as Datakeys or what ever method you have used, the index of the row should be 0 since it's always viewing one row only. Get your id and fetch your data and bind it the second gridview2.
After that on the Gridview1_Paging event you bind your data again and the use the same method above to fetch the data for the next record.
Seems a bit clunky, but the simplest option may be to add GV1 and GV2 to a repeater. Each row of the repeater would essentially be the data source for your GV1.

Is it possible to have a SQLDataSource with a parameter that is based only upon the GridView that is binding to it?

I have a scenario where I want to put four identical Gridviews on the same page. (They will be on different tabs in an Ajax TabControl.) They show the same source data, but there are four corresponding groups of source data in a common underlying table. So I want to show Group 1 on Tab 1, Group 2 on Tab 2, etc. These Gridviews contain complicated controls, so I would prefer to use the same data source for all of them to avoid unnecessary repetition. The Insert and Update commands are completely identical.
So in theory I could build the Select command in such a way that I could filter the data based on the GridView that is binding to the SQLDataSource. The problem is that if I use the same SQLDataSource for all the Gridviews, I cannot find a way to have each GridView tell the SQLDataSource which one is calling it. I am thinking maybe this is not possible, because the SQLDataSource binds first before it knows what is binding to it, but I'm not sure. Can anyone think of a way to do this?
You can change the parameter value dynamically using OnSelecting event of SQLDataSource. This can be done in server side code.
Create a property which holds your current gridview unique key, which is causing SQLDataSource to fetch data from SQL database.
Assign this property unique gridview key on DataBinding event of gridview.
Based on this property change the parameter in OnSelecting event of SQLDataSource.
Let me know if I am missing something.

Gridview: column with different controls is different modes

Here is my scenario.
I have an objectdatasource-bound gridview where i want to show Students {name, age, class}
name:string, age:int, Class:Foreign key, int
I also want to edit it, but when doing so i want to show a dropdownlist for the class field.
Currently i solve this by
a)creating a view, binding the grid to that view
b) on the editItemTemplate, put a DDL and bind it to the Class table.
What i want to know is if there is a way without involving the view shortcut.
NOTE: this question, Bind column to a different entity source than the gridview is
is somewhat related, but not quite the same thing. in that example, the OP just wanted to show the related data. Since am also updating, i need both the key and the value.

Resources