2012-12-29 19 views
0

我想做一個簡單的投票頁面,我應該工作的代碼,但它似乎是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(); 
} 

}

gvResult,radVote,lblStatus都有一個錯誤,表示該名稱在當前上下文中不存在。我檢查了名字,一切似乎都很好,但我仍然得到這個錯誤。

在HTML我有單選按鈕列表,一個gridview,投票一個按鈕和顯示結果

+0

請顯示default.aspx頁面。 – Blachshma

+0

@Blachshma我試圖添加它,但它沒有出來,因爲它應該 – Ang

+0

順便說一下,你使用的是什麼版本的ASP.NET? – IrishChieftain

回答

0

你的頁面文件中的一個按鈕可能沒有達到之間不同步設計文件。創建一個新頁面,在那裏移動你的網格,並看看是否修復它?

您可以自己手動在設計器文件中添加控件聲明,但不建議這樣做。

+0

@IrishChieftan試過,但它沒有區別 – Ang

+0

我們需要從ASPX頁面看到頁面指令和GridView外部標籤的片段。 – IrishChieftain

+0

設計器文件中是否有GridView聲明? – IrishChieftain

0

或者,您可以刪除設計器文件並讓ASP.Net重新生成它。有一個更長的解釋here但基本上,你只需刪除問題.designer.cs文件。要重新生成它,請單擊解決方案資源管理器中的.aspx文件,然後單擊「轉換爲Web應用程序」。

+0

不錯....... :-) – IrishChieftain