How to read items during databinding which don't appear in GridView? - asp.net

In ASP.NET, using a GridView control. Assume my SQLDataSource has an ID, Name and Address. I am only showing Name and Address in the grid, but I want to read the ID every time a row is databound to the grid. How would I do this?
Thanks

The best way in these scenarios is to set the DatakeyName of your ID column in the GridView.
You may use it later on where you need DataKeyNames="ID"

You can bind the ID to a hiddenfield or to any control and make that control's visible property false.

What you need to do is get access to the underlying DataViewRow Object through the Gridview's RowDataBound Event. Once you have the DataViewRow you have access to the DataItem object this is the actual object your GridView is bound to. MSDN has an example here.
Cheers

Related

Bind to a page in ASP.NET

I am confused about something really simple in ASP.NET. I have seen many times a pattern where there is a three column table, with label, control and validator in each column, which I can put together pretty straightforwardly. However, what I don't understand is how to handle binding here. If I have a table with Customer record and FirstName, LastName and PhoneNum, I want a page that takes customerId in the query string, and binds that record to the page so that I can use Bind() as the text box values.
However, there is no DataSource property on the page to bind the record to. I know I am missing something obvious, but I can't figure it out.
Any help would be appreciated.
You need to put your controls (labels, textboxes, validators) inside a control that accepts data binding (ie. FormView, GridView, Repeater, etc.etc.) and then bind your data to that control.
One way to do this is to use a DataSource Control such as a SqlDataSource and use it in conjunction with a Data Control that will bind to the previous datasource control like a DetailsView. Then you configure your DataSource control to only bring in data for the particular CustomerId; the SqlDataSource allows you to use a QueryStringParameter as part of the select statement to the database.
Each of the above controls have wizards that will allow you to easily configure them.

Getting the selected row from a ASP.Net FormView bound to ObjectDataSource in the ItemUpdating event

I have to maintain an ASP.net application in VB.Net.
There is a page with a FormView bound to a ObjectDataSource.
I have to add some business logic on the ItemUpdating event of this FormView.
Unfortunately, some the data that I need to add this business logic is not exposed on the FormView user-interface itself, so I can not use FindControl to get the values (I could add the controls, bind them to the fields I need and set their visible property to true, but that's ugly).
So, what I would need to do is to get access to the Data Row corresponding to the currently selected item in the FormView from the code behind as it has the data I need to add my business logic code.
Unfortunately, I don't manage to get access to the row.
Thanks in advance for your help.
Try this:
Dim myData As Object = DirectCast(formview1.DataItem, DataRowView)("MyColumn")
EDIT:
If I remember correctly the DataItem is Nothing on ItemUpdating so my solution above does not work, does it? Then you have to load it from your Datasource with the given ID(CommandArgument).
Thanks, I managed to sort it using the Select method of the ObjectDataSource object.
This returned me a DataView containing the row that was currently being edited.

ASP.NET: Repeater control inside a Gridview row

I want a Repeater control inside a Gridview row. So how can I assign the DataSource of the Repeater control so that the data it uses is that of the specific data of the GridView row?
I tried this:
DataSource='<%# Eval(Container, "DataItem.InnerVal")
but it does not work.
You will need to handle the GridView.RowDataBound event in your code-behind. This will allow you to dynamically create a Repeater and bind it to the data for the row you are on.
go through this: http://www.codeproject.com/KB/database/repeater_in_gridview.aspx
Hope it help you.

Changing WebControl Property Inside of a Repeater

I have a repeater control that contains an ItemTemplate containing a databound label and a DropDownList control. With each iteration, I'd like to change the ID of the DropDownList with each iteration, in order to use the values as inputs upon submission. Can anyone tell me how this is usually done? Thanks.
This is done for you automatically, see the Using the NamingContainer Property to Determine a Control's Naming Container for details on how it works.
I imagine that probably isn't exactly what you are looking for though. In order to get the values from the drop downs, you should loop through the items in the repeater and use the FindControl method to find the drop down based on the ID you specified.
foreach(RepeaterItem item in repeater1.Items) {
DropDownList dropdown = (DropDownList)item.FindControl("DropDownList1")
//dropdown.SelectedValue
}
Edit: Based on your comment, I would not renamed the drop down list to be the ID in your database. Instead you should put a hidden literal control in your repeater. Set the value of that to the ID of your database record. Then get the ID the same way you get the value of the drop down list.
(Literal)item.FindControl("ListeralWithDataBaseId")
Your aspx markup will look like this
<asp:Literal runat="server" id="ListeralWithDataBaseId"
Value='<%# Eval("Id")' %> Visible="false" />
I found it easiest to just put a PlaceHolder in the Repeater, then create the DropDownList controls at design-time, rather than trying to create one at design-time and changing the ID as it iterates.

Issue trying to bind rss feed to repeater

I am trying to bind a datatable to a repeater. To do this, according to a code sample, I need to call dataview() when choosing the datasource property of the repeater and then write databind().
When I do this, I still get the exception that the source to databind to is not derived from IDatasourceControl.
What is the correct way to bind a datatable to a repeater? I want each record to repeat itself in the repeater (obviously).
I have seen this link (http://blogs.x2line.com/al/archive/2008/06/21/3469.aspx). What exactly does Container.Dateitem in the expression on the ASPX page mean?
Thanks
With a repeater you should be able to just do the following:
rpYourRepeater.DataSource = dtYourDataTable;
rpYourRepeater.DataBind();
That's it.
Just verify that your DataSourceID is tied to a real field in your datatable or just leave the DataSourceID out completely as you don't really need it depending on what you are doing.
You don't need any of that dataview stuff unless you want to start putting filters on your data.
I'm guessing that you are attempting to bind to DataSourceID. You should bind to DataSource in your repeater.

Resources