Creating a textbox in ASP.Net with unique ID programatically - asp.net

I'm wondering what the best way to create text boxes in my loop here so that the IDs are unique instead of just "TextBox" for all of them. I need to add up the Price based on the Price per unit and the Quantity.
My items are loaded from a database so there could be any number of them, so I need to generate the fields on the fly. I was thinking maybe an array of fields could be used like in a form application, or is this not the right way to go?
I tried googling an answer, but maybe I'm just not wording my question well. It seems like this should be solved rather easily, but I just can't find the answer.
Here is my code:
<table class="proposal" width="800">
<tr>
<th>Item</th>
<th>Price per Unit</th>
<th>Quantity</th>
<th>Price</th>
</tr>
<% int x = 0; %>
<% do
{
%>
<tr>
<td valign="top">
<%= this.name[x] %><br />
<%= this.desc[x] %></td>
<td valign="top" align="right"><%= "$" + this.price[x] %></td>
<td valign="top" align="center"><asp:TextBox ID="TextBox" runat="server" Width="75px"></asp:TextBox></td>
<td valign="top" align="right"></td>
</tr>
<% x++;
} while (x != this.y);
%>
</table>

You can't do a dynamic id for an asp.net TextBox. Could you use plain html <input type="text"> text boxes instead? Then you could assign whatever id you want.
Another approach would be to use a repeater control to generate your table. Each TextBox will then automatically get a unique id. If you need to get the ids in code, then in the repeater's ItemDataBound method, use FindControl to get a handle to the TextBox and read the ids/

You'll want to use the Repeater control as in this demonstration.

Related

How to a use a web user control more than one time on the same page?

I have created web user control for calendar. I.e it has a textbox and a calendar image. By clicking on calendar popup the calendar opens. Then by selecting any date it stores the year of that date. I use this code:
<script language="javaScript" type="text/javascript" src="Scripts/Calendar.js"> </script>
<link href="Styles/Calendar.css" rel="stylesheet" type="text/css" />
<table>
<tr>
<td>
<input type="text" name="datum1"/>
</td>
<td>
<img id="Img1" src="images/calFinal.jpg" alt="" runat="server" onclick="setYears(1947, 2040);
showCalender(this, 'datum1');" />
</td>
</tr>
</table>
<div>
<!-- Calender Script -->
<table id="calenderTable">
<tbody id="calenderTableHead">
<tr>
<td colspan="4" align="center">
<select onchange="showCalenderBody(createCalender(document.getElementById('selectYear').value,
this.selectedIndex, false));" id="selectMonth">
<option value="0">Jan</option>
<option value="1">Feb</option>
<option value="2">Mar</option>
<option value="3">Apr</option>
<option value="4">May</option>
<option value="5">Jun</option>
<option value="6">Jul</option>
<option value="7">Aug</option>
<option value="8">Sep</option>
<option value="9">Oct</option>
<option value="10">Nov</option>
<option value="11">Dec</option>
</select>
</td>
<td colspan="2" align="center">
<select onchange="showCalenderBody(createCalender(this.value,
document.getElementById('selectMonth').selectedIndex, false));" id="selectYear">
</select>
</td>
<td align="center">
<font color="#003333" size="+1">X</font>
</td>
</tr>
</tbody>
<tbody id="calenderTableDays">
<tr style="">
<td>Sun</td>
<td>Mon</td>
<td>Tue</td>
<td>Wed</td>
<td>Thu</td>
<td>Fri</td>
<td>Sat</td>
</tr>
</tbody>
<tbody id="calender">
</tbody>
</table>
<!-- End Calender Script -->
</div>
The problem occurs when I use the user control twice on the same web form. I have registered the control. When I use it once, it works proper. But I want it at two places. So, please give some solution on how to use the user control more than one time on the same web form. I have given different IDs for the user control, but it still does not work.
Code for user control:
<tr>
<td class="directorytdWidth directorylabel">
School Name:
</td>
<td class="directoryTdPadding">
<asp:TextBox ID="txtSchoolName" runat="server" Width="120px" ForeColor="#FF9F00"></asp:TextBox>
</td>
</tr>
<tr>
<td class="directorytdWidth directorylabel">
School Passout Year:
</td>
<td class="directoryTdPadding">
<uc1:calendar ID="schoolPassout" runat="server" />
</td>
</tr>
<tr>
<td class="directorytdWidth directorylabel">
College Name:
</td>
<td class="directoryTdPadding">
<asp:TextBox ID="txtCollegeName" runat="server" Width="120px" ForeColor="#FF9F00"></asp:TextBox>
</td>
</tr>
<tr>
<td class="directorytdWidth directorylabel">
College Passout Year:
</td>
<td class="directoryTdPadding">
<uc1:calendar ID="collegePassout" runat="server" />
</td>
</tr>
Use the Same User control but With diferent ID. For example (See code below)
Reg Your User control in you Web page
<%# Register Src="~/Controls/SearchAffiliated.ascx" TagPrefix="uc" TagName="SearchAffiliated" %>
First User Control call
<uc:SearchAffiliated runat="server" ID="ucSearchAffiliated" />
Second User Control Call
<uc:SearchAffiliated runat="server" ID="SearchAffiliated1" />
See The Users Controls have diferents IDs, That Works For call The same UC in one web form.
Cheers.
Use the loop of how many times that u want the usercontrol to display:
for(int i=0;i < n; i++) //Define the number of time that u want to display
{
usercontrolname.id="UC"+i;//Assign the id for that usercontrol
//add line of code,that controls in placeholder or panel
}
You can use same user control in same page in N number of times. either it dynamically or statically. Only the thing is we need to give the different ID for the usercontrol
This need to add in your Reg
<%# Register Src="~/UserControls/Hello.ascx" TagPrefix="uc" TagName="Hello" %>
calling the user control 1
<uc:Hello runat="server" ID="ucHello1" />
calling the user control 2
<uc:Hello runat="server" ID="ucHello2" />
I faced same problem but the problem was not related with the ID you prefer to use inside the asp.net page. Because I guess asp.net automatically generates ID for each element in page if it doesn't have one.
The problem was I copied ascx files and forget to change their class names. Make sure you use different class names for each user control.
<%# Control Language="C#" ClassName="ucSchema2" %>
I know this i late to the game but maybe someone can use this answer anyway.
Using a static clientside id for a html element in a user control and then using that user control multiple times will results in invalid html.
You are only allowed to use an id once on a html page otherwise getElementById (which returns a single element) will not know which element to get and then you will probably not get any element whatsoever.
You need to make those clientside ids inside the user control truely unique, i.e. selectMonth, selectYear.
This might be done by appending the ClientID of the user control to the element id. Something like this (not sure if this approach really works but you get the idea):
<select onchange="showCalenderBody(createCalender(document.getElementById('selectYear_<%=this.ClientID%>').value,
this.selectedIndex, false));" id="selectMonth_<%=this.ClientID%>">
This way the clientside id should be a combination of locally unique ids and the page unique control ids.

