I need to convert a Html Table to DataTable in C#. I used HtmlAgilityPack but it does not convert it well because of rowspans.
The code I am currently using is:
private static DataTable convertHtmlTableToDataTable()
{
WebClient webClient = new WebClient();
string urlContent = webClient.DownloadString("http://example.com");
string tableCode = getTableCode(urlContent);
string htmlCode = tableCode.Replace(" ", " ");
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(htmlCode);
var headers = doc.DocumentNode.SelectNodes("//tr/th");
DataTable table = new DataTable();
foreach (HtmlNode header in headers)
{
table.Columns.Add(header.InnerText);
}
foreach (var row in doc.DocumentNode.SelectNodes("//tr[td]"))
{
table.Rows.Add(row.SelectNodes("td").Select(td => td.InnerText).ToArray());
}
return table;
}
And this is a part of Html Table:
<table class="tabel" cellspacing="0" border="0">
<caption style="font-family:Verdana; font-size:20px;">SEMGRP</caption>
<tr>
<th class="celula" >Ora</th>
<th class="latime_celula celula">Luni</th>
<th class="latime_celula celula">Marti</th>
<th class="latime_celula celula">Miercuri</th>
<th class="latime_celula celula">Joi</th>
<th class="latime_celula celula">Vineri</th>
</tr>
<tr>
<td class="celula" nowrap="nowrap">8-9</td>
<td class="celula" rowspan="2">
<table border="0" align="center">
<tr>
<td nowrap="nowrap" align="center">
Curs
<br />
<a class="link_celula" href="afis_n0.php?id_tip=287&tip=p">Prof</a>
<br />
<a class="link_celula" href="afis_n0.php?id_tip=9&tip=s">Sala</a>
<br />
</td>
</tr>
</table>
</td>
<td class="celula" rowspan="2">
<table border="0" align="center">
<tr>
<td nowrap="nowrap" align="center">
Curs
<br />
<a class="link_celula" href="afis_n0.php?id_tip=287&tip=p">Prof</a>
<br />
<a class="link_celula" href="afis_n0.php?id_tip=12&tip=s">Sala</a>
<br />
</td>
</tr>
</table>
</td>
<td class="celula"> </td>
<td class="celula"> </td>
<td class="celula" rowspan="2">
<table border="0" align="center">
<tr>
<td nowrap="nowrap" align="center">
Curs
<br />
<a class="link_celula" href="afis_n0.php?id_tip=293&tip=p">Prof</a>
<br />
<a class="link_celula" href="afis_n0.php?id_tip=9&tip=s">Sala</a>
<br />
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td class="celula" nowrap="nowrap">9-10</td>
<td class="celula"> </td>
<td class="celula"> </td>
</tr>
<tr>
<td class="celula" nowrap="nowrap">10-11</td>
<td class="celula" rowspan="2">
<table border="0" align="center">
<tr>
<td nowrap="nowrap" align="center"> Curs
<br /><a class="link_celula" href="afis_n0.php?id_tip=303&tip=p">Prof</a>
<br /><a class="link_celula" href="afis_n0.php?id_tip=9&tip=s">Sala</a>
<br />
</td>
</tr>
</table>
</td>
<td class="celula" rowspan="2">
<table border="0" align="center">
<tr>
<td nowrap="nowrap" align="center"> Curs
<br />
<a class="link_celula" href="afis_n0.php?id_tip=331&tip=p">Prof</a>
<br />
<a class="link_celula" href="afis_n0.php?id_tip=14&tip=s">Sala</a>
<br />
</td>
</tr>
</table>
</td>
<td class="celula" rowspan="2">
<table border="0" align="center">
<tr>
<td nowrap="nowrap" align="center"> Curs
<br /><a class="link_celula" href="afis_n0.php?id_tip=330&tip=p">Prof</a>
<br /><a class="link_celula" href="afis_n0.php?id_tip=9&tip=s">Sala</a>
<br />
</td>
</tr>
</table>
</td>
<td class="celula"> </td>
<td class="celula" rowspan="2">
<table border="0" align="center">
<tr>
<td nowrap="nowrap" align="center"> Curs
<br />
<a class="link_celula" href="afis_n0.php?id_tip=293&tip=p">Prof</a>
<br />
<a class="link_celula" href="afis_n0.php?id_tip=10&tip=s">Sala</a> <br />
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td class="celula" nowrap="nowrap">11-12</td>
<td class="celula"> </td>
</tr>
<tr>
I tried some solutions but I did not find any good...
Thanks for any help in advance.
Related
I am trying to parse HTML tables, but the tables are not equal in rows with different row numbers, all tables under (form) I am selecting the (form) as SingleNode, but the (tbody) came the row not (td), I can't loop for all (td).
Part of the HTML code:
<form name="DetailsForm" method="post" action="">
<input type="hidden" name="helpPageId" value="WF03">
<input type="hidden" name="withMenu" value="1">
<table width="100%" cellspacing="0" border="0">
<tbody>
<tr valign="center">
<td class="blackHeadingLeft">Details</td>
</tr>
<tr></tr>
<tr>
<td></td>
</tr>
</tbody>
</table>
<table width="100%" cellspacing="0" border="0">
<tbody>
<tr>
<td class="whiteTd" height="21"> AWB:</td>
<td class="whiteTdNormal" nowrap="nowrap" height="21"> 7777995585 </td>
<td class="whiteTd" nowrap="nowrap" height="21"> No of Shipment Details:</td>
<td class="whiteTdNormal" nowrap="nowrap" height="21"> 1 </td>
<td class="whiteTdNormal" width="100%" height="21"> </td>
</tr>
</tbody>
</table>
<table class="bordered-table" width="100%" border="0">
<tbody>
<tr>
<td class="grayTd" width="5%" height="21"> Details</td>
<td class="grayTd" width="5%" height="21" align="center"> Orig</td>
<td class="grayTd" width="8%" height="21" align="center"> Location</td>
<td class="grayTd" width="7%" height="21"> Dest</td>
<td class="grayTd" width="5%" height="21" align="center"> Pcs</td>
<td class="grayTd" width="5%" height="21"> Weight(kg)</td>
<td class="grayTd" width="11%" height="21"> Volumetric Weight(kg)</td>
<td class="grayTd" width="9%" height="21"> Date/Time</td>
<td class="grayTd" width="8%" height="21"> Route/Cycle</td>
<td class="grayTd" width="8%" height="21"> Post Code</td>
<td class="grayTd" width="6%" height="21"> Product</td>
<td class="grayTd" width="9%" height="21"> Amount</td>
<td class="grayTd" width="9%" height="21"> Duplicate</td>
</tr>
Here is the way that I am able to do it:
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(html);
foreach (HtmlNode table in doc.DocumentNode.SelectNodes("//table"))
{
Console.WriteLine("Table: ");
foreach (HtmlNode tbody in table.SelectNodes("tbody"))
{
if (tbody.ChildNodes.Any(x => x.Name == "tr"))
{
Console.WriteLine("TBody: ");
foreach (HtmlNode cell in tbody.SelectNodes("tr"))
{
Console.WriteLine("TR: ");
if (cell.ChildNodes.Any(c => c.Name == "td"))
{
foreach (var item in cell.SelectNodes("td"))
{
Console.WriteLine("TD: ");
Console.WriteLine(item.InnerHtml);
}
}
Console.WriteLine();
}
}
}
}
This way it doesn't matter how many tr or td tags there are. One thing to note is that you have to add validation if there is a case in which there are no tr or td tags in the tbody.
I hope this helps.
Edited to include validation for tr and td tags. A similar logic can be used for all other tags that might be missing.
I have string html, I want to get all id name of tag in string html.
get string html in file text:
<tr>
<td class="X8">
</td>
<td colspan="6" class="X9"></td>
<td colspan="4" class="X12" id="closedate">
</td>
<td colspan="6" class="X9"></td>
<td colspan="4" class="X12" id="startdate">
</td>
<td class="X8">
</td>
<td class="X8" colspan="3">
</td>
<td class="X8">
</td>
<td colspan="9" class="X9"></td>
<td colspan="6" class="X15" id="totalpayment"></td>
<td class="X8">
</td>
<td class="X8">
</td>
</tr>
<tr>
<td class="X17">
</td>
<td class="X17" colspan="8">
</td>
<td class="X17" colspan="33">
</td>
<td class="X17">
</td>
</tr>
<tr>
<td class="X17">
</td>
<td class="X17" colspan="8">
<td class="X17" colspan="16">
</td>
<td class="X17">
</td>
<td colspan="9" class="X20"></td>
<td colspan="6" class="X23" id="approvaldate"></td>
<td class="X17">
</td>
<td class="X17">
</td>
</tr>
expected results:
closedate, startdate,totalpayment, approvaldate.
Then I want to set inner text for id name tag
(Ex:<td colspan="6" class="X23" id="approvaldate">2018/07/18</td>)
Using c#.Help me, please. Thanks a lot.
What I am understood from your question is you need the id of all in string simple Example Created for you
<form id="form1" runat="server">
<input id="Name" type="text" name="Full Name" runat="server" />
<input id="Email" type="text" name="Email Address" runat="server" />
<input id="Phone" type="text" name="Phone Number" runat="server" />
</form>
foreach (var control in Page.Form.Controls)
{
if (control is HtmlInputControl)
{
var htmlInputControl = control as HtmlInputControl;
string controlName = htmlInputControl.Name;
string controlId = htmlInputControl.ID;
}
}
Another Approach:-
HtmlElement table = testWebBrowser.Document.GetElementById("TableID");
if (table != null)
{
foreach (HtmlElement row in table.GetElementsByTagName("TR"))
{
// ...
}
}
So basically i have Web page form named Lab4.aspx but everytime i pressed button, it will always direct to script runat="server" and not the aspx.cs,also the button is changed back to Button_1 when i changed it to BtnSubmit
<script runat="server">
Protected Sub Button1_Click(sender As Object, e As EventArgs)
End Sub
</script>
For the aspx.cs, i already changed the public partial class to public partial class Lab4: System.Web.UI.Page in the aspx.cs and also name the file to Lab4.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Lab4 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void BtnSubmit_Click(object sender, EventArgs e)
It would be great if this mystery can be solved because i have tried search for this solution everywhere
Here's the code for the button control on aspx page
<asp:Button ID="BtnSubmit" runat="server" onclick="Button1_Click" Text="Submit"
Width="85px" />
and this is the full code for aspx page
<script runat="server">
Protected Sub Button1_Click(sender As Object, e As EventArgs)
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.style1
{
font-size: xx-large;
font-family: "Tw Cen MT";
height: 1161px;
}
.style2
{
text-decoration: underline;
}
.style3
{
font-size: x-large;
background-color: #9933FF;
}
.style4
{
width: 100%;
}
.style5
{
width: 243px;
}
.style6
{
width: 609px;
}
.style7
{
width: 243px;
height: 32px;
}
.style8
{
width: 609px;
height: 32px;
background-color: #FFFFFF;
}
.style9
{
height: 32px;
}
.style10
{
background-color: #9966FF;
}
.style11
{
width: 243px;
height: 31px;
}
.style12
{
width: 609px;
height: 31px;
}
.style13
{
height: 31px;
}
.style14
{
width: 303px;
font-size: x-large;
}
.style15
{
width: 303px;
height: 41px;
}
.style16
{
height: 41px;
}
.style17
{
font-size: x-large;
}
.style20
{
width: 254px;
}
.style21
{
width: 254px;
height: 41px;
}
.style22
{
width: 243px;
height: 45px;
}
.style23
{
width: 609px;
height: 45px;
}
.style24
{
height: 45px;
}
.style25
{
width: 303px;
font-size: x-large;
height: 32px;
}
.style26
{
width: 254px;
height: 32px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div class="style1">
Nasi Tapau O<span class="style2">nline Order
</span>
<br />
<br />
<span class="style3"><span class="style10">Customer's Details
</span>
<br />
<table class="style4">
<tr>
<td class="style5">
Name :
</td>
<td class="style6">
<span class="style3">
<asp:TextBox ID="TxtName" runat="server" Width="545px"></asp:TextBox>
</span>
</td>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td class="style7">
</td>
<td class="style8">
</td>
<td class="style9">
</td>
<td class="style9">
</td>
</tr>
<tr>
<td class="style5">
Contact Number :</td>
<td class="style6">
<span class="style3">
<asp:TextBox ID="TxtContactNo" runat="server" Width="165px"></asp:TextBox>
</span>
</td>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td class="style5">
</td>
<td class="style6">
</td>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td class="style5">
Email :</td>
<td class="style6">
<span class="style3">
<asp:TextBox ID="TxtEmail" runat="server" Width="165px"></asp:TextBox>
</span>
</td>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td class="style7">
</td>
<td class="style8">
</td>
<td class="style9">
</td>
<td class="style9">
</td>
</tr>
<tr>
<td class="style5">
Order Details :</td>
<td class="style6">
</td>
<td>
Quantity :
</td>
<td>
</td>
</tr>
<tr>
<td class="style5">
</td>
<td class="style6">
</td>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td class="style5">
Rice :</td>
<td class="style6">
<asp:RadioButton ID="RBYes" runat="server" Text="Yes" />
<span class="style3"><asp:RadioButton ID="RBNo" runat="server"
style="background-color: #FFFFFF" Text="No" />
</span>
</td>
<td>
<span class="style3">
<asp:TextBox ID="TxtQRice" runat="server" Width="165px"></asp:TextBox>
</span>
</td>
<td>
</td>
</tr>
<tr>
<td class="style22">
</td>
<td class="style23">
</td>
<td class="style24">
</td>
<td class="style24">
</td>
</tr>
<tr>
<td class="style5">
Side Dishes :</td>
<td class="style6">
<asp:CheckBox ID="CBChicken" runat="server" style="background-color: #FFFFFF" Text="Chicken Curry - RM4.50" />
</td>
<td>
<span class="style3">
<asp:TextBox ID="TxtQChicken" runat="server" Width="165px"></asp:TextBox>
</span>
</td>
<td>
</td>
</tr>
<tr>
<td class="style7">
</td>
<td class="style8">
<span class="style3">
<asp:CheckBox ID="CBMixed" runat="server" OnCheckedChanged="CBMixed_CheckedChanged" style="background-color: #FFFFFF" Text="Mixed Vegetables - RM2.50" />
</span>
</td>
<td class="style9">
<span class="style3">
<asp:TextBox ID="TxtQMixed" runat="server" Width="165px"></asp:TextBox>
</span>
</td>
<td class="style9">
</td>
</tr>
<tr>
<td class="style5">
</td>
<td class="style6">
<span class="style3">
<asp:CheckBox ID="CBGrilled" runat="server" style="background-color: #FFFFFF" Text="Grilled Fish - RM4.00" />
</span>
</td>
<td>
<span class="style3">
<asp:TextBox ID="TxtQGrilled" runat="server" Width="165px"></asp:TextBox>
</span>
</td>
<td>
</td>
</tr>
<tr>
<td class="style7">
</td>
<td class="style8">
<span class="style3">
<asp:CheckBox ID="CBCondiment" runat="server" style="background-color: #FFFFFF" Text="Condiment - RM1.50" />
</span>
</td>
<td class="style9">
<span class="style3">
<asp:TextBox ID="TxtQCondiment" runat="server" Width="165px"></asp:TextBox>
</span>
</td>
<td class="style9">
</td>
</tr>
<tr>
<td class="style5">
Beverages :</td>
<td class="style6">
<span class="style3">
<asp:CheckBox ID="CBMangoJ" runat="server" style="background-color: #FFFFFF" Text="Mango Juice - RM3.00" />
</span>
</td>
<td>
<span class="style3">
<asp:TextBox ID="TxtMangoJuice" runat="server" Width="165px"></asp:TextBox>
</span>
</td>
<td>
</td>
</tr>
<tr>
<td class="style5">
</td>
<td class="style6">
<span class="style3">
<asp:CheckBox ID="CBCendol" runat="server" style="background-color: #FFFFFF" Text="Cendol - RM3.00" />
</span>
</td>
<td>
<span class="style3">
<asp:TextBox ID="TxtCendol" runat="server" Width="165px"></asp:TextBox>
</span>
</td>
<td>
</td>
</tr>
<tr>
<td class="style5">
</td>
<td class="style6">
</td>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td class="style11">
Delivery:</td>
<td class="style12">
<asp:DropDownList ID="DdlDelivery" runat="server">
<asp:ListItem Value="0">Self-pickup</asp:ListItem>
<asp:ListItem Value="1.50">Request For Delivery</asp:ListItem>
</asp:DropDownList>
</td>
<td class="style13">
</td>
<td class="style13">
</td>
</tr>
<tr>
<td class="style11">
</td>
<td class="style12">
</td>
<td class="style13">
<asp:Button ID="BtnSubmit" runat="server" onclick="Button1_Click" Text="Submit"
Width="85px" />
</td>
<td class="style13">
</td>
</tr>
</table>
<br />
<br />
<asp:Label ID="Label1" runat="server" style="background-color: #9966FF"
Text="Thank you for choosing us! Here are you order details :"></asp:Label>
<br />
<br />
</span>
<table class="style4">
<tr>
<td class="style14">
<span class="style17">Name :
</td>
<td class="style20">
<asp:Label ID="lblName" runat="server" Font-Size="X-Large"></asp:Label>
</span>
</td>
<td>
</td>
</tr>
<tr>
<td class="style14">
</td>
<td class="style20">
</td>
<td>
</td>
</tr>
<tr>
<td class="style14">
Telephone :</td>
<td class="style20">
<span class="style17">
<asp:Label ID="LblTel" runat="server"></asp:Label>
</span>
</td>
<td>
</td>
</tr>
<tr>
<td class="style14">
</td>
<td class="style20">
</td>
<td>
</td>
</tr>
<tr>
<td class="style14">
Email :</td>
<td class="style20">
<span class="style17">
<asp:Label ID="lblEmail" runat="server"></asp:Label>
</span>
</td>
<td>
</td>
</tr>
<tr>
<td class="style14">
</td>
<td class="style20">
</td>
<td style="font-size: x-large">
Quantity :</td>
</tr>
<tr>
<td class="style25">
Rice :</td>
<td class="style26">
<span class="style17">
<asp:Label ID="lblRice" runat="server"></asp:Label>
</span>
</td>
<td class="style9">
<span class="style17">
<asp:Label ID="lblQRice" runat="server"></asp:Label>
</span>
</td>
</tr>
<tr>
<td class="style14">
</td>
<td class="style20">
</td>
<td>
</td>
</tr>
<tr>
<td class="style14">
Side Dishes :</td>
<td class="style20">
<span class="style17">
<asp:Label ID="Label6" runat="server" Visible="False"></asp:Label>
</span>
</td>
<td>
<span class="style17">
<asp:Label ID="Label16" runat="server" Visible="False"></asp:Label>
</span>
</td>
</tr>
<tr>
<td class="style14">
</td>
<td class="style20">
<span class="style17">
<asp:Label ID="Label7" runat="server" Visible="False"></asp:Label>
</span>
</td>
<td>
<span class="style17">
<asp:Label ID="Label17" runat="server" Visible="False"></asp:Label>
</span>
</td>
</tr>
<tr>
<td class="style15">
</td>
<td class="style21">
<span class="style17">
<asp:Label ID="Label8" runat="server" Visible="False"></asp:Label>
</span>
</td>
<td class="style16">
<span class="style17">
<asp:Label ID="Label18" runat="server" Visible="False"></asp:Label>
</span>
</td>
</tr>
<tr>
<td class="style14">
</td>
<td class="style20">
<span class="style17">
<asp:Label ID="Label9" runat="server" Visible="False"></asp:Label>
</span>
</td>
<td>
<span class="style17">
<asp:Label ID="Label19" runat="server" Visible="False"></asp:Label>
</span>
</td>
</tr>
<tr>
<td class="style14">
Beverages :</td>
<td class="style20">
<span class="style17">
<asp:Label ID="lblMangoJ" runat="server"></asp:Label>
</span>
</td>
<td>
<span class="style17">
<asp:Label ID="lblQMangoJ" runat="server"></asp:Label>
</span>
</td>
</tr>
<tr>
<td class="style14">
</td>
<td class="style20">
<span class="style17">
<asp:Label ID="lblCendol" runat="server"></asp:Label>
</span>
</td>
<td>
<span class="style17">
<asp:Label ID="lblQCendol" runat="server"></asp:Label>
</span>
</td>
</tr>
<tr>
<td class="style14">
</td>
<td class="style20">
</td>
<td>
</td>
</tr>
<tr>
<td class="style14">
Delivery Method :</td>
<td class="style20">
<span class="style17">
<asp:Label ID="lblDelivery" runat="server"></asp:Label>
</span>
</td>
<td>
</td>
</tr>
<tr>
<td class="style14">
</td>
<td class="style20">
</td>
<td>
</td>
</tr>
<tr>
<td class="style14">
Total :</td>
<td class="style20">
<span class="style17">
<asp:Label ID="lblTotal" runat="server"></asp:Label>
</span>
</td>
<td>
</td>
</tr>
<tr>
<td class="style14">
</td>
<td class="style20">
</td>
<td>
</td>
</tr>
<tr>
<td class="style14">
Grand Total(Inclusive GST) :</td>
<td class="style20">
<span class="style17">
<asp:Label ID="lblTotalWGST" runat="server"></asp:Label>
</span>
</td>
<td>
</td>
</tr>
<tr>
<td class="style14">
</td>
<td class="style20">
</td>
<td>
</td>
</tr>
<tr>
<td class="style14">
</td>
<td class="style20">
</td>
<td>
</td>
</tr>
<tr>
<td class="style14">
</td>
<td class="style20">
</td>
<td>
</td>
</tr>
<tr>
<td class="style14">
</td>
<td class="style20">
</td>
<td>
</td>
</tr>
</table>
<br />
<br />
</div>
</form>
</body>
</html>
Your aspx page should actually start with something like this.
<%# Page Title="" Language="C#" MasterPageFile="" AutoEventWireup="true" CodeBehind="Lab4.aspx.cs" Inherits="" %>
CodeBehind="Lab4.aspx.cs" is the place where you specify which is your code behind file in C#.
Every aspx will have a page directive which will tell compiler where to look for its code file
<%# Page CodeFile="Lab4.aspx.cs" %>
documentation
I am facing an error in generating a proper PDF Document. the code i have, can generate a pdf document, it can download the document, but the issue is i cannot view the view the document. This is the Page I am trying to export to pdf.
Here is my code so far:
ASPX:
<asp:Button ID="btnDownload" CssClass="btn" runat="server" Text="Download Invoice" OnClick="btnDownload_Click" />
<asp:Panel ID="pnl" runat="server">
<div id="page-wrap">
<textarea id="header" style="height: 30px">PAYMENT DETAILS</textarea>
<div id="identity">
<textarea style="background-color: #F7F7F7;" readonly="readonly" id="address">My Name
My Street Address
Phone: 111-111-111</textarea>
<div id="logo">
<div id="logoctr">
</div>
<div id="logohelp">
<input id="imageloc" readonly="readonly" type="text" size="50" value="" /><br />
(max width: 540px, max height: 100px)
</div>
<img id="image" src="images/logo.png" alt="logo" />
</div>
</div>
<div style="clear: both"></div>
<div id="customer">
<textarea id="tbCustomer" readonly="readonly" runat="server"></textarea>
<table id="meta">
<tr>
<td class="meta-head">Payment ID</td>
<td>
<textarea readonly="readonly" runat="server" id="tbPID"></textarea></td>
</tr>
<tr>
<td class="meta-head">Date</td>
<td>
<textarea id="date" readonly="readonly" runat="server"></textarea></td>
</tr>
<tr>
<td class="meta-head">Amount Due</td>
<td>
<div class="due">
$
<asp:Label ID="lblTotal" runat="server" Text="Total"></asp:Label>
</div>
</td>
</tr>
</table>
</div>
<table id="items">
<tr>
<th>Property Title</th>
<th>Description</th>
<th>Status</th>
<th>Invoiced By</th>
<th>Total Payment</th>
</tr>
<tr class="item-row">
<td class="item-name">
<div class="delete-wpr">
<textarea readonly="readonly" id="tbTitle" runat="server"></textarea>
</div>
</td>
<td class="description">
<div contenteditable="true" id="tbDetail" class="blank" runat="server">
</div>
</td>
<td>
<textarea id="tbStatus" runat="server" readonly="readonly">PAID</textarea></td>
<td>
<textarea class="qty" readonly="readonly" id="tbInvoicedBy" runat="server"></textarea></td>
<td><span class="price">$
<asp:Label ID="tbTotal1" runat="server" Text="Total"></asp:Label></span></td>
</tr>
<tr>
<td colspan="2" class="blank"></td>
<td colspan="2" class="total-line">Vaccant</td>
<td class="total-value">
<div id="subtotal">$<asp:Label ID="lblVaccant" runat="server" Text=""></asp:Label></div>
</td>
</tr>
<tr>
<td colspan="2" class="blank"></td>
<td colspan="2" class="total-line">Maintainance</td>
<td class="total-value">
<div id="total">$<asp:Label ID="lblMaintainance" runat="server" Text=""></asp:Label></div>
</td>
</tr>
<tr>
<td colspan="2" class="blank"></td>
<td colspan="2" class="total-line">Property Insurance</td>
<td class="total-value">
<div id="Insurance">$<asp:Label ID="lblInsurance" runat="server" Text=""></asp:Label></div>
</td>
</tr>
<tr>
<td colspan="2" class="blank"></td>
<td colspan="2" class="total-line">Dewa Bill</td>
<td class="total-value">
<div id="dewa">$<asp:Label ID="lblDewa" runat="server" Text=""></asp:Label></div>
</td>
</tr>
<tr>
<td colspan="2" class="blank"></td>
<td colspan="2" class="total-line">Furnishing Cost</td>
<td class="total-value">
<div id="FurnishingCost">$<asp:Label ID="lblFurnishing" runat="server" Text=""></asp:Label></div>
</td>
</tr>
<tr>
<td colspan="2" class="blank"></td>
<td colspan="2" class="total-line">Cleaning Fees</td>
<td class="total-value">
<div id="CleaningFees">$<asp:Label ID="lblCleaning" runat="server" Text=""></asp:Label></div>
</td>
</tr>
<tr>
<td colspan="2" class="blank"></td>
<td colspan="2" class="total-line">House Keeping</td>
<td class="total-value">
<div id="HouseKeeping">$<asp:Label ID="lblHouseKeeping" runat="server" Text=""></asp:Label></div>
</td>
</tr>
<tr>
<td colspan="2" class="blank"></td>
<td colspan="2" class="total-line">Next Rent Due</td>
<td class="total-value">
<div id="paid">$<asp:Label ID="lblNextRent" runat="server" Text=""></asp:Label></div>
</td>
</tr>
<tr>
<td colspan="2" class="blank"></td>
<td colspan="2" class="total-line">Rental Comission</td>
<td class="total-value">
<div id="RentalComission">$<asp:Label ID="lblRentalComission" runat="server" Text=""></asp:Label></div>
</td>
</tr>
<tr>
<td colspan="2" class="blank"></td>
<td colspan="2" class="total-line">Credit Card Fees</td>
<td class="total-value">
<div id="CreditCardFees">$<asp:Label ID="lblCreditCardFees" runat="server" Text=""></asp:Label></div>
</td>
</tr>
<tr>
<td colspan="2" class="blank"></td>
<td colspan="2" class="total-line">Pest Control</td>
<td class="total-value">
<div id="PestControl">$<asp:Label ID="lblPestControl" runat="server" Text=""></asp:Label></div>
</td>
</tr>
<tr>
<td colspan="2" class="blank"></td>
<td colspan="2" class="total-line">Chillar Utilities</td>
<td class="total-value">
<div id="ChillarUtilities">$<asp:Label ID="lblChillarUtilities" runat="server" Text=""></asp:Label></div>
</td>
</tr>
<tr>
<td colspan="2" class="blank"></td>
<td colspan="2" class="total-line">Du, etisilate wifi</td>
<td class="total-value">
<div id="DuEtisilatewifi">$<asp:Label ID="lblDuEtisilateWifi" runat="server" Text=""></asp:Label></div>
</td>
</tr>
<tr>
<td colspan="2" class="blank"></td>
<td colspan="2" class="total-line balance">Total Payment</td>
<td class="total-value balance">
<div class="due">$<asp:Label ID="lblTotal2" runat="server" Text=""></asp:Label></div>
</td>
</tr>
</table>
<div id="terms">
<h5>Terms</h5>
<textarea readonly="readonly">These payment details are final and non negotiable.</textarea>
</div>
</div>
</asp:Panel>
ASPX.CS
public void ExportToPDF()
{
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=Panel.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
pnl.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 100f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
sw.Close();
htmlparser.Close();
Response.Write(pdfDoc);
Response.End();
}
also it says the HTMLWorker class is obsolete.
I have an page called Update.aspx, and in it I am updating all values instead of filename. When I am going to update info, all values are fetched from the database to corresponding textboxes. But if I have a filename such as abc.jpg in the database and I go on the update page without browsing the file and update, it's inserted as null.
How can I keep the same file (i.e. abc.jpg) in database even though it's not updated by user?
This is my code:
string onlyname = string.Empty;
string filename = "";
string path = "";
if (FileUp.HasFile)
{
filename = FileUp.PostedFile.FileName;
path = Server.MapPath("/clients_images/") + FileUp.FileName;
onlyname = path.Substring(path.LastIndexOf("\\") + 1);
FileUp.SaveAs(path);
}
else
{
onlyname = dr["Client_Logo"].ToString().Trim();
}
param[4] = new SqlParameter("#Client_Logo", SqlDbType.VarChar, 200);
param[4].Value = onlyname.ToString().Trim();
cmd.Parameters.Add(param[4]);
ASPX code:
<table align="center" cellpadding="3" cellspacing="0" border="0" width="638" class="tableborder">
<tr>
<td colspan="2" class="mainbg" align="center" height="13">
<font class="general">Update Client Detail</font></td>
</tr>
<tr>
<td class="general" width="50%" align="right">
<b>Client Name :</b></td>
<td align="left">
<asp:TextBox ID="txtUpdateclientname" Text="" Columns = "30" CssClass="checkbox02" runat="server" onchange="Javascript: return initialCap(this);"></asp:TextBox>
<font class="mandatory">*</font>
</td>
</tr>
<tr>
<td class="general" width="50%" align="right">
<b>Company Name :</b></td>
<td align="left">
<asp:TextBox ID="updatecompanyname" Text="" Columns = "30" CssClass="checkbox02" runat="server" onchange="Javascript: return initialCap(this);"></asp:TextBox>
<font class="mandatory">*</font>
</td>
</tr>
<tr>
<td class="general" width="50%" align="right">
<b>Email :</b></td>
<td align="left">
<asp:TextBox ID="updateemail" Text="" Columns = "30" CssClass="checkbox02" runat="server"></asp:TextBox>
<font class="mandatory">*</font>
</td>
</tr>
<tr>
<td class="general" width="50%" valign="top" align="right">
<b>Logo :</b></td>
<td class="general" width="50%" align="left">
<asp:FileUpload ID="FileUp" runat="server" CssClass="checkbox02" />
<br /> (Height of image should not be more than 67 And Width of image should not be more than 380)
</td>
</tr>
<tr>
<td class="general" width="50%" align="right">
<%--<b><font class="bluetext"><strong> Upload : </strong>--%></td>
<td align="left">
<asp:LinkButton ID="viewImage" Text="View Existing" runat="server" cssClass="general-white" class="link" OnCommand="LinkButton_Command" CommandName="downloadfile" ToolTip="Click to View Existing" Font-Bold="true"></asp:LinkButton>
</td>
</tr>
<tr>
<td class="general" width="50%" align="right" valign="top">
<b>Project Type :</b></td>
<td width="100%" align="left" valign="top" colspan="2">
<table cellpadding="0" cellspacing="0" border="0">
<tr>
<td align="right" valign="top" colspan="3">
<asp:ListBox ID="LstUserLeft" Width="100%" Autoscroll="false" SelectionMode="Multiple" Height="150" CssClass="checkbox02"
runat="server">
</asp:ListBox>
</td>
</tr>
</table>
</td>
</tr>
<tr>
</tr>
</table>
You can use HiddenField or ViewState to store the filename from the Client_logo field (from the database).
For instance.
Assign the value to the HiddenField1 while retrieving result,
HiddenField1.Value=dr["Client_Logo"].ToString().Trim();
and while updating,
if (FileUp.HasFile)
{
...
}
else
{
onlyname = HiddenField1.Value;
}