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.
Related
I want to give a value to a control in asp.net repeater footer template.
Dim FooterTemplate As Control = TicketRepeater.Controls(TicketRepeater.Controls.Count - 1).Controls(0)
Dim lblFooter As Literal = TryCast(FooterTemplate.FindControl("TicketTotalNumberOfTickets"), Literal)
lblFooter.Text = TicketDataTable.Rows.Count.ToString()
Return with error:
Specified argument was out of the range of valid values.
Parameter name: index
AFAIU it cant find count of controls = no controls found?. Any suggestions?
Check points:
all ID's are correct
repeater has 3 part headertemplate,
itemtemplate, footertemplate
Help!
Update
The code actually work after databind(). Any ideas how to make it work before databind event?
It can't be done before databind. Until you bind that repeater to something, it's just an empty shell and has no controls. Binding is what creates the header, footer, and items.
Binding would wipe away anything to do with the footer, anyway, so if you intend to bind to that repeater (and why else use one?) there's no point in trying to manipulate it before you bind to it.
If what you want is to have an empty repeater with header and footer and later (perhaps after some action by the user) bind to a populated data source, you can try binding to an empty (but not null) data source. I'm not 100% sure, but I think that should produce a header and footer in your repeater but no items.
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
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.
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.
I'd like to set the text of two labels to values found in a FormView on a page (whose data comes from an SQLDataSource.)
What's the best way to do this? I'm thinking of using the DataBound event for the FormView to set the label text to the value of a field in the FormView, or of using the SQLDataSource Selected event to set the labels to values retrieved by the query. Could I use the Page_Load event in conjunction with the FormView?
The FormView only displays one of the two values, though the other value is retrieved by the SQLDataSource.
I'm unfamiliar with accessing the data structures behind these controls but figure the data is there so I might as well use it rather than run the same SQL query twice.
My question then is which event do I use, which control do I access the data from, and how do I access the data from that control?
I'd use the OnDataBound event and get the value from the underlying datasource using:
lblExample.Text = ((DataRowView)((FormView)sender).DataItem)["fieldName"].ToString();
Hope it helps.
// CeriQ
If you're just trying to set a label, just set the label at the Page_load event:
myLabel.Text = "someValue";