asp.net inline tags within a table cell

I cannot get the "style width" value to display correctly when using inline syntax. It renders as:
style=""
Any suggestions.
<table>
<tr id="tr1" runat="server">
<td style="width: <%= this.LabelColumnWidth %>">
</td>
</tr>
</table>
give this a whirl
<td style="width: <%# this.LabelColumnWidth %>">
EDIT: (now that I know a little more)
why dont you add an id to that td something like this
<td id="test" runat="server">
and then in code behind do
test.style = "width: 100px;" // or whatever
inline Attempt 2:
give this a whirl
<td style='<%# "width: " + this.LabelColumnWidth %>'>
Thinking maybe the whole thing needs to be in the inline code. and not just the value of width.
The answer suggested by Eric is almost correct. Only, it should have a = instead of #
<td style='<%= "width:" + this.LabelColumnWidth %>' >
A hash is used only when you need to include a eval() or bind() function. Otherwise, use the = sign.

ASP.NET MVC displaying user Data such as comments?

hows the best way to display comments/user data from a db using mvc?
do we just do our own for loop and manually display it or? will doing it this way cause paging problems in the future? any best methods or practice?
jquery?
thanks guys.
When I first started learning MVC, I put a lot of conditional code in the view to manage the display of the data and controls. However, once I figured out the benefit of HTML helpers, I found that it not only reduced the code in my views, but it also made it easier to test the resulting output.
As an example, this is a code fragment for an entry form that would appear in the finished HTML page:
<table class="listing">
<tr>
<td class="prompt">Last Name:</td>
<td><input id="LastName" name="LastName" value="Smith" /></td>
</tr>
<tr>
<td class="prompt">First Name:</td>
<td><input id="FirstName" name="FirstName" value="John" /></td>
</tr>
<tr>
<td class="prompt">Job Title:</td>
<td><input id="JobTitle" name="JobTitle" value="Manager" /></td>
</tr>
</table>
Building the view using standard MVC helpers would result in code that looks something like this:
<table class="listing">
<tr>
<td class="prompt">Last Name:</td>
<td><%= Html.TextBox("LastName", Model.LastName) %></td>
</tr>
<tr>
<td class="prompt">First Name:</td>
<td><%= Html.TextBox("FirstName", Model.FirstName) %></td>
</tr>
<tr>
<td class="prompt">Job Title:</td>
<td><%= Html.TextBox("JobTitle", Model.JobTitle) %></td>
</tr>
</table>
However, I was able to create HTML helpers that allowed me to make the view look like this:
<table class="listing">
<%= Html.Edit_LastName(Model.LastName) %>
<%= Html.Edit_FirstName(Model.FirstName) %>
<%= Html.Edit_JobTitle(Model.JobTitle) %>
</table>
Here are the HTML helpers:
private string Edit_Field(string prompt, string fieldName, string value)
{
string textBox = helper.TextBox(fieldName, value);
return string.Format("<tr><td class='prompt'>{0}</td><td>{1}</td></tr>", prompt, textBox);
}
public string Edit_LastName(this HtmlHelper helper, string value)
{
return EditField("Last Name:", "LastName", value);
}
public string Edit_FirstName(this HtmlHelper helper, string value)
{
return EditField("First Name:", "FirstName", value);
}
public string Edit_JobTitle(this HtmlHelper helper, string value)
{
return EditField("Job Title:", "JobTitle", value);
}
This method is not nearly as straightforward as the standard method, but if you have similar controls that are used on multiple views, this approach clearly helps with maintenance. For example, if you want to change the prompt text from "Job Title" to "Position", you just change the prompt string in the Edit_JobTitle method and the change is reflected in every view where that method is called.
is the best way to use client jquery
with tablesorter or server side for
loop?
I don't think there is a connection between these two. Anyways you have to use an iterator to get all the rows from the database. Then you can use jQuery plugin to view the data in pages.
Here is a nice implementation of this
Table Sorting, Paging and Filtering with jQuery
IMO, use the iterator. I use jquery plugins when I need a display format that is complex. jquery itslef is useful when you need to access/modify dom or make ajax calls. Paging shouldn't affect this decision or vice-versa.

