C# 制作简单的投票页面

C# 制作简单的投票页面,c#,asp.net,html,gridview,C#,Asp.net,Html,Gridview,我想做一个简单的投票页面,我有一个代码,应该可以工作,但似乎.cs文件不想识别gridview public partial class _Default : System.Web.UI.Page { int Count = 0; protected void Page_Load(object sender, EventArgs e) { } protected void btnVote_Click(object sender, EventArgs

我想做一个简单的投票页面,我有一个代码,应该可以工作,但似乎.cs文件不想识别gridview

public partial class _Default : System.Web.UI.Page 
{
    int Count = 0;
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void btnVote_Click(object sender, EventArgs e)
    {
        if (radVote.SelectedItem != null)
        {
            InsertVotes(radVote.SelectedItem.ToString());
        }
        else
        {
            lblStatus.ForeColor = Color.Red;
            lblStatus.Text = "Please select at least one option to vote for poll";
        }
    }

    protected void InsertVotes(string theVote)
    {
        try
        {
            XmlDocument xmldoc = new XmlDocument();
            xmldoc.Load(Server.MapPath("Votes.xml"));
            XmlElement parentelement = xmldoc.CreateElement("Vote");
            XmlElement votechoice = xmldoc.CreateElement("Choice");
            votechoice.InnerText = theVote;
            parentelement.AppendChild(votechoice);
            xmldoc.DocumentElement.AppendChild(parentelement);
            xmldoc.Save(Server.MapPath("Votes.xml"));
            lblStatus.ForeColor = Color.Green;
            lblStatus.Text = "Thank you for your vote.";
        }
        catch
        {
            lblStatus.Text = "Sorry, unable to process request. Please try again.";
        }
    }

protected void readXML()
{
    int mCount = 0;
    int iCount = 0;
    int gCount = 0;
    XmlTextReader xmlreader = new XmlTextReader(Server.MapPath("Votes.xml"));
    DataSet ds = new DataSet();
    ds.ReadXml(xmlreader);
    xmlreader.Close();
    if (ds.Tables.Count > 0)
    {
        int dscount = ds.Tables[0].Rows.Count;
        for (int i = 0; i < dscount; i++)
        {
            if (ds.Tables[0].Rows[i][0].ToString() == "Mozilla")
                mCount++;
            else if (ds.Tables[0].Rows[i][0].ToString() == "Internet Explorer")
                iCount++;
            else if (ds.Tables[0].Rows[i][0].ToString() == "Google Chrome")
                gCount++;
        }

        double theTotal;
        theTotal = mCount + iCount + gCount;

        double mPercent;
        double oPercent;
        double gPercent;
        mPercent = (mCount / theTotal) * 100;
        oPercent = (iCount / theTotal) * 100;
        gPercent = (gCount / theTotal) * 100;

        double totalpercentage = mPercent + oPercent + gPercent;

        string[] votescount = { mCount.ToString(), iCount.ToString(), gCount.ToString() };
        string[] array = { mPercent.ToString(), oPercent.ToString(), gPercent.ToString() };
        DataTable dt = new DataTable();

        dt.Columns.Add("OPTION_NAME");
        dt.Columns.Add("VOTES");
        dt.Columns.Add("PERCENTAGE");

        int count = radVote.Items.Count;
        Count = count + 1;

        for (int i = 0; i < count; i++)
        {
            dt.Rows.Add();
            dt.Rows[i]["OPTION_NAME"] = radVote.Items[i].ToString();
            dt.Rows[i]["VOTES"] = votescount[i];
            dt.Rows[i]["PERCENTAGE"] = array[i];
        }
            dt.Rows.Add("Total", theTotal, totalpercentage);
            gvResult.DataSource = dt;
            gvResult.DataBind();
   }
   else
   {
        gvResult.DataSource = null;
        gvResult.DataBind();
   }
}

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

int cnt = 0;
protected void gvResult_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)

    cnt++;

    Label lblpercent = (Label)e.Row.FindControl("lblpercentage");
    HtmlTable tblpercent = (HtmlTable)e.Row.FindControl("tblBar");
    tblpercent.Width = lblpercent.Text+"%";

    if (lblpercent.Text == "0")
    {
        tblpercent.Visible = false;
    }

    if (cnt == Count)
    {
        e.Row.CssClass = "TablePollResultFoot";
    }

    foreach (TableCell tc in e.Row.Cells)
    {
        tc.Attributes["style"] = "border-color:#CCCCCC";
    }
}

protected void btnResult_Click(object sender, EventArgs e)
{
    readXML();
}
public分部类\u默认值:System.Web.UI.Page
{
整数计数=0;
受保护的无效页面加载(对象发送方、事件参数e)
{
}
受保护的无效btnVote\u单击(对象发送者,事件参数e)
{
如果(radVote.SelectedItem!=null)
{
插入投票(radVote.SelectedItem.ToString());
}
其他的
{
lblStatus.ForeColor=颜色。红色;
lblStatus.Text=“请至少选择一个选项进行投票”;
}
}
受保护的void insertvote(字符串theVote)
{
尝试
{
XmlDocument xmldoc=新的XmlDocument();
Load(Server.MapPath(“voates.xml”);
XmlElement parentelement=xmldoc.CreateElement(“投票”);
XmlElement votechoice=xmldoc.CreateElement(“选择”);
votechoice.InnerText=投票;
parentelement.AppendChild(votechoice);
xmldoc.DocumentElement.AppendChild(parentelement);
Save(Server.MapPath(“voates.xml”);
lblStatus.ForeColor=颜色.绿色;
lblStatus.Text=“感谢您的投票。”;
}
抓住
{
lblStatus.Text=“抱歉,无法处理请求。请重试。”;
}
}
受保护的void readXML()
{
int mCount=0;
int-iCount=0;
int gCount=0;
XmlTextReader=newxmltextreader(Server.MapPath(“vows.xml”);
数据集ds=新数据集();
ReadXml(xmlreader);
xmlreader.Close();
如果(ds.Tables.Count>0)
{
int dscount=ds.Tables[0].Rows.Count;
对于(int i=0;i
}

gvResult、radVote和lblStatus都有一个错误,表示该名称在当前上下文中不存在。我已经检查了名字,一切似乎都很好,但我仍然得到这个错误


在html我有radiobuttonlist、gridview、一个投票按钮和一个显示结果按钮中,您的页面文件可能与设计器文件不同步。创建一个新页面,将你的网格移动到那里,看看是否可以修复它


您可以自己在设计器文件中手动添加控制声明,但不建议这样做

或者,您可以删除设计器文件并让ASP.Net重新生成它。有一个较长的解释,但基本上,您只需删除problem.designer.cs文件。要重新生成它,请单击解决方案资源管理器中的.aspx文件,然后单击“转换为Web应用程序”

请显示default.aspx页面。@Blachshma我曾尝试添加它,但没有显示出来。顺便问一下,您使用的是什么版本的ASP.NET?我们需要从您的aspx页面查看页面指令和GridView外部标记的片段。GridView声明是否在设计器文件中?