I have a question .. I've been searching for a solution that match with my case but nothing work ..
I have simple Datatable with some data, then I Convert it to HTML and put in to Place Holder in runtime .. The table has contentEnable=true .. So, user can edit some data in the table ..
Now, what I want to do is get the updated table back into data table ..
I was follow this link asp:placeholder contents to string
but still it doesn't work for me .. can anyone please tell me what's wrong in my code, and tell me how to solve it ..
Here is my Default.aspx code:
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:Panel runat="server">
<asp:UpdatePanel runat="server">
<ContentTemplate>
<dx:ASPxPageControl ID="ASPxPageControl1" runat="server">
<TabPages>
<dx:TabPage>
<ContentCollection>
<dx:ContentControl>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Buat Data" />
<asp:PlaceHolder ID="PH1" runat="server"></asp:PlaceHolder>
<asp:Button ID="btn1" runat="server" OnClick="btn1_Click" Text="Buat Data" />
</dx:ContentControl>
</ContentCollection>
</dx:TabPage>
</TabPages>
</dx:ASPxPageControl>
</ContentTemplate>
</asp:UpdatePanel>
</asp:Panel>
</div>
</form>
And here is my Default.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
}
private void LoadData()
{
DataTable dt = new DataTable();
dt.Columns.Add("C1", typeof(string));
dt.Columns.Add("C2", typeof(string));
dt.Columns.Add("C3", typeof(string));
DataRow dr = dt.NewRow();
dr[0] = "Test 1";
dr[1] = "Test 2";
dr[2] = "Test 3";
dt.Rows.Add(dr);
string html = ConvertDataTableToHTML(dt);
PH1.Controls.Add(new Literal { Text = html });
}
protected void Button1_Click(object sender, EventArgs e)
{
LoadData();
}
public static string ConvertDataTableToHTML(DataTable dt)
{
string html = "<table border='1' class='rerere'>";
//add header row
html += "<tr>";
for (int i = 0; i < dt.Columns.Count; i++)
html += "<td>" + dt.Columns[i].ColumnName + "</td>";
html += "</tr>";
//add rows
for (int i = 0; i < dt.Rows.Count; i++)
{
html += "<tr>";
for (int j = 0; j < dt.Columns.Count; j++)
html += "<td contentEditable='true'>" + dt.Rows[i][j].ToString() + "</td>";
html += "</tr>";
}
html += "</table>";
return html.ToString();
}
public static DataTable ConvertHTMLTablesToDataTable(string HTML)
{
DataTable dt = null;
DataRow dr = null;
DataColumn dc = null;
string TableExpression = "<table[^>]*>(.*?)</table>";
string HeaderExpression = "<th[^>]*>(.*?)</th>";
string RowExpression = "<tr[^>]*>(.*?)</tr>";
string ColumnExpression = "<td[^>]*>(.*?)</td>";
bool HeadersExist = false;
int iCurrentColumn = 0;
int iCurrentRow = 0;
// Get a match for all the tables in the HTML
MatchCollection Tables = Regex.Matches(HTML, TableExpression, RegexOptions.Multiline | RegexOptions.Singleline | RegexOptions.IgnoreCase);
// Loop through each table element
foreach (Match Table in Tables)
{
// Reset the current row counter and the header flag
iCurrentRow = 0;
HeadersExist = false;
// Add a new table to the DataSet
dt = new DataTable();
// Create the relevant amount of columns for this table (use the headers if they exist, otherwise use default names)
if (Table.Value.Contains("<th"))
{
// Set the HeadersExist flag
HeadersExist = true;
// Get a match for all the rows in the table
MatchCollection Headers = Regex.Matches(Table.Value, HeaderExpression, RegexOptions.Multiline | RegexOptions.Singleline | RegexOptions.IgnoreCase);
// Loop through each header element
foreach (Match Header in Headers)
{
//dt.Columns.Add(Header.Groups(1).ToString);
dt.Columns.Add(Header.Groups[1].ToString());
}
}
else
{
for (int iColumns = 1; iColumns <= Regex.Matches(Regex.Matches(Regex.Matches(Table.Value, TableExpression, RegexOptions.Multiline | RegexOptions.Singleline | RegexOptions.IgnoreCase)[0].ToString(), RowExpression, RegexOptions.Multiline | RegexOptions.Singleline | RegexOptions.IgnoreCase)[0].ToString(), ColumnExpression, RegexOptions.Multiline | RegexOptions.Singleline | RegexOptions.IgnoreCase).Count; iColumns++)
{
dt.Columns.Add("Column " + iColumns);
}
}
// Get a match for all the rows in the table
MatchCollection Rows = Regex.Matches(Table.Value, RowExpression, RegexOptions.Multiline | RegexOptions.Singleline | RegexOptions.IgnoreCase);
// Loop through each row element
foreach (Match Row in Rows)
{
// Only loop through the row if it isn't a header row
if (!(iCurrentRow == 0 & HeadersExist == true))
{
// Create a new row and reset the current column counter
dr = dt.NewRow();
iCurrentColumn = 0;
// Get a match for all the columns in the row
MatchCollection Columns = Regex.Matches(Row.Value, ColumnExpression, RegexOptions.Multiline | RegexOptions.Singleline | RegexOptions.IgnoreCase);
// Loop through each column element
foreach (Match Column in Columns)
{
DataColumnCollection columns = dt.Columns;
if (!columns.Contains("Column " + iCurrentColumn))
{
//Add Columns
dt.Columns.Add("Column " + iCurrentColumn);
}
// Add the value to the DataRow
dr[iCurrentColumn] = Column.Groups[1].ToString();
// Increase the current column
iCurrentColumn += 1;
}
// Add the DataRow to the DataTable
dt.Rows.Add(dr);
}
// Increase the current row counter
iCurrentRow += 1;
}
}
return (dt);
}
protected void btn1_Click(object sender, EventArgs e)
{
System.IO.TextWriter tw = new System.IO.StringWriter();
HtmlTextWriter h = new HtmlTextWriter(tw);
PH1.RenderControl(h);
string html = tw.ToString();
DataTable dt = ConvertHTMLTablesToDataTable(html);
int n = dt.Rows.Count;
for (int i = 1; i < n; i++)
{
string a = dt.Rows[i][0].ToString();
string b = dt.Rows[i][1].ToString();
string c = dt.Rows[i][2].ToString();
}
}
So, with above code I always getting empty in TextWriter .. Can anyone tell me how to solve it?
Thanks, sorry for my bad English ...