Span columns in datagrid

I have a datagrid where some of the text needs to span multiple columns. Here is an example of what I need.
Row # Image Name Price Date
1 xxx My Name $99 1/1/2009
xxx
xxx Long description goes here
2 xxx name 2 $99 1/1/2009
xxx
xxx Another long description
Is something like this possible in Asp.Net using a datagrid? Any suggestions on how to do this?
You might find it easier to use an ASP.Net Repeater instead, and have multiple rows per DataItem. That way, you get complete control over the layout.
It's technically possible to use the ASP.Net GridView to do it, but I suspect this wouldn't be the most elegant solution here. The GridView (and Datagrid) were designed out of the box for one row per DataItem.
Here is some sample ASP.Net containing a Repeater that illustrates an approach that might work for you:
<asp:Repeater ID="Repeater1" runat="server">
<HeaderTemplate>
<table>
</HeaderTemplate>
<tr>
<th>
Col 1
</th>
<th>
Col 2
</th>
<th>
Col 3
</th>
</tr>
<ItemTemplate>
<tr>
<td>
<%# Eval("Field1") %>
</td>
<td>
<%# Eval("Field2") %>
</td>
<td>
<%# Eval("Field3") %>
</td>
</tr>
<tr>
<td>
<%# Eval("Field4") %>
</td>
<td colspan="2">
<%# Eval("Field5") %>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
Note that the ItemTemplate contains two HTML table rows, the second of which contains a td with a colspan of 2.
Hope that helps a little.
Do you have to use a DataGrid?, I would suggest the ListView control. It has all the functionality you need and uses templates for full UI control.

Possible to dynamically add an asp:CheckBox control to a TableCell?

In my .NET application I need to add a checkbox to each row in a dynamically created asp:Table.
Is it possible to do that by dynamically creating an asp:CheckBox for each row and somehow put it inside a TableCell object? (In that case how?)
Or do I need to replace the asp:table control with something else, like a Repeater control or GridView to make it work?
I'm looking for the quickest solution because I don't have much time.
Thanks in advance!
/Ylva
in aspx:
<asp:Table id=T1 runat=server />
in cs:
TableCell tc;
foreach(TableRow tr in T1.Rows)
{
tr.Cells.Add(tc = new TableCell());
((IParserAccessor)tc).AddParsedSubObject(new CheckBox());
}
You do not want to do it on server side( in the cs as Yossarian said). because every time your page is reloaded or refreshed, you would have to recreate those checkboxes, which would mean new checkboxes every load, which also would mean your checkbox controls info will be lost because they are not on the client side, so all updated info done by the user (checkbox checked)will be lost, so you want be able to find out what is checked unless you add jquery in and it starts to get more complicated then it needs to be
if you are using webpages then it would be best to use asp:Gridview web control and bind the data to the table in code behind as so:
Gridview.Datasource=//ex:data;
Gridview.Databind();
As shown in the example on this page here
but if you are using MVC then you would add them in the client code in a form as so:
<% using (Html.BeginForm("Presentation", "Home")) %>
<% { %>
<table id="Table" class="color" width="100%" border="1">
<colgroup width="3%" ></colgroup>
<colgroup width="15%"></colgroup>
<colgroup width="20%"></colgroup>
<colgroup width="15%"></colgroup>
<colgroup width="47%"></colgroup>
<thead>
<tr class="dxgvHeader_Glass">
<th id="CheckBox" class="style1" ><input type="checkbox" class="selectall" id="selectall" name="CheckBox" /></th>
<th id="DateTime" runat="server"></th>
<th id="Description" runat="server"></th>
</tr>
</thead>
<tbody >
<%try
{ %>
<% foreach (var SamAuditLog in ViewData.Model)
{ %>
<tr>
<td class="style1" align="center"><%=Html.CheckBox(""+data.ID) %></td>
<td><%= data.DateTime%></td>
<td><%= data.Description%></td>
</tr>
<% } %>
<%} %>
</tbody>

Resources