asp.net c# using if conditions with Bind - c#

with asp.net c# I get the code of a country with bind :
<asp:Label runat="server" Text='<%# Bind("h_country") %>' ID="Label1"></asp:Label>
this return code of country like usa, tr, eg, il.
I need to put if conditions and I try to do it like this:
<asp:Label runat="server" Text='<%# if(Bind("h_country")=="usa") resonse.write("United States"); %>' ID="Label1"></asp:Label>
note: I'm using GridView and template.
I tried alo at code behind to get the value of country like this:
String s = Lable1.Text;
but also not workes!
how can I get it as a variable and use if condition ?

You need to use find control if thus label is within the gridview and then get the sting from label.

You probably want to handle this in the code behind
<asp:Label runat="server" Text='<%# GetCountry(Eval("h_country")) %>' ID="Label1"></asp:Label>
code behind
public string GetCountry(object country)
{
if (county.ToString() == "usa")
return "United States";
}
It would be better if you had a lookup so you don't have to have a lot of if statements

This is how you can do conditional checks in the markup while binding. Try something like below but I would not advise doing this kind of conditional checks and response.write at the template level but do it in code behind.
<asp:Label runat="server" Text='<%# Bind("h_country") == "usa" ? "United States" : (string)Bind("h_country") %>' ID="Label1"></asp:Label>
And this is how you can do it at code behind. Use the databound event to find the Label1 and set the appropriate text in there.
You need to define OnRowDataBound="gvTest_DataBound for the gridview in the markup
protected void gvTest_DataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label lblCountry = (Label)e.Row.FindControl("Label1");
if (lblCountry.text == "usa"){
// do something here
}
else {
// do something otherwise
}
}
}

Related

Aspx C#: How to get result of Eval(...) in grid_HtmlRowCreated?

I'm trying to use a parameter from .aspx in C# code inside grid_HtmlRowCreated method.
I tried it by setting a text in a label in .aspx and then fetch that text from the label.
It worked:
aspx
<dx:ASPxLabel runat="server" ID="cl" Text="Some text"/>
...
protected void grid_HtmlRowCreated(object sender, ASPxGridViewTableRowEventArgs e)
{
if (e.RowType == DevExpress.Web.ASPxGridView.GridViewRowType.Data)
{
...
string id;
if (comments_grid != null && expIDControl != null)
{
id = ((DevExpress.Web.ASPxEditors.ASPxLabel)expIDControl).Text;
System.Windows.Forms.MessageBox.Show( id );
}
}
But when I set the value like:
<dx:ASPxLabel runat="server" ID="cl" Text='<%# Eval("Id") %>'/>
then id is empty and when I set
<dx:ASPxLabel runat="server" ID="cl" Text='<%# Eval("Id") %> + " test"'/>
then id is literally <%# Eval("Id") %> test
Thank You for any ideas...
EDIT: That works as well, the code just need to be in HtmlRowPrepared, not in HtmlRowCreated. Thanks guys!
You can write a method on the class (.aspx.cs) that will concatenate.
Going from memory:
Text='<%# ConcatSomething (Eval("Id") , "Peanut" ) %> '/>
and then
public object ConcatSomething(object x , object y)
{
return Convert.ToString(x) + Convert.ToString(y);
}
or maybe
public object ConcatSomething(string x , string y)
{
return x + y;
}
You'll have to play with it. I've done it a thousand times on data_binding.
The stinker is the "object" stuff.
And you'll have to code for null values (coming in) if you may have some.
I just copied this from my current project , I have the same Grid with Item template and the correct way to do it is :
<dx:ASPxLabel runat="server" ID="cl" Text='<%# DataBinder.Eval(Container.DataItem, "Id") %>' />
regards

OnItemCommand function not working with asp Linkbutton in Listview

I´m having some wierd issus with my asp:Linkbutton functionality in my asp:ListView.
Here is my code:
<asp:ListView ID="lvData" runat="server" OnItemCommand="lvData_ItemCommand" OnItemDataBound="lvData_ItemDataBound">
<LayoutTemplate>... </LayoutTemplate>
<ItemTemplate>
...
<td>
<asp:LinkButton ID="ItemLink" runat="server" CommandName="View" Text='<%# Eval("NameOfBatch")%>'></asp:LinkButton>
</td>
...
my code-behind is like this:
protected void lvData_ItemCommand(object sender, ListViewCommandEventArgs e)
{
string smu = "";
}
if I put a breakpoint on string smu it never goes there.
The only thing that happens is that my table dissapears and nothing else.
Do you have any ideas ?
Set the CausesValidation property to false on the controls if you have validation on the form and don't want them to trigger the validation.

How to display object's List property in listview

I'm currently having a problem in displaying some stuff in ListView. I have a normal C# Student class and a Classroom class. In Classroom class contains a List of Student classes which is shown below:
public class Classroom
{
int classroomid {set;get;}
List<Students> students{set;get;}
}
My question is if I bind my DataSource of the ListView to a List of objects of Classroom, how do I render the Student lists in the ListView?
<ListView>
<ItemTemplate>
<asp:Label Text='<%# Eval("classroomid") %>'></asp:Label>
<asp:Label Text='<%# Eval("students") %>'</asp:Label>
<asp:Label Text='<%# Eval("students.name") %>'</asp:Label>
</ItemTemplate>
</ListView>
The code above essentially explains that what I'm trying to achieve, or possibly displays the student's details such as name or etc. Is there any way to achieve that? I do understand that nested ListView works, but I'm not sure how is the implementation.
Thank you for your help in advance. :-)
You could use string.Join:
<asp:Label runat="server" ID="LblStudents"
Text='<%# string.Join(",", (List<string>)Eval("students")) %>'>
</asp:Label>
Edit: I missed that Student is a custom type. So you should better do this in the ListView's ItemDataBound event for readability.
But it should work also on aspx:
<asp:Label runat="server" ID="LblStudents"
Text='<%# string.Join(",", ((List<Student>)Eval("students")).Select(s=>s.Name)) %>'>
</asp:Label>
in codebehind:
protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
{
if (e.Item.ItemType == ListViewItemType.DataItem)
{
Label LblStudents = (Label)e.Item.FindControl("LblStudents");
Classroom cr = e.Item.DataItem as Classroom;
if (cr != null && cr.students != null && cr.students.Count > 0)
{
LblStudents.Text = string.Join(",", cr.students.Select(s => s.Name));
}
}
}

