Adding Controls dynamically. Asp.Net and VB - asp.net

I'm trying to add a "panel" with controls dynamicaly. Something like the code below, Clicking "Add" button, displays a "Panel" with the controls(which are within a table), the number of "Education" someone can enter is unlimited. I know how to capture the data, but i can't figure out how to implement/code this,Can anyone please give me some pointers for the same?
Should i be reading about AJAX? I don't know anything about AJAX..
<asp:Panel ID="Panel1" runat="server">
<table>
<tr>
<td><asp:TextBox ID="CollUniv_Name" runat="server"></asp:TextBox></td>
<td><asp:TextBox ID="CollUniv_Location" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td><asp:TextBox ID="StartDateText" runat="server"></asp:TextBox></td>
<td><asp:TextBox ID="EndDateText" runat="server"></asp:TextBox></td>
</tr>
</table>

Seems like this would more naturally handled by a repeating control. Something along the lines of a ListView control.
There's a fairly full example in the MSDN page I linked to so I won't repeat it here. The two parts you'll want to take a look at a bit more closely are the ItemTemplate which lets you define what normal items look like and the InsertItemTemplate which lets you specify what your insert row will look like. You can also specify if the InsertTemplate appears at the top or bottom. In the code-behind you'll subscribe to the ItemInserting even and handle user input from there.

Related

How to create excel kind of filter, sorting, edit data in asp.net?

