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
Related
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.
I'm new for asp.net and now using control asp:Repeater.
I want to implement follow table by Repeater:
index Name Delete
1 DDD X
2 EEE X
3 FFF X
Now the index is auto generate by set <%# (Container.ItemIndex+1) %> in asp.net page.
My question is below:
when I click 'X' to remove TableRow by JS method. How to make the column index value refresh? But sadly, the column idx become below:
index Name Delete
1 DDD X
3 FFF X
Thanks in advance.
when I click 'X' to remove TableRow by register Js method
If it's JS , then you're out of C# code.
so your question should also be tagged as JS/jQuery.
regarding :
How to make the column index value refresh ?
My solution using jQuery is :
$("#t tr td:nth-child(1)").on('click',function (i,n){
$(this).closest('tr').remove();
$("#t tr td:nth-child(1)").each(function (i,n){$(this).text(i+1)})
})
http://jsbin.com/hutefo/3/edit
Since you're removing it via a javascript. just hook into that and add a piece of code that reseeds your indices
// sample reseeding code
$('button').on('click', function() {
$(this).closest('tr').remove();
$('table').find('tbody').find('tr').each(function(i) {
// update the value of the index column
$(this).find('td').first().text(i + 1);
});
});
DEMO: http://jsfiddle.net/7fbnn95a/
I am simply trying to create a list and add elements to it from the code behind. Each list element must be connected to a function in the code behind so I am using the Asp:LinkButton to do this. In the Default.aspx page I have:
<asp:ListView ID="ulNumTenants" runat="server">
<ItemTemplate>
<li>
<%# DataBinder.Eval(Container.DataItem, "XXX" ) %>
</li>
</ItemTemplate>
</asp:ListView>
And in the code behind I have the following:
var listItems = new List<LinkButton>();
int numberOfTenantsPossible = Space.MaxNumberOfTenants - (Space.MaleHousemates + Space.FemaleHousemates);
for (int itemCount = 0; itemCount < numberOfTenantsPossible; itemCount++ )
{
LinkButton currentItem = new LinkButton();
currentItem.CommandArgument = (itemCount + 1).ToString();
currentItem.CommandName = "Tenant_OnClick";
currentItem.Text = (itemCount + 1).ToString() + " tenants";
listItems.Add(currentItem);
}
ulNumTenants.DataSource = listItems;
ulNumTenants.DataBind();
The issue I am having is in the default.aspx code since I do not know what the expression field( "XXX" ) should be set to when I am not getting the entries from a database. Any suggestions are greatly appreciated.
Try this:
<%# Container.DataItem %>
I doubt it will work, since I think it will just take the string representation of a LinkButton instead of the HTML markup. However, why create the LinkButton dynamically in code? Try this instead:
Code Behind:
public class TenantViewModel
{
public string ID {get; set;}
public string Name {get; set;}
}
int numberOfTenantsPossible = Space.MaxNumberOfTenants - (Space.MaleHousemates + Space.FemaleHousemates);
var vms = new List<TenantViewModel>();
for (int itemCount = 0; itemCount < numberOfTenantsPossible; itemCount++ )
{
var vm = new TenantViewModel { ID = (itemCount + 1).ToString(), Name = (itemCount + 1).ToString() + " tenants"};
vms.Add(vm);
}
ulNumTenants.DataSource = vms;
ulNumTenants.DataBind();
ASPX:
<asp:ListView ID="ulNumTenants" runat="server">
<ItemTemplate>
<li>
<asp:LinkButton runat="server" CommandName="Tenant_OnClick" CommandArgument='<%# (Container.DataItem as TenantViewModel).ID' Text='<%# (Container.DataItem as TenantViewModel).Name' />
</li>
</ItemTemplate>
</asp:ListView>
That allows you to keep UI element declaration in your ASPX markup, and instead of creating all the buttons in your code behind, you just create a view model to bind it to. Container.DataItem will be an object, so we use the as syntax to convert it to the correct type TenantViewModel so we can access the properties. This results in much cleaner code. Instead of a ListView, you might also consider binding to a Repeater. ListViews are typically for two way binding directly to a database, but we're binding to a custom IEnumerable.
Also, if you do find that this markup is significantly cleaner, you might consider looking into ASP.NET MVC. The markup gets even cleaner there with Razor syntax, because you won't have to worry about casting to the correct type. Instead of using a repeater, you'd just use a foreach loop.
On default.aspx I have the following hidden fields:
<asp:HiddenField runat="server" ID="icon1" />
<asp:HiddenField runat="server" ID="icon2" />
<asp:HiddenField runat="server" ID="icon3" />
As you can see, the name of the field is the same each time but increments by 1 up to 3.
In my code behind I have been doing this (if statements and other code removed for brevity - this is the meat of it):
icon1.Value = "Bonus1";
icon2.Value = "Bonus2";
icon3.Value = "Bonus3";
Must I assign the iconX.Value individually every time like that? Can I do it all in one shot in a loop (also with everything else removed for brevity)?
for (int i = 1; i <=3; i++)
{
icon(i).Value = "Bonus" + i.ToString();
}
Everything I have read leads me to believe this is not possible in C#. Let's pretend I have 50 iconX.Value = whatever to assign. A loop makes the most logical sense. Possible?
A loop makes the most logical sense. Possible?
Yes. Use the FindControl method of the page to look up a control by its ID:
for (int i = 1; i <= 3; i++)
{
HiddenField field = (HiddenField)this.FindControl("icon" + i);
field.Value = "Bonus" + i.ToString();
}
Note: Because the return type of FindControl is Control, you must cast the result in order to access properties specific to HiddenField.
You have to create a asp:panel in the HTML section.
Then, in the c# code, make a loop and create elements like you describe.
When the element is fully configured, add it to the panel.
.ASPX file
<asp:Panel ID="panel_controls" runat="server"> </asp:Panel>
C# code
for (int i = 1; i <= 3; i++) {
HiddenField myField = new HiddenField();
myField.ID = "icon" + i;
myField.Value = "Bonus" + i;
panel_controls.Controls.Add(myField);
}
I am new to ASP.NET MVC 2 and would like to know what the easiest and/or best practice is to send "grid data" from a View to a Controller. Think of this "grid data" as an excel spreadsheet where some columns will have some data and some rows will have some data. In the controller I need to know not only what column and row contains data but also the specific data it contains. My hope would be to have a "grid object" in the controller class that I could loop through to gather the necessary data but I'm open to other options.
Obviously this probably isn't the easiest way to capture data like this but just imagine this is the only way you are allowed to get this input from the user.
Also I am using VS.NET 2010, ASP.NET 4.0, C# 4.0 so I've thought about not using the ASP.NET MVC 2 and using ASP.NET web forms since it seems this solution would be easier in web forms. My only concern with doing that is there are other aspects of the website that would be easier in MVC and this seems to be the only issue at this point with using MVC.
Thanks,
Paul
That will depend a great deal on how you've bilt your table. But here is a very simple sample:
1 - put your hole table in a form;
2 - Identify each data cell's field with it's "coorditate"
Like this:
<%: using(form = html.Form("UpdateGrid")) { %>
<table>
<% for (int r = 0; r < rowCount; r++) { %>
<tr>
<% for (int c = 0; c < columnCount; c++) { %>
<td><%: html.TextBox(string.format("cell_{0}_{1}",r,c)) %></td>
<% } %>
</tr>
<% } %>
</table>
<% } %>
It's just a sample code (I'm not sure if it will work as it is), but it will give you an idea.
If you want a Excel like address you can use this:
<td><%: html.TextBox(string.format("{1}{0}",(char)(r + 'A'),c + 1)) %></td>
To access the table info from the controller you can use the following cone:
public ActionResult UpdateGrid(FormCollection form) {
// ... Some initialization
for (int r = 0; r < rowCount; r++ ) {
for (int c = 0; c < columnCount; c++ ) {
var cellValue = form[string.format("{1}{0}",(char)(r + 'A'),c + 1)]; // Excell like format
// Add your manipulation here;
}
}
// ... Continue your controller implementation
}