0
要重新生成問題,請創建VS一個項目用以下代碼:HTML標籤不改變AsyncPostBack
該代碼母版的形式標記中,並沒有對後面的代碼。
<asp:ScriptManager ID="scmManager" runat="server" AsyncPostBackTimeout="3600" />
<div>
<div id="divCContents">
<div id="divSideBar">
<asp:contentplaceholder ID="cphMenus" runat="server" />
</div>
<div id="divMContents">
<asp:contentplaceholder ID="cphPages" runat="server" />
</div>
</div>
</div>
該代碼的第一個ASP內:內容在TESTDefault.aspx
<asp:UpdatePanel ID="updCombos" runat="server">
<ContentTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" Width="170px" AutoPostBack="true"
OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
</asp:DropDownList>
<br />
<br />
<asp:DropDownList ID="DropDownList2" runat="server" Width="170px" AutoPostBack="true"
OnSelectedIndexChanged="DropDownList2_SelectedIndexChanged">
</asp:DropDownList>
</ContentTemplate>
</asp:UpdatePanel>
此代碼第二ASP內:內容在TESTDefault.aspx
<asp:UpdatePanel ID="updPanel" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Panel ID="pnlTest" runat="server" />
</ContentTemplate>
</asp:UpdatePanel>
這最後一個碼是在TESTDefault.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
subInitiateFirstCombo();
}
}
protected void subInitiateFirstCombo()
{
for (int xI = 0; xI < 5; xI++)
{
ListItem itmlist = new ListItem();
itmlist.Value = String.Format("Dep{0}", Convert.ToString(xI));
itmlist.Text = String.Format("Department{0}", Convert.ToString(xI));
DropDownList1.Items.Add(itmlist);
}
DropDownList1.SelectedIndex = 0;
subInitiateSecondCombo();
}
protected void subInitiateSecondCombo()
{
DropDownList2.Items.Clear();
for (int xI = 0; xI < 5; xI++)
{
ListItem itmlist = new ListItem();
itmlist.Value = String.Format("{0}_Ind{1}", (string)DropDownList1.SelectedItem.Value, Convert.ToString(xI));
itmlist.Text = String.Format("{0}_Indicator{1}", (string)DropDownList1.SelectedItem.Text, Convert.ToString(xI));
DropDownList2.Items.Add(itmlist);
}
DropDownList2.SelectedIndex = 0;
CreateNewTable();
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
subInitiateSecondCombo();
}
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
CreateNewTable();
}
protected void CreateNewTable()
{
int intCellCount = DropDownList1.SelectedIndex + 2;
pnlTest.Controls.Clear();
updPanel.Update();
using (Table tblTest = new Table())
{
tblTest.ID = (string)DropDownList2.SelectedItem.Value;
tblTest.Attributes.Add("style", "border-collapse: collapse; margin-top: 100px;");
for (int xI = 0; xI < 3; xI++)
{
using (TableRow tbrRow = new TableRow())
{
tbrRow.ID = String.Format("tbrRow{0}", Convert.ToString(xI));
for (int xJ = 0; xJ < intCellCount; xJ++)
{
using (TableCell tbcCell = new TableCell())
{
tbcCell.ID = String.Format("{0}_tbcCell{1}", tbrRow.ID, Convert.ToString(xJ));
tbcCell.Attributes.Add("style", String.Format("border: #808080 1px solid; " +
"font-family: Verdana; " +
"font-size: 8pt; " +
"color: #3b5998; " +
"text-align: center; " +
"vertical-align: middle;"));
tbcCell.Width = Unit.Pixel(100);
tbcCell.Height = Unit.Pixel(80);
tbcCell.Text = String.Format("{0}", tblTest.ID);
tbrRow.Controls.Add(tbcCell);
}
}
tblTest.Controls.Add(tbrRow);
}
}
pnlTest.Controls.Add(tblTest);
updPanel.Update();
}
}
運行該頁面並查看源代碼後,您將看到動態生成的表格的標籤。現在,從DropDownLists中選擇另一個選項來重新生成一個不同的表格,然後再次查看源代碼,您會發現舊錶格標籤仍然存在,並且沒有新表格的標誌(儘管它顯示在屏幕上)。
我發現它正確工作的唯一方法是強制完整的回發。有沒有其他方法?之所以我需要它會導致一些javascripts將會運行並且它必須獲取表ID。
希望我的例子很清楚。任何提示將非常感激。
乾杯。
查看源代碼不一定顯示頁面,因爲它存在於內存中。使用JavaScript或DOM調試工具來檢查是否進行了更改。 – 2009-11-20 17:59:22