c# - how to create asp.net voting system? -
i have been creating asp.net polling scheme .every thing ok there problem not why happen . illustration every time refresh page on item add together item list in buttonlist command . , every time take item show me 1 item (for illustration everi time show me terrible item). here code :
<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"/> <asp:label id="lblpoll" runat="server" text="" forecolor="red" font-italic="true" font-size="larger"></asp:label> </contenttemplate> </asp:updatepanel>
c# code :
protected void savebtn_click(object sender, eventargs e) { if (radvote.selecteditem == null) lblpoll.text = "لطفا در نظرسنجی شرکت کنید"; else countvote(radvote.selecteditem.tostring()); } protected void countvote(string thevote) { seek { 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(); } grab (exception) { lblpoll.text = "متاسفیم در حال حاضر نمیتوان انجام دهید بغدا انجام دهید"; } } private void readxml() { string conn = configurationmanager.connectionstrings["englishdbconnectionstring"].tostring(); dataclasses1datacontext db = new dataclasses1datacontext(); poll po = new poll(); var votes = 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(); }
every time select item, goes countvote() result lastly one:
lblresult.text = "perfect:" + acount + "رای(" + apercent + "%).<br />"; lblresult.text = "good:" + acount + "رای(" + bpercent + "%).<br />"; lblresult.text = "bad:" + acount + "رای(" + cpercent + "%).<br />"; lblresult.text = "terrible:" + acount + "رای(" + dpercent + "%).<br />";
you overwriting result lastly terrible one. need concatenate strings show all. utilize stringbuilder store result , lblresult.text= stringbuilder.tostring()
c# asp.net
No comments:
Post a Comment