<%# ((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.
Related
I have a gridview that I populate with List<>.
GridView's columns are TextBoxes (as TemplateField).
The list might contain objects from 2 different custom classes whose fields to display are not exactly the same.
For class1, I need to display in GridView:
class1.Name
class1.field1
For class2, I need to display in GridView:
class2.Name
class2.field2
So I can setup the gridview into the aspx so that it displays the class1 items:
<asp:GridView ID="DG_Table" runat="server" style="z-index: 1;
autogeneratecolumns="False"
onrowcommand="DG_Table_RowCommand"
<Columns>
<asp:TemplateField HeaderText="Name" >
<ItemTemplate>
<asp:TextBox ID="Name" runat="server" Text='<%# Bind("Name") %>'></asp:TextBox>
</ItemTemplate>
<asp:TemplateField HeaderText="field1" >
<ItemTemplate>
<asp:TextBox ID="field1" runat="server" Text='<%# Bind("field1") %>'></asp:TextBox>
</ItemTemplate>
</Columns>
</asp:GridView>
Displaying a List<Class1> into the DataGrid works fine.
Now, if I want to display items of class2 into the datagrid, I will do something like:
DG_Table.DataSource = new List<Class2>;
DG_Table.DataBind();
This will obviously cause this kind of error :
An exception of type 'System.Web.HttpException' occurred in System.Web.dll but was not handled in user code
Additional information: DataBinding: 'Class2' does not contain a property with the name 'Field1'.
So I suppose that, before binding to the List, I need to convert my second column into the code behind.
What would be the equivalent of
Text='<%# Bind("field1") %>'
into the code behind?
I expected something like this :
((TextBox)DG_Table.Columns[2]).Text = "";
But this conversion is not allowed.
Thx in advance.
You can try using the anonymous type to bind the data just before the binding the data convert them to the anonymous types and then bind them.
Change your grid view as,
<asp:GridView ID="DG_Table" runat="server" style="z-index: 1;autogeneratecolumns="False" onrowcommand="DG_Table_RowCommand">
<Columns>
<asp:TemplateField HeaderText="Name" >
<ItemTemplate>
<asp:TextBox ID="Name" runat="server" Text='<%# Eval("Name") %>'></asp:TextBox>
</ItemTemplate>
<asp:TemplateField HeaderText="field1" >
<ItemTemplate>
<asp:TextBox ID="field1" runat="server" Text='<%# Eval("FieldValue") %>'></asp:TextBox>
</ItemTemplate>
</Columns>
</asp:GridView>
For binding your first class,
gridView1.DataSource = class1List.Select(x=> new{Name = x.Name,FieldValue = x.Field1});
gridview1.DataBind();
When binding the second class list
gridView1.DataSource = class2List.Select(x=> new{Name = x.Name,FieldValue = x.Field1});
gridview1.DataBind();
Your problem is in the second TextBox it Binds or Evals "field1" which is not a property in Class2.
You can do as same as it was mentioned in the #code of code answer or just Change the Create a Model class for the View to bind it
Class GvItemModel
{
public string Name {get; set;}
public string Field{get; set;}
}
And past it always as dataSource
var dataSource = new List<CvItemModel>();
... //Load class1 or Class2 it depends on your choice
Then
DG_Table.DataSource = dataSource ;
DG_Table.DataBind();
Change your second TextBox dataSource.
<asp:TemplateField HeaderText="field1" >
<ItemTemplate>
<asp:TextBox ID="field1" runat="server" Text='<%# Bind("Field") %>'></asp:TextBox>
</ItemTemplate>
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
I have a GridView that is made up of two database fields populated via a stored procedure and then three fields for user input (two checkbox controls and one textbox) when I click on the save button I can get the information from the three controls but nothing from the two that were populated via the database. How can I get the first two fields?
<asp:GridView ID="gvA1" runat="server" AutoGenerateColumns="false" DataKeyNames="CodeNo" AutoGenerateSelectButton="false" EnablePersistedSelection="True" Visible="false">
<Columns>
<asp:TemplateField Visible="false" >
<ItemTemplate>
<%#DataBinder.Eval(Container.DataItem, "CodeNo")%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-Wrap="true" ItemStyle-Width="400px" HeaderText="Violation">
<ItemTemplate>
<%#DataBinder.Eval(Container.DataItem, "CodeViolationCited") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="A1Accordion_cbPool" runat="server" Text="Pool:" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="A1Accordion_cbSpa" runat="server" Text="Spa:" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Additional Comments" Visible="true">
<ItemTemplate>
<asp:TextBox ID="A1Accordion_tb" runat="server" TextMode="MultiLine"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Code Behind:
protected void SaveAndCollapseA1(object sender, EventArgs e)
{
//good stuff
CheckBox myCheckBox_1 = gvA1.Rows[0].FindControl("A1Accordion_cbPool") as CheckBox;
CheckBox myCheckBox_2 = gvA1.Rows[0].FindControl("A1Accordion_cbSpa") as CheckBox;
TextBox myTextBox = gvA1.Rows[0].FindControl("A1Accordion_tb") as TextBox;
//not so good stuff
String myString1 = gvA1.Rows[0].Cells[0].ToString();
String myString2 = gvA1.Rows[0].Cells[1].ToString();
}
I figured it out but I haven't been hear long enough to post the solution via the links but if you change the columns as so:
<asp:label ID="lblA1CodeNo" runat="server" text='<%# Eval("CodeNo") %>'></asp:label>
then the values are available...
Try:
<%# Bind("CodeViolationCited") %>
Instead of
<%#DataBinder.Eval(Container.DataItem, "CodeViolationCited") %>
Eval is one way, bind is two-way.
See Microsoft's documentation for data-binding expressions
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
I would like to have a repeater control bound to method and display the result as a list of linkbuttons, but I can't get by head around it. This is what I've tried:
In the asp page I have:
<asp:Repeater ID="resultCountRepeater" runat="server" Visible="false" >
<ItemTemplate>
<asp:LinkButton ID="userResultCount" runat="server" OnClick="userResultCount_Click" Text="<%# DataBinder.Eval(Container.DataItem,"Text") %>" >
</asp:LinkButton>
</ItemTemplate>
</asp:Repeater>
And in the code behind:
List<ListItem> resultCountList = new List<ListItem>();
foreach (ISearchEngine oneEng in engines)
{
ListItem item = new ListItem();
item.Text = oneEng.ObectName();
item.Value = Convert.ToString(oneEng.PageCount(searchWords, townId));
resultCountList.Add(item);
}
resultCountRepeater.DataSource = resultCountList;
resultCountRepeater.DataBind();
Unfortunately this is giving me a compile error: The server tag is not well formed.
Any ideas what it wrong?
Thanks
Use single quotes to dynamically set properties.
<asp:LinkButton ID="userResultCount" runat="server" OnClick="userResultCount_Click" Text="<%# DataBinder.Eval(Container.DataItem,"Text") %>" ></asp:LinkButton>
Should be
<asp:LinkButton ID="userResultCount" runat="server" OnClick="userResultCount_Click" Text='<%# DataBinder.Eval(Container.DataItem,"Text") %>' ></asp:LinkButton>