我有一个问题.. 我一直在寻找符合我案例的解决方案,但没有任何效果......
我有一些简单的Datatable和一些数据,然后我将它转换为HTML并在运行时放入Place Holder。 该表有contentEnable = true .. 因此,用户可以编辑表格中的一些数据..
现在,我想要做的是将更新的表格重新放回数据表..
我关注此链接asp:placeholder contents to string
但它仍然不适合我.. 任何人都可以告诉我我的代码中有什么问题,并告诉我如何解决它..
这是我的Default.aspx代码:
<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>
这是我的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();
}
}
所以,使用上面的代码我总是在TextWriter中变空。 谁能告诉我如何解决它?
谢谢,抱歉我的英语不好......
答案 0 :(得分:0)
当您点击button1时,会发生回发,页面会重新加载,因此您会丢失占位符的内容
仅用于测试,请尝试插入:
protected void Page_Load(object sender, EventArgs e)
{
LoadData();
}
您将在调试器中看到btn1_Click有效:
string a = "Test 1"
string b = "Test 2"
string c = "Test 3"
在您的方法中:
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();
}
}