如何创建asp.net投票系统?

时间:2014-06-21 10:02:35

标签: c# asp.net

我一直在创建asp.net轮询系统。每件事都可以,但有问题 不要为什么会这样。例如,每次我刷新项目页面添加到按钮列表控件中的项目列表。每次我选择项目时它只显示一个项目(例如,everi时间显示我可怕的项目)。 这是我的代码:

         <asp:UpdatePanel ID="updatepanel1" runat="server">
           <ContentTemplate>
                <asp:Label ID="lblresult" runat="server" Text="Label"></asp:Label> 
               <br />
                  <asp:RadioButtonList ID="radVote" runat="server"  Width="91px" DataSourceID="LinqDataSource1" DataTextField="Id" DataValueField="Id">
                <asp:ListItem>perfect</asp:ListItem>
                <asp:ListItem>good</asp:ListItem>
                <asp:ListItem>bad</asp:ListItem>
                <asp:ListItem>terrible</asp:ListItem>
            </asp:RadioButtonList>
                  <asp:LinqDataSource ID="LinqDataSource1" runat="server" ContextTypeName="EnglishClass1.DataClasses1DataContext" EntityTypeName="" TableName="Polls">
                  </asp:LinqDataSource>
        <asp:Button ID="Savebtn" runat="server" Text="Save" BackColor="#40e023" ForeColor="Blue" OnClick="Savebtn_Click"/>
        <asp:Button ID="Showbtn" runat="server" Text="Show" BackColor="#40e023" ForeColor="Blue" OnClick="Showbtn_Click"/>
               &nbsp;
               <asp:Label ID="lblpoll" runat="server" Text="" ForeColor="Red" Font-Italic="true" Font-Size="Larger"></asp:Label>
           </ContentTemplate>
       </asp:UpdatePanel>

C#代码:

                     protected void Savebtn_Click(object sender, EventArgs e)
    {
        if (radVote.SelectedItem == null)

            lblpoll.Text = "لطفا در نظرسنجی شرکت کنید";
            else
            countVote(radVote.SelectedItem.ToString());

    }
    protected void countVote(string Thevote) 
    {
        try
        {
             string conn = ConfigurationManager.ConnectionStrings["EnglishDBConnectionString"].ToString();
        DataClasses1DataContext db = new DataClasses1DataContext(conn);
        Poll po = new Poll();
        po.Vote = Thevote;
        db.Polls.InsertOnSubmit(po);
        db.SubmitChanges();
        lblpoll.Text = "از حمایت شما متشکریم";
        readXML();
        }
        catch (Exception)
        {

            lblpoll.Text = "متاسفیم در حال حاضر نمیتوان انجام دهید بغدا انجام دهید";
        }


    }

    private void readXML()
    {
        string conn = ConfigurationManager.ConnectionStrings["EnglishDBConnectionString"].ToString();
        DataClasses1DataContext db = new DataClasses1DataContext();
        Poll po = new Poll();
        var votes = from vote in db.Polls select vote;
        int acount;
        int bcount;
        int ccount;
        int dcount;
        acount = 0;
        bcount = 0;
        ccount = 0;
        dcount = 0;
        foreach (var vote in votes)
        {
            if (vote.Vote == "perfect")
                acount++;
            if (vote.Vote == "good")
                bcount++;
            if (vote.Vote == "bad")
                ccount++;
            if (vote.Vote == "terrible")
                dcount++;
        }
        double thetotal;
        thetotal = acount + bcount + ccount + dcount;
        double apercent;
        double bpercent;
        double cpercent;
        double dpercent;
        apercent = (acount / thetotal) * 100;
        bpercent = (bcount / thetotal) * 100;
        cpercent = (ccount / thetotal) * 100;
        dpercent = (dcount / thetotal) * 100;
        lblresult.Visible = true;
        lblresult.Text = "perfect:" + acount + "رای(" + apercent + "%).<br />";
        lblresult.Text = "good:" + acount + "رای(" + bpercent + "%).<br />";
        lblresult.Text = "bad:" + acount + "رای(" + cpercent + "%).<br />";
        lblresult.Text = "terrible:" + acount + "رای(" + dpercent + "%).<br />";
    }

        protected void Showbtn_Click(object sender, EventArgs e)
    {
        readXML();
    }

1 个答案:

答案 0 :(得分:0)

每次选择一个项目时,都会进入countvote() 然后结果将永远是最后一个:

lblresult.Text = "perfect:" + acount + "رای(" + apercent + "%).<br />";
    lblresult.Text = "good:" + acount + "رای(" + bpercent + "%).<br />";
    lblresult.Text = "bad:" + acount + "رای(" + cpercent + "%).<br />";
    lblresult.Text = "terrible:" + acount + "رای(" + dpercent + "%).<br />";
你正在覆盖最后一个可怕的结果。您需要连接字符串以显示所有字符串。使用stringbuilder存储结果,然后执行lblresult.Text = stringBuilder.Tostring()