ItemType when datasource is datareader - asp.net

I want to set the ItemType in my repeater, but my datasource is a datareader, not a class.
How can I do thats?
Help me please.. Thank

If the DataSource is a DataReader, your ItemType needs to be either a DbDataRecord (which implements IDataRecord) or better, IDataRecord directly.
Example (assuming you are getting an Id column back):
<asp:Repeater ID="rpt" ItemType="System.Data.IDataRecord" runat="server" >
<ItemTemplate>
<%# Item.GetInt32(Item.GetOrdinal("Id")) %>
</ItemTemplate>
</asp:Repeater>
But I think the above is probably worse than simply doing Eval or Cast. I can see the benefit when you are binding to a custom business object but not when you are using a DataReader.

Related

A tricky way to using <%# Bind %> in ASP.NET

In ASP.NET, I can use <%# Bind %> to achieve two-way data binding, then the data source control can complete update function for me.
However, I think it's only easy when you just show simple format, like
<%# Bind("InsertDate") %>
But, if I want to show 'N/A' when InsertDate is not exist. then how to use <%# Bind %> to achieve the condition check?
I know Bind function supports format string, like
<%# Bind("InsertDate", "0:{dd MMM yyyy}") %>
But it cannot change format base on InsertDate's content.
I even try to use:
<%# FormatString(Bind("InsertDate").ToString()) %>
It seems's not working which normally works for Eval function.
Another example is I have a bitwise column in table (like 5) which is represented by a checkboxlist wrapped by ListView control, like
<asp:checkboxlist>
<asp:ListItem value="1" text="OptionA">
<asp:ListItem value="2" text="OptionB">
<asp:ListItem value="4" text="OptionC">
</asp:checkboxlist>
then how to use <%# Bind %> to achieve two-way bind to bind the bitwise column to this checkboxlist?
Currently what I do is:
set checkboxlist's selected item(value) in ListView's ItemDataBound event handler
using a HiddenField and bind the data to this hiddenfield to
achieve two-way data bind.
In LivtView's ItemUpdating event handler, I update above ListViewUpdateEventArgs' NewValue property to the value I want. and then the EntityDataSource can do the update operation correctly.
But I think this method is not easy and good.
So is there any other better method to do this?
Try to make use of the public data type.
On your code-behind declare your variable as
public string name;
//Do whatever assignment operations to your variable "name"
And on your aspx page. You can call it
<% Response.Write(name); %>

How do I bind a GridView to a custom object?

If I have the following ASP.NET code (it's not complete - obviously there's a lot missing, but none of it matters):
<asp:GridView>
<Columns>
<asp:TemplateField>
<ItemTemplate>
My Label: <asp:Label />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
My Text Box: <asp:TextBox />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
And if I have something like this in the CodeBehind:
Private MyListOfObjects As List(Of MyObject)
...
Public Class MyObject
Public MyTextBoxString as String
Public MyLabelString as String
End Class
How can I bind the GridView so that one row is equivalent to one item in my MyListOfObjects list, and so that the data will populate and persist across page loads or postbacks? I've never done custom databinding like this before, so a full explanation would be very helpful. All the tutorials I've come across so far only talk about using GridViews directly with Database query results, and that's not what I need.
Thanks!
Just set the datasource of the gridview to your object.
MyGridView.DataSource = myList
MyGridView.DataBind()
Here's a very similiar post:
Binding a method that returns List<employee> to a gridview
Looks like you are using a list in vb.net. Remember lists can hold integers, strings, dates, objects (these include user defined types (your object)).
So you can bind a gridview to a list object by setting the datasource property to your list.
In the above example, myList, might hold a ton of employee objects, etc. So assign it to the datasource and .DataBind() and voila a gridview with each row containing your object.
You can do something like
My Label: <asp:Label id="myLabel" runat="server" Text='<%# Eval("MyTextBoxString") %>' />
in the markup and similar stuff for your textbox.
GridView1.DataSource = MyListOfObjects
GridView1.DataBind()
First remember any binding controls like GridView, DropdownList e.t.c bind to public properties, so first make your public members to public properties.
Then create objects of MyObject class and add them to your List<MyObject> object
Finally you can persist this list object by saving it in Session or ViewState to maintain it after postbacks.
I hope you can do it now!!! you can ask for more help if neccessary.

Totally lost – data binding expressions inside GridView’s template