DataBinder.Eval in c#

Hi anybody know how to use databinder.eval in c#
Actually I've tried this
LinkButton lnkName = new LinkButton();
lnkName.CommandArgument = DataBinder.Eval("object","<%#COURSE_ID%>");
it is showing error. Whats the wrong with this?
You can't use Eval in the code behind of an aspx page.
this:
lnkName.CommandArgument = DataBinder.Eval("object","<%#COURSE_ID%>");
should be this:
lnkName.CommandArgument = YOUR_OBJECT_PROPERTY_HERE;
To fill in YOUR_OBJECT_PROPERTY_HERE you either need to specify the object.property etc like normal in C# code, or you'll have to use reflection to get the property value from the object (which is what eval does for you).
Here is a link showing how to use reflection to get the property information from an object. You can use it to duplicate how eval works if you need to: https://web.archive.org/web/1/http://articles.techrepublic%2ecom%2ecom/5100-10878_11-6099345.html
Link to DataBinder Eval Method: http://msdn.microsoft.com/en-us/library/4hx47hfe.aspx
How the DataBinder Eval Method works (and why the author thinks it should be avoided) http://weblogs.asp.net/jgalloway/archive/2005/09/20/425687.aspx
For Example in design page you can use like:
<asp:Button ID="btnEdit" CommandName="Edit"
CommandArgument='<%# DataBinder.Eval(Container, "RowIndex") %>'
CssClass="cursor_hand" runat="server" Text="Edit" />
Code Behind:
int rowIndex = int.Parse(e.CommandArgument.ToString());
if (e.CommandName.Equals("Edit"))
{
//do something
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowIndex > -1)
{
string h = DataBinder.Eval(e.Row.DataItem, "ColumnName").ToString();
}
}
You should use Eval expression and <% %> in *.aspx code not with C# code.

define variable in repeater?

I like to handle and compare a lot of date times in my repeater even I have to work more than one time with the same.
It's a bit ugly, to cast everywhere the Eval("MyDate") like ((DateTime)Eval("MyDate")) to substract 2 datetimes or to compare it, even if you have to do this more than in one operation.
I thought of saving all the evals in a var at start of the repeater?
DateTime mydt1 = Eval("myDate");
DateTime mydt2 = Eval("mydate");
after that, it's easy to do any operations in the whole repeater. Hope you understand my idea. Is this possible? I tried short but everytime errors.
mydt1 - mydt2....
Thank you and best regards.
You could call a method on the code behind page from the repeater using the DateTimes as arguments. The casting logic can be done in the code behind if the goal it to create a cleaner looking aspx page.
Example ASPX:
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<asp:Literal
ID="Literal1"
runat="server"
Text='<%# DateFoo(Eval("myDate1"), Eval("myDate2")) %>' />
</ItemTemplate>
</asp:Repeater>
Example C# code behind:
protected string DateFoo(Object o1, Object o2)
{
DateTime? dt1 = o1 as DateTime?;
DateTime? dt2 = o2 as DateTime?;
// Do logic with DateTimes
return "string";
}
If you want to add more logic to your repeater I would suggest you move the binding logic to the code behind:
ASPX:
<asp:Repeater id="myRepeater" runat="server">
<ItemTemplate>
<asp:Literal id="myLiteral" runat="server" />
</ItemTemplate>
</asp:Repater>
CS:
protected override void OnInit(EventArgs e)
{
myRepeater.ItemDataBound += myRepeater_ItemDataBound;
base.OnInit(e);
}
void myRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
// this method will be invoked once for every item that is data bound
// this check makes sure you're not in a header or a footer
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
// this is the single item being data bound, for instance, a user, if
// the data source is a List<User>
User user = (User) e.Item.DataItem;
// e.Item is your item, here you can find the controls in your template
Literal myLiteral = (Literal) e.Item.FindControl("myLiteral");
myLiteral.Text = user.Username + ", " + user.LastLoginDate.ToShortDateString();
// you can add any amount of logic here
// if you need to use it, e.Item.ItemIndex will tell you what index you're at
}
}
I hate evals with a passion.
This is why I use this code to be rid of them forever and go back to strong typing:
public static class DataItemExtensions
{
public static T As<T>(this IDataItemContainer repeater) where T : class
{
return (T)repeater.DataItem;
}
public static dynamic AsDynamic(this IDataItemContainer repeater)
{
return repeater.DataItem;
}
}
Then use it like this:
<asp:Repeater runat="server" DataSource="<%# this.MyObjectCollection %>">
<ItemTemplate>
<%# Container.As<MyObject>().DateTime %>
</ItemTemplate>
</asp:Repeater>
Note that if you use the Datasource like I did, you need to use this.DataBind() on the page.

Categories