Dynamic properties of object in c# webform - c#

I have a n properties of ShowSeat Class: SeatCol1Status , SeatCol2Status ..., SeatColnStatus
I want list value of this to view, but I don't like
<%= ShowSeats[rowIndex].SeatCol1Status %>
<%= ShowSeats[rowIndex].SeatCol2Status %>
..................
<%= ShowSeats[rowIndex].SeatColnStatus %>
How do generate it with loop in html of webform

You can add an indexed property that you can then loop over (guessing at the return type):
public SeatStatus SeatStatus[int col]
{
get
{
switch(col)
case 1: return SeatCol1Status;
case 2: return SeatCol2Status;
// etc
}
}
But maybe you shouldn't have made "n" specific properties but just a single one, containing a list of "seat statusses" that you can index into (and easily iterate over). And this also adapts easily when not all rows have the same number of seats.

Related

dropdownlist inside a for loop asp.net

to build a dropdownlist inside for loop :
<%
for (int i = 0 ; i < 3 ; i++)
{
%>
<ASP:Dropdownlist id="list" .... ></ASP:Dropdownlist
<%
}
%>
it would create three drop down list with the id = list
but how to get the value of the list from c# code behind
like :
string x = list.SelectedValue;
The Question is : how to specify which one of those list I want to select it's value ?
P.S :
- I need to build it in asp.net not code behind because its a table and so complicated
- without using a repeater

How to pass list as parameter to a method in .aspx page

I must bind a gridview to a method which returns list<>, there are few multi select lookups in the list, for this I have a method which returns dictionary<int, string>.
In the list I have the multi select item values as {1, xyz}, {2, abc}
So I must display in the grid as xyz, abc.
For this I have written a method FormatString which is called in gridview binding.. i.e aspx page
<%# FormatString(?????????) %>
I must pass list<> in the ???? to retreive the data..
Please suggest me some solutions..
'<%# FormatString(Container.DataItem as YourListType) %>'
public String FormatString(YourListType listObj )
{
//do what you want.
}
You can try this way
Get the values to a different list from your dictionary items
var items = yourDictionary.Values.SelectMany(x => x).ToList();
Then you can simply bind the list to gridview

Passing entire Container.DataItem row to code behind

This question relates to ASP.Net 3.5 and C#
I'm building an RSS Feed from a database containing a lot of columns.
I need to format the data in a particular node and doing it inline is going to be very messy.
I'm aware I can pass all the parameters individually to a subroutine
<%# formatAddress(Container.DataItem["propertyName"],
Container.DataItem["propertyNumber"], ... ) %>
As there's going to be up to 20 of these columns, I'd rather pass the entire row.
<%# formatAddress(Container.DataItem) %>
This would be ideal then I can pick out the columns I want in code behind:
protected string FormattedAddress(object row)
{
DataRowView data = (DataRowView)row;
StringBuilder Address = new StringBuilder();
string Postcode = data["propertyPostcode"];
...
return Address.ToString();
}
I'm getting the error Unable to cast object of type 'System.Data.Common.DataRecordInternal' to type 'System.Data.DataRowView'.
Previously, I was using protected string FormattedAddress(DataRowView row) but that didn't work either.
Any clues?
Eventually found a couple of examples which led me to realise I should be casting to DbDataRecord.
I'm still passing <%# formattedAddress(Container.DataItem) %> but my function is now as follows:
protected string FormattedAddress(object dataItem)
{
DbDataRecord data = (DbDataRecord)dataItem;
string Postcode = data.GetValue(
data.GetOrdinal("propertyPostcode")).ToString();
...
return NicelyFormattedAddress;
}
Is this the best way to handle the data?

How to bind CheckBoxFor

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.

Enum with mvc rendering in checkbox on my view, reaction of my controller?

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

Categories

Resources