I want to allow 20k data records in asp.net to filter, sort, paging and edit. Which is the best possible?
Well, as noted, we can't load 20k records at one time - that would be too much data for a use at one time.
So, can you add paging and say display a screen of data in grid/table like view? yes, that's easy.
Can you add sorting to the grid display (say sort up/down) Yes, it will take a wee bit of code, but that can be done quite easy.
But to allow say the user to filter on any column? Hum, that going to take quite a bit of work. So you can certainly acheive the first two goals - quite easy.
but there not any built in filter system + set of controls that will acheive the filtering options for you. I REALLY wish there was. You can consider some 3rd party controls - they are quite expensive, but they do look very nice. So, say this grid from telerick is a great example of what you looking for:
So the above has paging, and sorting, and filters.
demo here:
https://demos.telerik.com/aspnet-ajax/grid/examples/overview/defaultcs.aspx
I REALLY don't know why after all these years that the base .net package does not include a grid extender or a listview control that is so feature starved, that everyone every day has to go out and re-invent the world.
But then you have to work to add a data pager And out of the box, the default format for the pager does not look all that great. Once you spend a hour or two, you can get a nice formatted data pager (but it should not be that much work to get a nice look and feel out of the box - it just should not be).
So say here is a listview (I used the wizards - then deleted 99% of the markup, and with this markup:
<asp:ListView ID="ListView1" runat="server" DataKeyNames="ID">
<ItemTemplate>
<tr style="">
<td><asp:label ID="FirstName" runat="server" Text='<%# Eval("FirstName") %>' /></td>
<td><asp:label ID="LastName" runat="server" Text='<%# Eval("LastName") %>' /></td>
<td><asp:label ID="HotelName" runat="server" Text='<%# Eval("HotelName") %>' /></td>
<td><asp:label ID="City" runat="server" Text='<%# Eval("City") %>' /></td>
<td><asp:CheckBox ID="Active" runat="server" Checked='<%# Eval("Active") %>' /></td>
</tr>
</ItemTemplate>
<LayoutTemplate>
<table id="itemPlaceholderContainer" runat="server" border="0" style="">
<tr runat="server" style="">
<th runat="server">FirstName</th>
<th runat="server">LastName</th>
<th runat="server">HotelName</th>
<th runat="server">City</th>
<th runat="server">Active</th>
</tr>
<tr id="itemPlaceholder" runat="server"></tr>
</table>
</LayoutTemplate>
</asp:ListView>
And it looks like this:
So the listview is a nice way to present the data.
However, to get a nice layout, nice filtering, a half decent looking datapager? It takes a truckload of work. The out of the box asp.net controls used to be perhaps the BEST feature of asp.net
That above page was created by me in less time to write this post. It looks not bad - but "not bad" was 5 or 10 years ago.
The new data controls out of the box should be like the first grid - the amounts of efforts to obtain a nice looking grid IMHO is just not all that great right now.
So, I would consider a 3rd party grid control, or you going to have to learn quite a bit of css to get a great looking Excel like display.
It is possible. My beters would be for simple - gridview.
But, for really nice layout - extra options - go with listview - it requires a bit more markup, but has near unlimited flexbility.

Dynamically adding controls with button click [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I am trying to create:
A form that loads with two text boxes ("item" and "amount") and an "add" button.
Every time the "add" button is clicked, another set of text boxes identical to the first is added.
The text boxes need to retain whatever has been entered in them when the "add" button is clicked.
What is the best way to do this? I have been trying a lot of different things but none are working quite right.
I've had to do something like this a couple times both on win forms and on a web page. Unfortunately on a web page its not as easy as it is on win forms. I'm sure there are JQuery implementations out there but the way that I've done it in the past is using a repeater.
The handling of the item command event is a little tricky but I'll try to slim it down as best I can. A little background on this particular project was that I was coming up with an interface where people could edit the links that would be on their webpage. The original contains a lot more controls in the repeater but I slimmed it down so it wouldn't take up too much space.
First, declaring the repeater is easy enough:
<asp:Repeater ID="rptLinks" runat="server">
<HeaderTemplate>
<table style="width:99%; text-align:center; margin-left:auto; margin-right:auto"><tr>
<td style="width:15%;"><asp:Label ID="lblDisplayName" runat="server" Text="Display Name:"></asp:Label></td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td style="width:15%;"><asp:TextBox ID="txtLinkDisplayName" runat="server" Width="95%" Text='<%#Bind("Name") %>'></asp:TextBox></td>
<td style="width:5%;"><asp:Button ID="btnLinkSave" runat="server" Text="" CssClass="linkButton" style="background-image:url('Images/Save_16.png')" ToolTip="Update Link" CommandName="Update" CommandArgument='<%#Eval("Customer_Link_ID") %>' /></td>
<td style="width:5%;"><asp:Button ID="btnLinkClear" runat="server" Text="" CssClass="linkButton" style="background-image:url('Images/Cancel_16.png')" ToolTip="Delete Link" CommandName="Delete" CommandArgument='<%#Eval("Customer_Link_ID") %>'/></td>
</tr>
</ItemTemplate>
<FooterTemplate>
<tr>
<td style="width:15%;"><asp:TextBox ID="txtLinkDisplayName" runat="server" Width="95%"></asp:TextBox></td>
<td style="width:5%;"><asp:Button ID="btnLinkNew" runat="server" Text="" CssClass="linkButton" style="background-image:url('Images/Add_16.png')" ToolTip="Save New Link" CommandName="New" /></td>
<td style="width:5%;"><asp:Button ID="btnLinkCancel" runat="server" Text="" CssClass="linkButton" style="background-image:url('Images/close_16.png')" ToolTip="Clear" CommandName="Clear" /></td>
</tr>
</table>
</FooterTemplate>
</asp:Repeater>
Basically all this does is create a template for the repeater. The header portion starts the table and contains a single label. The item template is what is used to display the data that is bound to the repeater. The text in this case is bound using server side includes and the value in the "Bind" function needs to match exactly to the column in the table being bound to the repeater. The footer in this case is where your new entries will be typed in and also closes the table tag. The template and the footer both contain 2 buttons. Its important to note that the CommandName value is set accordingly because that will be used later on on the server side.
So once the client side is all set up we will need to handle the item command event on the server. I set my function up like this:
Private Sub rptLinks_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.RepeaterCommandEventArgs) Handles rptLinks.ItemCommand
Try
'This clears the datasource because if I didn't do this I was getting weird behavior.
rptLinks.DataSource = Nothing
rptLinks.DataBind()
Select Case e.CommandName
Case "New"
'Do whatever you need to for a new record
Case "Update"
'Do whatever you need to to update the record
Case "Clear"
'Clear the controls
Case "Delete"
'Delete the link that is in this row
Case Else
End Select
'rebind the repeater to the updated data.
rptLinks.DataSource = LinksDatatable
rptLinks.DataBind()
Catch ex As Exception
End Try
End Sub
This is the easiest way I found to determine which button was clicked but I'm sure there are others ways to accomplish it.
Now once you get into the case that you need to handle you will eventually need to get the values from the controls and the way that you do that is by: e.Item.FindControl("txtLinkDisplayName"). In my case I used that code inside a directcast() call and cast the controls as needed to make them easier to work with.
So once all that is in place, databound and you handle each case then you should be ready to go.

FindControl() Keeps Returning Null!

I realise there are many posts on the web about this already but I cant seem to find any for my particular problem!
I have a dynamic table that is populated using a repeater.
the code is:
<asp:Repeater ID="rptPending" runat="server">
<HeaderTemplate>
<table id="tblPending" cellpadding="0" cellspacing="0" border="0" class="display">
<thead>
<tr>
<th>Company Name</th>
<th>Telephone</th>
<th>Fax Number</th>
</tr>
</thead>
<tbody>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td runat="server">
<asp:TextBox ID="a" runat="server" TextMode="MultiLine" Rows="1"
onChange="__doPostBack($(this).attr('name'),$(this).parent().attr('id'));"
Text='<%# Eval("_companyName")%>'></asp:TextBox>
</td>
<td runat="server">
<asp:TextBox ID="b" runat="server" TextMode="MultiLine" Rows="1"
onChange="__doPostBack($(this).attr('name'),$(this).parent().attr('id'));"
Text='<%# Eval("_telephone")%>'></asp:TextBox>
</td>
<td runat="server">
<asp:TextBox ID="c" runat="server" TextMode="MultiLine" Rows="1"
onChange="__doPostBack($(this).attr('name'),$(this).parent().attr('id'));"
Text='<%# Eval("_faxNo")%>'></asp:TextBox>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</tbody> </table>
</FooterTemplate>
</asp:Repeater>
Once this table has been populated with data from a datasource, a jquery script iterates through every table cell and edits the id's accordingly. cell_0_0, cell_0_1, cell_0_2 etc.
Now When the text on these text areas change, a postback is initiated, with the __EVENTTARGET being the textarea ID, and the __EVENTARGUMENT being the table cell (parent) ID.
These get sent to the server-side no problem. The issue I am having is GETTING THE TEXT inside the text area...
I have tried adding controls using FindControl("cell_0_0"); Which returns null. Then i found out the FindControl() function IS NOT recursive, so i copied a recursive function off the net... and it Still Fails!
Basically ALL i need to do is GET the value (either innerText or InnerHTML). Ive tried using Control, HtmlControl, HtmlTableRow, and HtmlTextArea.
I just cant seem to get the value. Ive tried recursing throught i a noted earlier, but the controls arent even registered. Im confused.
PLEASE HELP!
Thanks
Alex
Well this is a usually issue, because of the timing of the creations of the controls, and when you create control inside a repeater the timing is even more complex because repeater must first full bind, and then search for the controls.
In my programs to avoid all that I just get the posted value from the Form and I actually not first search to find the control. So just get your posted values from the Request.Form and move on.
Request.Form
All the post data lives on Request.Form, so you can simple get the one you need, or find the one you need. Just a note, to get a value using the Form use the UniqueID, and not the ClientID. Even better get the value from your custom name ids.

ListView LayoutTemplate does not show when empty asp.net

I have an <asp:ListView> but for some reason the LayoutTemplate section does not show when the list is empty, although the <EmptyDataTemplate> section shows. The LayoutTemplate contains the headers for the table, and I want to show an empty table when there are no items in the datasource, not just the content of EmptyDataTemplate.
If there is no choice I will copy the LayoutTemplate into EmptyDataTemplate but it seems stupid to have to do this. Ideas?
From the MSDN:
The empty template is displayed in a
ListView control when the data source
that is bound to the control does not
contain any records and the
InsertItemPosition property is set to
InsertItemPosition.None. The template
is rendered instead of the
LayoutTemplate template. If the
InsertItemPosition property is set to
a value other than
InsertItemPosition.None, the
EmptyDataTemplate template is not
rendered.
the key words here are "...the template is rendered instead of the LayoutTemplate template..."
So I think, you have to copy the LayoutTemplate into the EmptyDataTemplate template.
In a very simple way you can get both your headers and a message saying that there were no data.
You make your LayoutTemplate like the following idea:
<LayoutTemplate>
<table>
<tr>
<td>a header</td>
<td>another header</td>
<td>third header</td>
</tr>
<tr runat="server" id="itemPlaceholder">
<td colspan="3"
There is no data!
</td>
</tr>
</table>
</LayoutTemplate>
Notice that the tr that is the placeholder (marked by id="itemPlaceholder") actually contains something. It contains what should be shown when there is no data. Then, in code behind, you set the <EmptyTemplate> to be equal to the <LayoutTemplate> (so that you have only one such template to maintain). I do it like this:
Private Sub lvwThings_Init(sender As Object, e As EventArgs) Handles lvwThings.Init
lvwThings.EmptyDataTemplate = lvwThings.LayoutTemplate
End Sub
The logic then is as follows:
When there is data, i.e. when the actual <LayoutTemplate> is used, the whole <tr runat="server" id="itemPlaceholder">, with the td and text it contains, will be replaced by the <ItemTemplate>.
But, when there is no data, i.e. when the <EmptyTemplate> is used (instead of the <LayoutTemplate>), nothing inside the <EmptyTemplate>is replaced, so everything is shown as it is.
You can also put your into a User Control (.acsx). Then include it in the layout template and the empty template... and it will feel less stupid since you can still manage it in one spot. I know how you feel about copying the same code...seems like something a 5th grader would do. Using a control is a more grown up approach.
I just solved this problem when you have InsertItemTemplate with EmptyDataTemplate.
Arrcording to MS docs, that's you can't have both. So I decided to create new tag in InsertItemTemplate.
You can preview my example code here.
<InsertItemTemplate>
<% if (CheckEmptyTable())
{ %>
<tr>
<td colspan="6">No data founds。</td>
</tr>
<% } %>
// Your insert template input here
<tr style="">
</tr>
</InsertItemTemplate>
My result image:

Background image for a label in a gridview

I have a gridview, under a item template I have a table, one of the row of the table look like this
<td bgcolor="White" colspan="2" style="width:100%; background-image:url(bubble1.gif);">
<asp:Panel ID="pnlHistory" runat="server" ScrollBars="Auto">
<asp:Label ID="lblHistory" runat="server" Text='<%# Server.HtmlDecode(Eval("History").ToString()) %>'></asp:Label>
</asp:Panel>
</td>
</tr>
I want to display bubble image as a background, on which my text [i.e. History (coming from database)] will be displayed.
I solved it, I am using Jquery bubble for the same..
This is truly strange that I haven't get any suggestion at stackoverflow for my question, instead I got a missing tag statement...
Anyways, I am posting this answer because I have resolved the issue and if anyone else is looking for the same , then this answer might proof helpful for them.

Resources