Passing data from listview controls - asp.net

My listview is displaying data for a product item. In my template I want to display data based on the id of a product by calling a function that takes the id as a parameter. How is this done?

If it is .NET 2.0 or above you can use this:
<ItemTemplate>
<%# SomeFunction((int)Eval("myIdColumn")) %>
</ItemTemplate>
Note that the SomeFunction must be protected or 'higher' for this to work at all.

You should be able to do something like this:
<ItemTemplate>
...
...
<%# myFunction((int)DataBinder.Eval(Container.DataItem, 'myIdColumn')) %>
...
...
</ItemTemplate>
You custom function (myFunction() in the example above) should return a string.

Related

using values within if conditions in mark up

I want to use a value that is pulled from the SQL within the if statement. Ideally i want to do the equivalent of
<% If DataBinder.Eval(Container, "DataItem.BookID") == 1 Then%>
Is there a way to do this with the correct syntax?
This is how you put conditions in aspx file. Just a rough sample base on what I understand:
<%# System.Convert.ToInt32((DataBinder.Eval(Container.DataItem, "BookID")!="") ? DataBinder.Eval(Container.DataItem, "BookID"):0) %>
Make sure you have int in BookID not any other type.
Explaining Further:
In case you want to have an if else condition:
<%# If DataBinder.Eval(Container.DataItem, "DATAFIELD") <> "" Then
Response.Write("something")
End If %> // This is invalid
The above statement can be properly written in aspx file as this:
<%# DataBinder.Eval(Container.DataItem, "DataField").Equals("")?"":"Something"%>
I'm not sure if this can be done or not the way you are requesting it.
As you may or may not know, the typical way to do this is to have a control in your markup, like so
<asp:listView ID="SophiesListView" ...
..
<ItemTemplate>
<asp:HyperLink ID="hlGlossary" title="click here for more information" target="_blank" runat="server" />
</ItemTemplate>
</asp:listView />
Then, in the codebehind, find your listview / repeater / datagrid or what have you and choose ItemDataBound. Inside this event, do something like this:
If e.Item.DataItem("vehicleType") IsNot DBNull.Value AndAlso e.Item.DataItem("vehicleType") = "JETSKI" Then
DirectCast(e.Item.FindControl("hlGlossary"), HyperLink).NavigateUrl = "Glossary.aspx#JETSKI"
DirectCast(e.Item.FindControl("hlGlossary"), HyperLink).Text = "?"
End If
To keep your page logic as simple as possible your best bet is to data bind to the Visible property of controls. For example, if you want to only show some html if the BookID == 1 then create a new property on your data source like this
public bool Show
{
get
{
return BookID == 1;
}
}
and in your page you'd have
<asp:Placeholder runat="server" Visible='<%# Eval("Show") %>'>
...html goes here...
</asp:Placeholder>

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); %>

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();

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.

Is it possible to use Data-Binding Expressions directly in markup to show/hide content?

I would like to do something like:
<%# if((bool)Eval("IsDisabled")){ %><span>Disabled</span><% }
else { %><span>Active</span><% } %>
but I don't think its possible.
There is a way to create method in codebehind which returns appropriate string and call it, but thats not an option.
You can use placeholders to hold the two versions of your markup and then use the Visible property to show the relevant one. Something like this... Note the use of ! before the call to IsDisabled in the second Visible property.
<asp:PlaceHolder ID="PlaceHolder1" runat="server" Visible='<%# IsDisabled((bool) Eval("IsDisabled")) %>'>
<span>Disabled</span>
</asp:PlaceHolder>
<asp:PlaceHolder ID="PlaceHolder2" runat="server" Visible='<%# !IsDisabled((bool) Eval("IsDisabled")) %>'>
<span>Active</span>
</asp:PlaceHolder>
The code behind IsDisabled method looks like this...
public bool IsDisabled (bool isDisabled)
{
return isDisabled;
}
Its not possible to use # eval in if statement,
You have some options to solve that:
You can put the condition of the if in a previous line then check on this variable in the if
example:
in code behind:
protected bool isDisabled;
in aspx:
<%# isDisabled=(bool)Eval("IsDisabled") %>
<% if(isDisabled) %>
Other way is to call a code behind method which return bool and check on it in the if.

Resources