当我从 .aspx 提交时,标签会重新绘制
我有一个正在运行一些简单代码的 .aspx 文件。当我“提交”表单时,我在标签(lblCount)中显示一些信息,该信息取决于下拉列表中的代码。当我多次提交时,它会重新创建标签 - 我不明白为什么,除非我缺少某种属性。是否有一个属性可以防止标签重绘?
<html>
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label runat="server" id="lblError" />
<asp:DropDownList id="monthList" AutoPostBack = "True" runat = "server">
<asp:ListItem Selected = "True" Value = "January"> January </asp:ListItem>
<asp:ListItem Value = "February"> February </asp:ListItem>
<asp:ListItem Value = "March"> March </asp:ListItem>
<asp:ListItem Value = "April"> April </asp:ListItem>
<asp:ListItem Value = "May"> May </asp:ListItem>
<asp:ListItem Value = "June"> June </asp:ListItem>
<asp:ListItem Value = "July"> July </asp:ListItem>
<asp:ListItem Value = "August"> August </asp:ListItem>
<asp:ListItem Value = "September"> September </asp:ListItem>
<asp:ListItem Value = "October"> October </asp:ListItem>
<asp:ListItem Value = "November"> November </asp:ListItem>
<asp:ListItem Value = "December"> December </asp:ListItem>
</asp:DropDownList>
<asp:Label runat="server" id="lblCount" />
</div>
<asp:Button ID="submitButton" OnClick="MonthSelection" Text="Submit" runat="server" />
<div>
</div>
</form>
</body>
</html>
月份选择:
protected void MonthSelection(object sender, EventArgs e)
{
dateLookup = monthList.SelectedItem.Value;
selectedMonth.Text = dateLookup.ToString();
switch (dateLookup)
{
case "January":
monthDate = 01;
break;
case "February":
monthDate = 02;
break;
case "March":
monthDate = 03;
break;
case "April":
monthDate = 04;
break;
case "May":
monthDate = 05;
break;
case "June":
monthDate = 06;
break;
case "July":
monthDate = 07;
break;
case "August":
monthDate = 08;
break;
case "September":
monthDate = 09;
break;
case "October":
monthDate = 10;
break;
case "November":
monthDate = 11;
break;
case "December":
monthDate = 12;
break;
}
try
{
string sql = "SELECT COUNT(*) FROM members_ WHERE DATEPART(month, DateUnsub_) = " + monthDate + " AND DATEPART(year, DateUnsub_) = 2011 AND DATEDIFF(day, DateJoined_, DateUnsub_) <= 30";
String[][] results = lm.SqlSelect(sql);
if (results != null)
{
for (int i = 0; i < results.Length; i++)
{
if (results[i] != null)
{
for (int j = 0; j < results[i].Length; j++)
{
if (results[i][j] != null)
{
if (results[i][j].Length > 5)
lblCount.Text += results[i][j];
else
lblCount.Text += results[i][j];
}
}
}
}
}
}
catch (SoapHeaderException ex)
{
lblError.Text = ex.Message;
}
}
I have a .aspx file that is running some simple code. When I "submit" my form, I am displaying some information in a label(lblCount) that is dependent on the code in my dropdownlist. When I submit more than once, it re-creates the label - I can't see why, unless I'm missing some sort of property. Is there a property that keeps the label from redrawing?
<html>
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label runat="server" id="lblError" />
<asp:DropDownList id="monthList" AutoPostBack = "True" runat = "server">
<asp:ListItem Selected = "True" Value = "January"> January </asp:ListItem>
<asp:ListItem Value = "February"> February </asp:ListItem>
<asp:ListItem Value = "March"> March </asp:ListItem>
<asp:ListItem Value = "April"> April </asp:ListItem>
<asp:ListItem Value = "May"> May </asp:ListItem>
<asp:ListItem Value = "June"> June </asp:ListItem>
<asp:ListItem Value = "July"> July </asp:ListItem>
<asp:ListItem Value = "August"> August </asp:ListItem>
<asp:ListItem Value = "September"> September </asp:ListItem>
<asp:ListItem Value = "October"> October </asp:ListItem>
<asp:ListItem Value = "November"> November </asp:ListItem>
<asp:ListItem Value = "December"> December </asp:ListItem>
</asp:DropDownList>
<asp:Label runat="server" id="lblCount" />
</div>
<asp:Button ID="submitButton" OnClick="MonthSelection" Text="Submit" runat="server" />
<div>
</div>
</form>
</body>
</html>
MONTH SELECTION:
protected void MonthSelection(object sender, EventArgs e)
{
dateLookup = monthList.SelectedItem.Value;
selectedMonth.Text = dateLookup.ToString();
switch (dateLookup)
{
case "January":
monthDate = 01;
break;
case "February":
monthDate = 02;
break;
case "March":
monthDate = 03;
break;
case "April":
monthDate = 04;
break;
case "May":
monthDate = 05;
break;
case "June":
monthDate = 06;
break;
case "July":
monthDate = 07;
break;
case "August":
monthDate = 08;
break;
case "September":
monthDate = 09;
break;
case "October":
monthDate = 10;
break;
case "November":
monthDate = 11;
break;
case "December":
monthDate = 12;
break;
}
try
{
string sql = "SELECT COUNT(*) FROM members_ WHERE DATEPART(month, DateUnsub_) = " + monthDate + " AND DATEPART(year, DateUnsub_) = 2011 AND DATEDIFF(day, DateJoined_, DateUnsub_) <= 30";
String[][] results = lm.SqlSelect(sql);
if (results != null)
{
for (int i = 0; i < results.Length; i++)
{
if (results[i] != null)
{
for (int j = 0; j < results[i].Length; j++)
{
if (results[i][j] != null)
{
if (results[i][j].Length > 5)
lblCount.Text += results[i][j];
else
lblCount.Text += results[i][j];
}
}
}
}
}
}
catch (SoapHeaderException ex)
{
lblError.Text = ex.Message;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我找出了问题所在——这是我没有真正看过的其他人的代码。他们正在增加价值(使用+=)。我只是拿走了加号,它起作用了。 叹息这一切都是为了这么小的事情。
I figured out what was wrong - it was somebody else's code that I hadn't really looked at. They were adding to the value (using +=). I just took away the plus, and it worked. sigh All that over something so small.