1) On aspx page we define GridView control named gvwPolls, and inside its template we define a user control named pollBox1
<asp:GridView ID="GridView1" DataSourceID="objPolls" ...>
<Columns>
<asp:TemplateField>
<ItemTemplate>
Question is : <%# Eval("QuestionText") %> <br />
<mb:PollBox ID="PollBox1" runat="server" PollID='<%# Eval("ID") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:ObjectDataSource ID="objPolls" ...></asp:ObjectDataSource>
a) I assume that inside gvwPolls’s template, the gvwPollBox1.DataBind is called before PollID='<%# Eval("ID") %>' and <%# Eval("QuestionText") %> expressions get evaluated?!
b) Can someone offer some explanation how or why is gvwPollBox1.DataBind called before PollID='<%# Eval("ID") %>' and <%# Eval("QuestionText") %> expressions get evaluated?
2) Continuing with the above example:
-- pollBox1 user control defines a Repeater control named rptOptions:
<asp:Repeater runat="server" ID="rptOptions">
<ItemTemplate>
<%# Eval("pollBoxTitle") %>
</ItemTemplate>
</asp:Repeater>
-- In pollBox1’s code-behind file we bind rptOptions to a data source inside DoBinding() method.
-- We also override pollBox1’s DataBind() method:
public override void DataBind()
{
base.DataBind();
DoBinding();
}
a) I assume that due to overriding pollBox1.DataBind(), the data binding expression <%# Eval("pollBoxTitle") %> ( defined inside rptOptions’s template ) will get evaluated prior to a call to DoBinding method? If so, won’t then <%# Eval("pollBoxTitle") %> get evaluated before rptOptions is actually bound to a data source?
b) If that is the case, how then is rptOptions able to extract value ( from data source’s pollBoxtitle property) from a data source, if at the time the <%# Eval("pollBoxTitle") %>
expression got evaluated, rptOptions wasn’t yet bound to any data source?
thanx
I can't explain why the page life cycle is the way that is, probably has something to do with rendering childs before the parent object. When exactly do you call .DataBind() in the PollBox control? Try to move it into an event that is later in the life cycle, like PreRender.
There is also another way to ensure it is working the way you want to:
Subscribe to the RowDataBound Event, use .FindControl("YourPollBoxID") to get the instance of the control current bound row, set the properties and perform a manuall .DataBind();

Databinding exception with entity navigation property

I have two Entity classes: Order and OrderItem. Order contains a navigation property OrderItemSet of type
System.Data.Objects.DataClasses.EntityCollection<OrderItem>
On an aspx page is a FormView bound to this EntityDataSource:
<asp:EntityDataSource ID="EntityDataSourceOrder" runat="server"
ConnectionString="name=EntitiesContext"
DefaultContainerName="EntitiesContext"
EntitySetName="Order"
Include="OrderItemSet"
// stuff to define a query
</asp:EntityDataSource>
The FormView is bound to the DataSource and the ItemTemplate of this FormView contains a ListView which I try to bind to the OrderItemSet. It looks this way:
<asp:FormView ID="FormViewOrder" runat="server" DataKeyNames="OrderID"
DataSourceID="EntityDataSourceOrder" AllowPaging="True" >
<ItemTemplate>
...
<asp:ListView ID="ListViewOrderItems" runat="server"
DataSource='<%# Eval("OrderItemSet")%>' >
...
</asp:ListView>
</ItemTemplate>
</asp:FormView>
When I run the application I get an exception pointing to the line DataSource='<%# Eval("OrderItemSet")%>' in markup and telling me:
DataBinding: System.Web.UI.WebControls.EntityDataSourceWrapper does not contain a property with name 'OrderItemSet'
What is wrong here?
(I've done the same with other navigation properties which are not lists but single object references, and that works.)
Thank you for help!
It seems to me that you are trying to evaluate a collection from within a datasource, without first binding to that datasource.
Why don't you try binding directly to the datasource? E.g.
<asp:ListView ID="ListViewOrderItems" runat="server"
DataSourceID="EntityDataSourceOrder"
...
</asp:ListView>

nested repeater pass value in header template

I have a nested repeater and i want to pass value in its header. Here is my code so far..
The main problem is the id of the control in header template is also coming from code behind.
<asp:Repeater ID="RptrProgCategory" runat="server">
<ItemTemplate>
<asp:Repeater ID="RptrPrograms" runat="server">
<HeaderTemplate><input type="hidden" id="<%= questvalue%>"/></HeaderTemplate>
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "cat") %>
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:Repeater>
I want value in questvalue from code behind. Any idea how to achieve this?
Edit: I wanted to put this value in a DataTable and bind that value in Repeater bcoz i want output like this may be <%# DataBinder.Eval(Container.DataItem, "questvalue") %> instead of <%= questvalue%>..but in tht case i am not able to find the control
Category1(id of hidden field )
subcat1
subcat2
subcat3
Category2(id of hidden field)
subcat4
subcat5..and so on..
Repeater mainRepeater = this.Page.FindControl("RptrProgCategory") as Repeater;
Repeater nestedRepeater = mainRepeater.FindControl("RptrProgCategory") as Repeater;
You can then do a FindControl in nestedRepeater for questValue.
Add a runat='server' to questvalue so that you can access it in code behind.
I am writing this from memory, syntax might not be correct but it should get you off in the right direction.
set the id of the control in the repeater to something like mycontrolId - and then on OnItemDataBound - (or even OnItemCreated) use findcontrol("mycontrolId") - and then change the id of the control to your questvalue param.

Resources