How to obtain Container.DataItem from parent control? - asp.net

Suppose that I create an ASP.net repeater. Suppose further that I put an ASP.net GridView in the ASP.net repeater's ItemTemplate.
Suppose that it looks like this:
<asp:Repeater runat='server' id='myRepeater'>
<ItemTemplate>
<%# "This Repeater DataSource is " + Container.DataItem.ToString() %>
<asp:GridView runat='server' id='repeaterGridView'>
<Columns>
<asp:TemplateField>
<ItemTemplate>
<% // I would like to refer to the Repeater Data Source here, but I only know
// how to refer to the GridView Data Source here.
<ItemTemplate>
</asp:templateField>
</Columns>
</asp:Repeater>
</ItemTemplate>
</asp:Repeater>
I would like to refer to the Repeater DataSource inside a TemplateField for the GridView. How can I do this?

Container refers to the TemplateField, so try calling
((RepeaterItem)Container.NamingContainer.NamingContainer).DataSource

Related

How to create labels for each item found in database?

The problem is like this, I have a announcement table in my database. I want to display the announcement's record that I've found from the database by using labels and text boxes. The question is how do I create new labels and text boxes each time a record is found and i want the layout to be something like this picture.
Use a Repeater or a DataList...
Try asp repeater. e.g
<asp:Repeater ID="myRepeater" runat="server" OnItemDataBound="myRepeater_ItemDataBound">
<HeaderTemplate>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lblHeading" runat="server" Text='<%# Eval("TableColoumnName") %>' />
<asp:TextBox ID="txtHeading" runat="server" Text='<%# Eval("TableColoumnName") %>' />
</ItemTemplate>
<FooterTemplate>
</FooterTemplate>
</asp:Repeater>
in code behind provide the data source to your repeater and bind that datasource...
protected void myRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
myRepeater.DataSource = // provide datasource here....
myRepeater.DataBind(); // bind the data with repeater
}
you can use gridview, repeater etc..
http://www.codeproject.com/Articles/18132/ASP-NET-using-GridView-Control-as-Lookup

asp.net FormView Data Binding with a Collection of Data

I have a collection of records, each record has an ID and a description.
Now in my formview I have 8 textboxes and I want each text box to hold description
of each record.
So if I do this
Text='<%# Eval("Record[0].Description") %>' />
This gives an error, any other way to do it?
Also can I do it in the markup, or do I need to do it in code behind, under databound method for the formview?
Thanks..
FormView is not meant for showing List of Data.
If you have a List of Data, then you should use GridView or ListView.
Bind your FormView with a datasource having single record and then directly Eval the fields of the datasource.
i.e. do this:
<asp:FormView ID="FormView1" runat="server" DataSourceID="SqlDataSourceId">
<ItemTemplate>
<asp:TextBox id="txtDescription"
Text='<%# Eval("Description") %>' />
<asp:TextBox id="txtName"
Text='<%# Eval("Name") %>' />
..
</ItemTemplate>
</asp:FormView>
so basically, your FormView should contain different DataField and it should be bound to a DataSource having just one Item.
You could use a repeater inside:
<asp:repeater ID="rep" runat="server" DataSource='<%# Eval("Record") &>'>
<ItemTemplate>
<asp:textbox id="txt" runat="server" Text='<%# Eval("Description") &>' />
</ItemTemplate>
</asp:repeater>
In the repeater you will bind to your outer datasource, inside the repeater your datacontext is the record

Get selected ID in asp.net listview bound to a hyperlink

I am new to asp.net, forgive my newbie question.
I have a listview which displays all item info (ItemID,Item name).
I bound the item name to a hyperlink control. now what i want to do is that when i click the hyperlink i want to get the ID and navigate the item detail page. i tried using selected index but it still returns null. here's my code.
On the listview itemTemplate code
<asp:HyperLink ID="HyperLink1" runat="server"
NavigateUrl="ItemDetails.aspx"> <%# Eval("[ItemName]") %>
</asp:HyperLink>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("[ItemID]") %>'/>
Please help.thanks in advance
Instead of Hyperlink, I used LinkButton in ItemTemplate If u want to pass the selectedId to the next page u can pass using Query String
<asp:TemplateField HeaderText="Edit" HeaderStyle-HorizontalAlign="Center" HeaderStyle-Width="120px">
<ItemTemplate>
<asp:LinkButton ID="lnk_ViewDetails" runat="server" Text='View Details' PostBackUrl='<%#"~/ViewDetailss.aspx?Id="+Eval("ID")%>'></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
If u want to encrypt querystring Please refer This Link

binding dataitem to columns in gridview

<%# ((DataRowView)Container.DataItem)["SomeProperty"] %>
<%# DataBinder.Eval(Container.DataItem, "SomeProperty")%>
From Google i figured out these can be used to bind the columns in GridView to ArrayList. But what is "some property" ?
For example i have a ArrayList in .aspx.cs as
static ArrayList componentSelectionArray = new ArrayList();
so can i just write in grid view to bind a arraylist to grid view columns as:
<asp:GridView ID= "GridView1" runat="server" AutoGenerateColumns="true">
<Columns>
<asp:TemplateField HeaderText="ComponentName">
<ItemTemplate>
<asp:Label ID="" text= "<%# DataBinder.Eval(Container.DataItem, "componentSelectionArray")%>" ></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</asp:GridView>
Please help me
Thank you in anticipation
To bind to an ArrayList you just have to get the the underlying DataItem.
Assuming your ArrayList is storing a string you just have to do:
<asp:Label ID="" Text="<%# GetDataItem().ToString() %>"></asp:Label>
GetDataItem(): Gets the data item at the top of the data-binding context stack.
More info on MSDN.

Iterate Over Controls in Repeater

I have a bit of code that determine whether or not a control (within a repeater) should be visible or not and I want to call this on Page_Load but I can't seem to get the Controls inside a repeater.
<asp:Repeater ID="repreat" runat="server" >
<HeaderTemplate>
<asp:PlaceHolder runat="server" ID="thActivePrimary">Blah</asp:PlaceHolder>
<asp:PlaceHolder runat="server" ID="PlaceHolder1">Blah</asp:PlaceHolder>
</HeaderTemplate>
<ItemTemplate>
<asp:PlaceHolder runat="server" ID="trActivePrimary">Blah</asp:PlaceHolder>
<asp:PlaceHolder runat="server" ID="thActivePrimary2">Blah</asp:PlaceHolder>
</ItemTemplate>
</asp:Repeater>
repreat.Controls is always empty.
How do I achieve this?
foreach (RepeaterItem ri in repeat.Items)
ri.FindControl("thActivePrimary").Visible = false;
This should work
The controls are not created at page load, They are created when databind is called. If you want to access each item as they are created have a look at the DataBound event of the repeater.
Or bind the visible attribute to your datasource

Resources