繁体   English   中英

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

[英]how to create asp.net voting system?

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

         <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();
    }

每次选择一个项目,它都会进入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()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM