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.
Related
I have declared an asp:Repeater on my page with ID="AnswersRepeater". I am calling the below segment of code when the user clicks a button on the page to load additional data from a DataSet denoted as "ds". I need to understand the control hierarchy of an asp:repeater so I can add back in the items the repeater had after rebinding to the datasource like below:
Dim currentItems As New ArrayList(AnswersRepeater.Items)
AnswersRepeater.DataSource = ds
AnswersRepeater.DataBind()
For Each item As RepeaterItem In currentItems
AnswersRepeater.Controls.Add(item)
Next
The problem is, the RepeaterItems are not added in the correct place within the repeater control's hierarchy. I need to find the item template and append the items in currentItems array to it. Any help is much appreciated.
You shouldn't be directly editing the controls of the Repeater control, the idea is you bind to a data source and it dynamically creates controls for you based on that data source.
You'd be better adding the items you already have to your data source ds and then just binding once.
I have a gridview that I load from a linq query.
MyGridData is a the list that's returned from the linq query and that contains a variable called MyVariable. So, in the code behind I have:
MyGrid.DataSource = MyGridData;
MyGrid.DataBind();
I then went to the aspx source and added AllowSorting = true and to the boundfield I want to enable sorting for, I added SortExpression = "MyVariable".
When the page renders, if I click on the header of the column to sort the grid, I get a yellow death screen with this message:
The GridView 'MyGrid' fired event Sorting which wasn't handled
What am I doing wrong?
Thanks.
You have to implement the handler for the Sorting-Event in the Codebehind. Sort your datasource and bind your grid again.
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";
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.
Well i have a gridview where i have defined the columns on my own and turned autogenerating off but now i have the problem that i cant access GridView.SelectedRow.DataItem.
As it turns out to be null now, when it had a value when auto generation was turned on..
Edit:
What i need is a way to save the ID of the row while not showing the ID to the user so if there is any way to do this?
I'm guessing DataItem is only properly filled when you are using DataBinding.
Are you using DataBinding?
Ok from this url:
The GridView (and actually, all our
data controls) does not save data
items across postbacks. This reduces
ViewState (if the objects are even
serializable) and enables garbage
collection to happen and clean up your
objects. So, when you click the
button to post back, the GridView has
not called DataBind and therefore your
data item isn't there. This is what
you've discovered.
Guessing you're reading the value from a postback, might just be the problem.
Try using SelectedValue, if you've setup the (primary) key for the items.
I've always used that and it worked.
msdn about SelectedValue
You can create a new hidden template column that will have a label with the ID . and in the cs file you use .FindControl on the rows.
You also have DataKeys property on the gridview, witch I think also does what you want