I only need this one variable in the front end of the page. I am trying to achieve something like this:
<% bool YesNo = Eval("isParent") == "True" ? true : false %>
Data binding doesnt allow me to do this
Does anyone know a way around this?
Thank you for all your answers, Instead of trying to find bool value, I made the If return as text true or false and worked my way around that.(with control properties and css classes)
My Solution
Visible='<%#(string)Eval("isParent") == "True" ? false : true %>'
class="<%#(string)Eval("isParent") == "True" ? "LegendHeader" : "" %>"
the reason I did it this way is because the data is bound to the specific fields, as Vladimir Sachek mentioned, if you do it the way I wanted to you'll have to loop the data and set variable accordingly
use DataBinder.Eval instead and specify the model you are taking the data from
<% bool YesNo = DataBinder.Eval(new{isParent = "True"}, "isParent") == "True" ? true : false; %>
<%= YesNo %>
try
var YesNo = <%= Eval("isParent").ToString() %> == "True"
Try something like
<% bool YesNo(Eval("isParent").toString()) %>
Where YesNo is a method in your code behind. Like following
public bool YesNo(string sIsParent)
{
if(sIsParent.equals("1")){ return true; } else { return false; }
}
Jack said:
it gives me error Databinding methods such as Eval(), XPath(), and
Bind() can only be used in the context of a databound control.
You can use Eval only inside Repeater, GridView and similar controls. This is because Eval
uses the current data item to retrieve the requested property (the 'IsParent')
Related
I have a radio button in my .aspx page and in the code behind I have written the following:
if (rbOnlyCreatedorUpdated.Checked == true)
{
searchObject.CreatedOrUpdatedOnValidDate = SearchCritera.OnlyCreatedOrUpdated;
}
else if (rbOnlyOld.checked == true)
{
searchObject.CreatedOrUpdatedOnValidDate = SearchCritera.OnlyOld;
}
else
{
searchObject.CreatedOrUpdatedOnValidDate = SearchCritera.AllChanged;
}
And I really dislike the above. It feels clumsy and unclean. I would like a value to be returned by the radio button itself (named rbOnlyCreatedOrUpdatedOnValidDate, i.e the GroupName).
Is it possible or is the above the correct way to get the value?
If you use a RadioButtonList control, you can access the selected radiobuttons value by using IdOfRadioButtonList.SelectedValue.
This will give you a string, so you will still have to do the conversion to one of the SearchCriteria class' properties yourself...
If it makes you feel any better, you can make it into a ternary operator, like so:
searchObject.CreatedOrUpdatedOnValidDate =
rbOnlyCreatedorUpdated.Checked ? SearchCritera.OnlyCreatedOrUpdated :
rbOnlyOld.checked ? SearchCritera.OnlyOld :
SearchCritera.AllChanged;
I am setting certain controls using this
divCorporateId.Style.Add("Visibility", "hidden");
How do i check what the value is in another method?
I thought something like:
if (divCorporateId.Style........ == "hidden")
{
do something
}
Something like:
if (divCorporateId.Style[HtmlTextWriterStyle.Visibility] == "hidden")
{
do something
}
You can get Css property value in C# like this
string value = divCorporateId.Style["Key"]
then you can apply your conditions based on this value.
I have a collection of "permissions". Each permission would have three properties: Id, Name, and HasPermission. So as an example, consider the following object:
public class AccessPerm
{
int PermId {get;set;}
string PermName {get;set}
bool HasPerm {get;set;}
}
public class UserProfile
{
Collection<AccessPerm> UserPerms {get;set;}
}
So I want to use the CheckBoxFor helper to create checkboxes so that one can set the user's permissions. If you check the box, then HasPerm should be true. If you uncheck it, HasPerm should be false. The problem I am having is I don't see a way to bind both the PermId and the HasPerm properties to the checkbox. I used the following code to bind the HasPerm property, but it is not useful because I don't know the PermId.
<%
for(int ix=0; ix< Model.UserProfile.Perms.Count; ix++)
{
Html.CheckBoxFor(model => model.UserProfile.Perms[ix].HasPerm);
}
%>
This code does indeed bind HasPerm, and the value is correct. However, since I don't have the id, I can't do anything with the value. Please advise.
You could include it as hidden field:
<% for(int ix = 0; ix < Model.UserProfile.Perms.Count; ix++) { %>
<%= Html.HiddenFor(model => model.UserProfile.Perms[ix].PermId) %>
<%= Html.CheckBoxFor(model => model.UserProfile.Perms[ix].HasPerm) %>
<% } %>
This way you will get the same list in your POST controller action containing the id and whether it is selected.
You might try building a SelectList object and bind it to checkbox list.
I am new to Repeater and DataBinding and I need help using it.
In PageLoad, I have
var photos = from p in MyDataContext.Photos
select new {
p,
Url = p.GetImageUrl()
};
repeater1.DataSource = photos;
repeater1.DataBind();
In the Repeater control, I have
<ItemTemplate>
<% Photo p = (Photo) Eval("p"); %> <!-- Apparently I can't do this -->
...
<asp:TextBox runat="server" ID="txtTime" Text='<%= p.Time == null ? "" : ((DateTime)p.Time).ToString("dd/MM/yyyy HH:mm:ss") %>' />
...
</ItemTemplate>
But that is wrong.
What I need is to get the Photo object in ItemTemplate so I can do things with it (eg. to display the time as in the second line in ItemTemplate above). Is it even possible to do this in a Repeater?
Could someone point me to the right direction?
Thank you in advance!
Try something like this In the onDatabound event
if (e.Item.ItemType = ListItemType.Item)
{
photo p = (photo)e.DataItem;
Textbox txtTime = (Textbox)e.Item.FindControl("txtTime");
txtTime.text = (p.Time == null ? "" : ((DateTime)p.Time).ToString("dd/MM/yyyy HH:mm:ss"));
}
Edit -
Sorry, I didn't see the extra Url there. I looks like you might have to create a small class or struct.
See this Stackoverflow link for a hack workaround.
Paul Suart's post in that thread made a valid point.
Have you tried just:
<%# Eval("p") %>
instead of
<% Photo p = (Photo) Eval("p"); %>
I use an alternative method. In my "Register" I import the object class:
<%# Import Namespace="Test.Test.TO" %>
With this It's possible use your object...
Next, I created an object the same type I want to bound in my codebehind, global variable...
public Test test;
In my Repeater inside ItemTemplete:
<span style="display: none;"> <%# test = (Test)Container.DataItem %> </span>
Now, you can use all object's properties, inclusive ToString to format with culture...
Sorry for my english.
If i got a list of checkbox in a View, and this list came from Enum (flags). If my checkbox as all the same name, did my controller will update automaticly my Enum (flags) values in my ViewModel with multiple selection ?
Suppose i get in my View
<% foreach (var t in Enum.GetValues(typeof(FoodType)))
{
Response.Write(t.ToString() + " ");
%>
<input type="checkbox" name="TypeOfFood" value="<%:(int)t %>" />
<% }%>
My Controller working like this
public ActionResult Manage(FoodEntity food)
{
}
If i check many check box when i look then FoodType property in my foodEntity, only the value of the first checkbox is selected, but my enum is a flag... what i need, if i want support flag ?
thanks.
Unfortunately no.
It will just grab the first checked value and assign that to your value field.
That would be a pretty cool feature though.
Heres a quick way to get the value you're looking for back into your model:
int newEnumValue = Request.Form["CheckBoxField"].Split(',').Aggregate(0, (acc, v) => acc |= Convert.ToInt32(v), acc => acc);