Click here to Skip to main content
16,012,015 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have an SQLDataSource control, which is bound to an ASP.net
GridView control. It outputs a list of user roles that a admin can give status like user,admin or disabled
In my gridview, I have a column called "Status" which is bound to
a column in my database, called "isCompanyAdmin". A zero means User, and a
one means Admin status.

Showing a 0 or 1 is ugly and I want to use USER or Admin. I can't figure
out how to get at my SQLDataSource control's rows so that I can look at
them and write text out, rather than what's in the database (a 0 or 1).

My code is like this:

C#
<asp:Label ID="lbStatus" runat="server" text='<%# Eval("isCompanyAdmin")
%>' />

I've tried assiging the return value of Eval("isCompanyAdmin") to a local
variable, but I get asp errors telling me that Eval must be used in a
databound control.

I want to do something like this:

If Eval("CheckedOut") = "0" Then
...
Else
...
End Of


I can post real code if that helps- but it's more the approach I am
looking for...

Thanks for any help
Posted
Updated 29-Feb-12 1:40am
v2

Make a static method somewhere to convert the boolean into a string.
Then try something like this

ASP.NET
<asp:label id="Label2" runat="Server" text="<%#YourNamespace.SomeClass.MethodName((System.Boolean)Eval("isCompanyAdmin")) %>"/>
 
Share this answer
 
v2
i have done this solution partially to use this
public string checkstatus(int status)
{
if (status == 1)
{
return "Admin";
}
else if (status == 0)
{ return "User"; }
else {
return "Disabled";

}


aspx page//

<ItemTemplate>
<asp:Label ID="lbstatus" Text='<%#checkstatus(Convert.ToInt32(Eval("IsCompanyAdmin"))) %>' runat="server" />
</ItemTemplate>


But now i have problem arise
in my database there are two different column IsComapnyAdmin and IsDeleted
if IsCompanyAdmin IsDeleted ==status
0 0 then User

1 0 then admin
0 1 then disabled

how i can use two Eval and how can i compare and solve this complex problem
i want to print user,admin,disabled value when gridview load and its value like above two column...
Please Help me
 
Share this answer
 
Try to Use Grid View OnRowDataBound event Write a event handler to this event and write you condition in that event handler and assign the resultant to the label as below

in the event handler the coding is as like below

protected void grdVw_OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label lblStatus = (Label)e.Row.FindControl("lblStatus");
int isDet = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem,"IsDeleted"));
int isAdm = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "IsCompanyAdmin"));
//Now Compare these two variables as your criteria and assign the final value to label as below

lblStatus.Text = "Your Result";

}
}
 
Share this answer
 
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{

if (e.Row.RowType == DataControlRowType.DataRow)
{
int i;
Label lblRole = (Label)e.Row.FindControl("lblSup");
i = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "RoleId"));
if (i == 1)
lblRole.Text = "Admin";
else
lblRole.Text = "User";
}
}

lblSup -- Label Name which you give in ItemTemplate
RoleId -- DataColumn Name
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900