I have a asp.net web page that has various pieces of data on it. One of those is a standard html table. 10 columns by 5 rows. I want to place a link below the table that allows the user to click on it and it will export the data from the table into an excel sheet.
Can anyone help?
If you are wanting to export the data i am presuming that the data is dynamic, from a database? On that assumption why not display the data in either a GridView or an ASP table? This way you will be better able to export the data.
If you are dynamically creating the table from code then i guess thats different, but you havent stated how your generating the page.
Exporting Controls to excel: Just change Gridview1 to the id of the control you want to export. Either your ASP table or your gridview ID.
Dim sw As New StringWriter()
Dim hw As New System.Web.UI.HtmlTextWriter(sw)
Dim frm As HtmlForm = New HtmlForm()
Page.Response.AddHeader("content-disposition", "attachment;filename=ExportFromWebPage.xls")
Page.Response.ContentType = "application/vnd.ms-excel"
Page.Response.Charset = ""
Page.EnableViewState = False
frm.Attributes("runat") = "server"
Controls.Add(frm)
frm.Controls.Add(GridView1)
frm.RenderControl(hw)
Response.Write(sw.ToString())
Response.End()
-Edit. Ive shown my naivety here and just assumed this wouldn't work with a native html table. I thought id try it and it actually will work. Just run your table at server and give it an ID then replace gridview1 with the id you've given your HTML table.
im just going to put some articles you can use or other answers but googling html table to excel you can find some answers.
html table to excel using jquery
How to export html table to excel using javascript
Related
Hello I'm developing a asp. Net page where I want to populate many tables in the ui
But I want it to be dynamic.
Eg: If I retrieve only 2 rows as per search criteria then I want to display only 2 tables.
I tried dynamic table creation using string builder but I got a null reference because the table Id was not being read . Is there any other way where I can create tables dynamically in the html page or should I do it in the code behind itself , then get the Id using flow control and then populate the table. Please help!!!
The problem of creatig tables dynamically has been rectified but now i cannot retrive their ids so that i can print values retrived from the datatbase in it using id.text command.
this is my code
StringBuilder htmlTable = new StringBuilder();
htmlTable.AppendLine("<table>");
htmlTable.AppendLine("<tr>");
htmlTable.AppendLine("<th>colum1</th>");
htmlTable.AppendLine("<th>colum2</th>");
htmlTable.AppendLine("<th>colum3</th>");
htmlTable.AppendLine("</tr>");
htmlTable.AppendLine("<tr>");
htmlTable.AppendLine("<td><asp:Label runat='server' id='lblt0'></asp:Label></td>");
htmlTable.AppendLine("<td>colum2data</td>");
htmlTable.AppendLine("<td>colum3data</td>");
htmlTable.AppendLine("</tr>");
htmlTable.AppendLine("</table>");
litTable.Text = htmlTable.ToString();
//litTable is the id of my asp:literal tag
You cannot create a table with a runat=server tag in this manner. Adding items to a literal control can only create client side markup not server side controls.
Your best route is to use a GridView as suggested or an HtmlTable
GridView: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview(v=vs.110).aspx
HtmlTable: http://msdn.microsoft.com/en-us/library/system.web.ui.htmlcontrols.htmltable(v=vs.110).aspx
If your example really is as trivial as the one you provided an HtmlTable might be the easiest route. Though if you want to populate your table from a database or some other data source a GridView is your best option.
Not done any programming for about 5 years so im a little rusty on this one.
I am building an Asset management system to make my job easier but am struggling on a few things.
What i have so far is the DB setup and populated with a small amount of data, The main site itself is built and the basic select statements to a gridview of any data currently in the system for that particular page/search and i also have an insert statement that is populating the relevant tables based on data filed in on a form on that page. All these functions are working seamlessly.
The problem i have is that one field that needs to be populated for the insert statement needs to populate based upon the selection that is being made from a drop down box in the same form.
i.e.
An Asset that is being registered into the system has an Asset Type (the type details are contained within a parent table Asset_Type) the form has 3 fields for display purposes (one of these fields will need to return an entry to the Asset table in the database from the form, this is working) that are taken from the Asset_Type table, the other fields only relate to the Asset table itself.
What i am trying to achieve is that a user goes to the page and sees a list of Assets registered (this is working) they then need to add a new asset by filling in a form lower down on the page (for is there and writes to DB) on this form is a drop down menu that queries the Asset_Type table and allows the user to select the Type by name (this works)
What i now need to get working is for the 2 other fields to populate based on what Asset type is selected.
These fields are currently textboxes in the form but can be changed if required.
The code i have behind the page is below:
Protected Sub Name_Model_SelectedIndexChanged(sender As Object, e As EventArgs) Handles Name_Model.SelectedIndexChanged
Using conn As New SqlConnection(ConfigurationManager.ConnectionStrings("AssetManagementConnectionString").ToString())
Dim cmd As New SqlCommand()
cmd.Connection = conn
cmd.CommandText = "SELECT [ID], [Name_Model], [Description_Spec] FROM [Asset_Type] WHERE ([ID] = #ID)"
cmd.CommandType = CommandType.Text
conn.Open()
Dim reader As SqlDataReader = cmd.ExecuteReader()
If reader.HasRows Then
While (reader.Read())
{
Type_ID.text = (reader["ID"].tostring())
Name_Model.text = (reader["Name_Model"].tostring())
Description_Spec.text = reader["Description_Spec"].tostring
}
End While
End If
End Using
End Sub
So I'm not sure about Steve's comment, but I know he is a lot more familiar with VB based on questions of my own he has answered. However I am going to suggest something.
So "Drop downs" typically have 2 pieces of data associated with them.
A DisplayMember and a ValueMember (also a SelectedValue if it's a ComboBoxColumn)
You can reference either, I believe.
Then, you can do what you please with that value using an If Then or however you plan on using it.
assetValue = YourComboBox.DisplayMember.ToString
YourTextBox.Text = assetValue
or
If ComboBox.ValueMember = YourIndex Then
assetValue = Whatever
ElseIf
....
End If
Give that a try and let me know what you get. I am just throwing ideas out there.
I am working with webpage and I will connect the data i have to data base. so my question how can I create different number of textboxes and assing values or data to them and not loss them?
I working on a web page and i have placeholders for the new textboxes but i have no idea how to keep them there and assign value to them in order to save them in the database
Dim c As Integer = 0
While c < 10
Dim lab As New Label()
Dim ltr As New Literal()
lab.Text = c.ToString()
ltr.Text = "<br/>"
PlaceHolder1.Controls.Add(lab)
PlaceHolder1.Controls.Add(ltr)
C+=1
End While
and if you are working with a databese, you can always use the data grid because data grid is dynamic. later on you can remove the borders to make it as label or text box it is up to u.
You tagged the question with asp-classic but talk about textboxes and placeholders so I assume you are using ASP.NET Web Forms.
It's hard to say what your situation is without example code but would this be helpful?
http://webdevel.blogspot.fi/2009/05/get-textbox-values-from-repeater-easy.html
I am new in .Net and I had a question regarding creating dynamic tables.
I am creating a page that adds a new row to a table (First Name, Last Name, Address, etc...) when a user clicks on a button. I have been reading that every time you do a postback to a dynamic table you have to re-create the rows. That is what I am doing.
I have been testing this and about 40 rows have being added already, when I click to add a new row, it runs completely slower and I can only imagine how long it would take to add 100 rows. I am assuming that it is because re-creating the rows takes time.
My questions is there a better approach or another way to accomplish task?
'***Edits Here what im currently doing
This is my table and button control:
Code when button is clicked, which creates the dynamic table:
Dim tblrow As TableRow
Dim tblcell As TableCell
Dim inputText As TextBox
tblrow = New TableHeaderRow
tblcell = New TableHeaderCell
tblcell.Text = tableCount 'variable used to count rows in the table
tblcell.HorizontalAlign = HorizontalAlign.Left
tblrow.Cells.Add(tblcell)
tblcell = New TableCell
inputText = New TextBox
inputText.ID = "txt_" & tableCount
tblcell.Controls.Add(inputText)
tblrow.Cells.Add(tblcell)
table1.rows.add(tblrow)
Now from what I learned and tested so far, everytime I do a postback I have to rebuild this table in order to keep all of the contents I entered into the table.
The next question is why dont you add another row using jquery so that you don't have to do postbacks. I have tried this approach and it worked well UNTIL I needed to put the information entered into the table into a database which required a postback. So I was back at my original problem.
Note, if there is a better way to approach this im all ears. Like I said before I am new to this language and im just trying to learn.
My suggestion for you will be to use the JqGrid it is a free open source jquery plugin but also available commercially. It is fast in data loading and have lots of dynamic features
If you know javascript and jquery then this will be easy for you to use, it comes as Asp.Net, mvc and php component
Are you using windows forms? If you are, use the ListView control and change the view (I believe thats what it's called...) to details. Then you can use one of the many tutorials like this to populate the list:
If not using Windows Forums sorry, I saw the VB.net part and assumed.
asp.net is a server side web technology which means that you are working with a stateless technology (basically). That is why you have to rebuild everything on every postback.
There are several ways of dealing with this:
using the asp.net ViewState and check for Page.IsPostback on PageLoad
using jQuery and clientside Templates like Pure and fetch the Data through webservices
I on your behalf would avoid creating a table manually like you showed above, but instead use templated controls and specify the behavior declaratively.
When you create a DataGrid for example and are using ViewState, you do not have to explicitly recreate the DataGrid, since asp.net is taking care of it when used correctly.
i am trying to load this xml file into the dropdownlist:
http://sites.google.com/site/shadchanproject/Home/lots1.xml
i want to only load the a7190, a7193 etc... (there are only three of them i believe)
please help!
i am doing this in asp.net
Hmm, I'm having issues downloading the file.
But in general, if you can download the XML file and create an XSD file based off this (there are several XSD generators out there), you can then create a DataSet object that will read the XML data into a DataTable.
From there, you can create a DataView, filter out the other items so that only the desired elements remain, and bind the DataView to the drop down list.
EDIT: Well, I looked and its simpler than that. You should be able to read the XML file straight in. You won't have a strongly typed table, but you can do this:
Dim dsStuff As New DataSet()
dsStuff.ReadXml("PathToFile")
Dim dvStuff As New DataView(dsStuff.Tables(0))
dvStuff.Sort = "Name = 'FilteredName'"
ddlStuff.DataSource = dvStuff
ddlStuff.DataTextField = "Name"
ddlStuff.DataValueField = "ID"
ddlStuff.DataBind()
Adjust the filter criteria accordingly.