利用ViewState["ProductFieldCount"]控制TextBox的数量
两个Button实现对其加减
ViewState["ProductFieldCount"]的值在改变
可是按"减"这个Button时 ViewState["ProductFieldCount"]减了一
可是TextBox并没有减少 看了很久也不知道哪里错了
请大家指点一下 谢谢!
源代码如下:<%@ Page Debug="true" %>
<script language="c#" runat="server">
void Page_Load(Object sender,EventArgs e)
{
int intCounter,ProductFieldCount;
if(!IsPostBack)
{
ViewState["ProductFieldCount"] = 1;
ViewState["PageLoadCount"] = 1;
ProductFieldCount = (int)ViewState["ProductFieldCount"];
}
else if((int)ViewState["ProductFieldCount"] > 1)
for(intCounter=2;intCounter <= (int)ViewState["ProductFieldCount"];intCounter++)
AddProductField( intCounter );
ViewState["PageLoadCount"] = (int)ViewState["PageLoadCount"] + 1;
}void AddProductFieldCount(Object sender , EventArgs e)
{
int ProductFieldCount = (int)ViewState["ProductFieldCount"];
ProductFieldCount += 1;
ViewState["ProductFieldCount"] = ProductFieldCount;
AddProductField( (int)ViewState["ProductFieldCount"] );
}void AddProductField( int strFieldNum )
{
LiteralControl litLabel;
TextBox txtTextBox; //Add Literal Control
litLabel = new LiteralControl();
litLabel.Text = "<p><b>Product" + strFieldNum.ToString() + ":</b>";
plhProductFields.Controls.Add( litLabel ); //Add TextBox Control
txtTextBox = new TextBox();
txtTextBox.ID = "txtProduct" + strFieldNum.ToString(); //注意这里ID
plhProductFields.Controls.Add( txtTextBox );
}void MinusProductFieldCount(Object sender , EventArgs e)
{
ViewState["ProductFieldCount"] = (int)ViewState["ProductFieldCount"] - 1;
if((int)ViewState["ProductFieldCount"] < 1)
ViewState["ProductFieldCount"] = 1;
} void btnSubmit_Click(Object sender,EventArgs e)
{
Response.Write( "Thankyou.aspx" );
}
</script><html>
<head><title>DynamicForm.aspx </title></head>
<body><form runat="server">
PageLoad = <%=ViewState["PageLoadCount"]%>
<br>
ProductFieldCount =
<%=ViewState["ProductFieldCount"]%>
<br><b>Customer Name:</b>
<asp:TextBox id="txtCustomer" runat="server" /><p>
<div style="background-color:yellow;padding:10px"><b>Product1:</b>
<asp:TextBox id="txtProduct1" runat="server" />
<asp:PlaceHolder id="plhProductFields" runat="server" /></div><p>
<asp:Button Text="Add Product Field"
OnClick="AddProductFieldCount" runat="server" /><asp:Button Text="Minus Product Field"
OnClick="MinusProductFieldCount" runat="server" /><asp:Button id="btnSubmit"
Text="Submit Complete Order"
OnClick="btnSubmit_Click" runat="server" />
</form></body>
</html>
两个Button实现对其加减
ViewState["ProductFieldCount"]的值在改变
可是按"减"这个Button时 ViewState["ProductFieldCount"]减了一
可是TextBox并没有减少 看了很久也不知道哪里错了
请大家指点一下 谢谢!
源代码如下:<%@ Page Debug="true" %>
<script language="c#" runat="server">
void Page_Load(Object sender,EventArgs e)
{
int intCounter,ProductFieldCount;
if(!IsPostBack)
{
ViewState["ProductFieldCount"] = 1;
ViewState["PageLoadCount"] = 1;
ProductFieldCount = (int)ViewState["ProductFieldCount"];
}
else if((int)ViewState["ProductFieldCount"] > 1)
for(intCounter=2;intCounter <= (int)ViewState["ProductFieldCount"];intCounter++)
AddProductField( intCounter );
ViewState["PageLoadCount"] = (int)ViewState["PageLoadCount"] + 1;
}void AddProductFieldCount(Object sender , EventArgs e)
{
int ProductFieldCount = (int)ViewState["ProductFieldCount"];
ProductFieldCount += 1;
ViewState["ProductFieldCount"] = ProductFieldCount;
AddProductField( (int)ViewState["ProductFieldCount"] );
}void AddProductField( int strFieldNum )
{
LiteralControl litLabel;
TextBox txtTextBox; //Add Literal Control
litLabel = new LiteralControl();
litLabel.Text = "<p><b>Product" + strFieldNum.ToString() + ":</b>";
plhProductFields.Controls.Add( litLabel ); //Add TextBox Control
txtTextBox = new TextBox();
txtTextBox.ID = "txtProduct" + strFieldNum.ToString(); //注意这里ID
plhProductFields.Controls.Add( txtTextBox );
}void MinusProductFieldCount(Object sender , EventArgs e)
{
ViewState["ProductFieldCount"] = (int)ViewState["ProductFieldCount"] - 1;
if((int)ViewState["ProductFieldCount"] < 1)
ViewState["ProductFieldCount"] = 1;
} void btnSubmit_Click(Object sender,EventArgs e)
{
Response.Write( "Thankyou.aspx" );
}
</script><html>
<head><title>DynamicForm.aspx </title></head>
<body><form runat="server">
PageLoad = <%=ViewState["PageLoadCount"]%>
<br>
ProductFieldCount =
<%=ViewState["ProductFieldCount"]%>
<br><b>Customer Name:</b>
<asp:TextBox id="txtCustomer" runat="server" /><p>
<div style="background-color:yellow;padding:10px"><b>Product1:</b>
<asp:TextBox id="txtProduct1" runat="server" />
<asp:PlaceHolder id="plhProductFields" runat="server" /></div><p>
<asp:Button Text="Add Product Field"
OnClick="AddProductFieldCount" runat="server" /><asp:Button Text="Minus Product Field"
OnClick="MinusProductFieldCount" runat="server" /><asp:Button id="btnSubmit"
Text="Submit Complete Order"
OnClick="btnSubmit_Click" runat="server" />
</form></body>
</html>
for(intCounter=2;intCounter <= (int)ViewState["ProductFieldCount"];intCounter++)
——————————————————————————————————
这里的>1和=2,我也看不懂?
包含一个用户顾客名的表单域和一个用于产品的表单域ViewState["ProductFieldCount"]代表页面中产品的个数,我想也可以认为是TextBox的个数
ViewState["ProductFieldCount"] > 1的意思是
当Product的个数>1时,就显示其他的Product的表单域
intCounter=2是不显示第一个Product 1
(因为有控件<asp:TextBox id="txtProduct1" runat="server" />)最原始的代码是实现动态产生表单,即利用PlaceHolder来添加其他的控件
即只有Add...
我想在原来的基础上实现减去控件的功能,就增加了Minus...
每次点击Minus...就对 ViewState["ProductFieldCount"]减一
提交server后在Page_Load中实现显示
但问题在于,按下Minus后ViewState["ProductFieldCount"]减了一,
而product的个数并没有减少,必须连续按才有效果不知这样解释能否看明白?
————————————————————————————
这是因为你并没有在Minus...之后更新(减少)Placeholder中的Product,在下一次刷新的时候才显示。另外你的逻辑好像很繁杂(重复和相关),自我捆绑的地方太多。我就不多说了,这需要慢功,学一些软件工程设计,养成逻辑、条理,即简单又不歧义的风格。就按照你的写法,可以这样改一下:(我没有测试,你理解一下意思自己改)void MinusProductFieldCount(Object sender , EventArgs e)
{
ViewState["ProductFieldCount"] = (int)ViewState["ProductFieldCount"] - 1;
if((int)ViewState["ProductFieldCount"] < 1)
ViewState["ProductFieldCount"] = 1;
plhProductFields.Controls.Clear();
if((int)ViewState["ProductFieldCount"] > 1)
for(intCounter=2;intCounter <= (int)ViewState["ProductFieldCount"];
intCounter++) AddProductField( intCounter );
}
每次点击Minus...就对 ViewState["ProductFieldCount"]减一
提交server后在Page_Load中实现显示
————————————————————————————————————————
你这个观念可有严重问题哦。asp.net机制不是这样的!你说的page_load并不是在Minus....方法执行之后执行的,因此也就没有你说的最后这个效果。其实,往往可能等不到下一个page_load执行,用户就已经认为你的程序写的太奇怪、不可用,而关闭浏览器或者去看别的页面了。
谢谢提醒 :)编程风格是要改改
没考虑到
plhProductFields.Controls.Clear();因为连续电击Minus...后会出现减少的现象
所以就猜测会执行完Minus...后调用Page_Load但通过ViewState["PageLoadCount"] 又发现确实是调用了Page_load
有点困惑