使用此基本示例重现该问题。当按下按钮(导致回发)时,<li>
消失,并按预期留下空<ol>
标签。我在页面后面的C#代码中动态添加<li>
标记,请参阅提供的示例。我试图在帖子后面保留这些信息。非常感谢任何帮助。
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="State.Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Example</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<ol id="myOl" runat="server">
</ol>
<asp:Button ID="Button1" runat="server" Text="Button" />
<asp:TextBox ID="TextBox1" runat="server" Text=""></asp:TextBox>
</div>
</form>
</body>
</html>
protected void Page_Load(object sender, EventArgs e)
{
EnableViewState = false;
if (TextBox1.Text == string.Empty)
PopulateTextBoxes(TextBox1, myOl);
}
private static void PopulateTextBoxes(ITextControl textBox1, Control bulletedList)
{
textBox1.Text = "TextBox1";
var li = new ListItem`enter code here`
{
Text = "Item1"
};
var bl = new BulletedList();
bl.Items.Add(li);
bl.DataBind();
bulletedList.Controls.Add(bl);
}
答案 0 :(得分:0)
其实我不确定你在尝试什么,但是如果你想要一个子弹列表,在每个按钮上添加项目,请点击试试这个:
<强> ASPX 强>
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication2.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:BulletedList runat="server" ID="bulletList"/>
<asp:Button ID="Button1" runat="server" Text="Button" />
<asp:TextBox ID="TextBox1" runat="server" Text=""></asp:TextBox>
</div>
</form>
</body>
</html>
<强>代码隐藏强>
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (TextBox1.Text == string.Empty)
{
PopulateTextBoxes();
}
}
private void PopulateTextBoxes()
{
var li = new ListItem()
{
Text = "Item1"
};
bulletList.Items.Add(li);
}
}
以下是有关BulletedList控件的更多信息:MSDN
当您禁用ViewState时,页面无法在回发之间保持其状态,因此您只应在需要减少网络流量时禁用它。