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.
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 looking at somebody else's code and in it there is a user control:
<UserControl:Comments ID="Comments1" runat="server" ObjectID="4" ObjectRecordID='<%#((Alert)Container.DataItem).AlertId %>'></UserControl:Comments>
What I don't quite understand is how the value for ObjectRecordID gets assigned. I
understand by looking at the code that AlerId is getting assigned to ObjectRecordID
but how is ((Alert)Container.DataItem).AlertId grabbing its value?
Thanks.
Container refers to the parent control of the comment box, which would be a single "row" of the Repeater.
Container.DataItem is a single object from the collection to which the Repeater is databound. Specifically, the object that is bound to the "row" being processed.
(Alert)Container.DataItem takes the DataItem and casts it as an Alert reference.
.AlertId grabs the AlertId property of the Alert object that resulted from the previous step, so that its value can be assigned to the ObjectRecordID property of the Comments control.
the <%# %> element signals to ASP.NET that you are databinding the expression; Because of that, and the fact that it is contained within the ItemTemplate of a Data-bindable control, the property Container.DataItem is now available. Container.DataItem is an Object (which is why it is being cast to the Alert type) representing the current item in the list of data objects being bound to the control.
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.
I have a form that contains a GridView control which is databound to an ObjectDataSource Control. There are several TextBox controls that are already being used as ControlParameters for the ObjectDataSource. I also have a custom data object that the ObjectDataSource is associated with {TypeName="myDataClass"}. The values are passed from the ObjectDataSource to myDataClass.
Now I need to also use a multi-select ListBox as a ControlParameter. When I use the SelectedValue parameter of the ListBox, the ObjectDataSource is only seeing the first selected item in the list.
Here's the question: How can I get all the multi-select ListBox values passed into my custom object "myDataClass" instead of just the first one selected? Hence the ["multi-select"]
Thanks for any help!
Multi-select list boxes are tricky. You need to loop through the items in code to build a list of selected values. So you will probably need to implement a custom parameter that does this for you.
You might end up needing to bind to the grid from code-behind, instead of doing it declaratively.
This is typical problem. multi-select list box always return the first selected value and not all the selected value. We have to loop through the entire collection and check each individual value if selected or not.