开发者

Label redraws when I submit from .aspx

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":
    开发者_如何学C        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 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