2017-07-13 112 views
0

我遇到問題。 我重命名此Web窗體後,我得到此錯誤,但我將所有更改爲新名稱 但我得到此錯誤。 Plz,幫助。 代碼:擴展方法必須在非泛型靜態類中定義

using System; 
using System.Collections.Generic; 
using System.Data; 
using System.Globalization; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 

public partial class bVoteAnswer : System.Web.UI.Page 
{ 
    UserVotes ue = new UserVotes(); 
    bVotes bv = new bVotes(); 
    bVoteAnswers ve = new bVoteAnswers(); 
    public void Page_Load(object sender, EventArgs e) 
    { 

     dropdownlist.Enabled = false; 
     int BuildingId = Convert.ToInt32(Session["BuildingId"]); 
     DataTable dt = new DataTable(); 
     dt = bv.Select(0, "", "", "", BuildingId, "",0); 
     Grid_Vote.DataSource = dt; 
     Grid_Vote.DataBind(); 

    } 
protected void Grid_Vote_RowCreated(object sender, GridViewRowEventArgs e) 
{ 
    e.Row.Cells[0].Visible = false; 
} 

protected void Grid_Vote_SelectedIndexChanged(object sender, EventArgs e) 
{ 
    dropdownlist.Enabled = true; 
    int VoteId = Convert.ToInt32(Grid_Vote.SelectedRow.Cells[0].Text); 
    DataTable dt = new DataTable(); 
    dt = ve.Select(0,VoteId,""); 
    for (int i = 0; i <= dt.Rows.Count; i++) 
    { 
     ListItem l = new ListItem(dt.Rows[i][2].ToString(), dt.Rows[i][2].ToString()); 
     dropdownlist.Items.Add(l); 
    } 
} 

protected void btn_insert_Click(object sender, EventArgs e) 
{ 
    int OwnerId = Convert.ToInt32(Session["OwnerId"]); 
    int VoteId = Convert.ToInt32(Grid_Vote.SelectedRow.Cells[0].Text); 
    DataTable dt = new DataTable(); 
    dt = ve.Select(0, VoteId, dropdownlist.SelectedValue); 
    int AnswerId = Convert.ToInt32(dt.Rows[0][0]); 
    DateTime ClientDateTime = DateTime.Now; 
    string PersianDate = GetPersianDate(ClientDateTime); 
    ue.Insert(0, VoteId, OwnerId, AnswerId, PersianDate); 
} 


public static string GetPersianDate(this DateTime date) 
{ 
    PersianCalendar jc = new PersianCalendar(); 
    return string.Format("{0:0000}/{1:00}/{2:00}", jc.GetYear(date), jc.GetMonth(date), jc.GetDayOfMonth(date)); 
} 

}

我將名稱更改爲第一個名字,但錯誤顯示 我想是不是一個錯誤和Visual Studio不要運行神

+1

在想,如果這是你自己,或者是錯誤的編譯器,這是非常經常自己。 –

回答

1

擴展方法需要處於靜態類。

public partial class bVoteAnswer 

這不是靜態類。 將函數移動到靜態類。

public static class ExtensionMethods 
{ 
    public static string GetPersianDate(this DateTime date) 
    { 
     PersianCalendar jc = new PersianCalendar(); 
     return string.Format("{0:0000}/{1:00}/{2:00}", jc.GetYear(date), 
       jc.GetMonth(date), jc.GetDayOfMonth(date)); 
     } 

} 
1

需要在靜態類中定義擴展方法GetPersianDate。您可以重構是這樣的:

public partial class bVoteAnswer : System.Web.UI.Page 
{ 
    UserVotes ue = new UserVotes(); 
    bVotes bv = new bVotes(); 
    bVoteAnswers ve = new bVoteAnswers(); 
    public void Page_Load(object sender, EventArgs e) 
    { 

     dropdownlist.Enabled = false; 
     int BuildingId = Convert.ToInt32(Session["BuildingId"]); 
     DataTable dt = new DataTable(); 
     dt = bv.Select(0, "", "", "", BuildingId, "",0); 
     Grid_Vote.DataSource = dt; 
     Grid_Vote.DataBind(); 

    } 

    protected void Grid_Vote_RowCreated(object sender, GridViewRowEventArgs e) 
    { 
     e.Row.Cells[0].Visible = false; 
    } 

    protected void Grid_Vote_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     dropdownlist.Enabled = true; 
     int VoteId = Convert.ToInt32(Grid_Vote.SelectedRow.Cells[0].Text); 
     DataTable dt = new DataTable(); 
     dt = ve.Select(0,VoteId,""); 
     for (int i = 0; i <= dt.Rows.Count; i++) 
     { 
      ListItem l = new ListItem(dt.Rows[i][2].ToString(), dt.Rows[i][2].ToString()); 
      dropdownlist.Items.Add(l); 
     } 
    } 

    protected void btn_insert_Click(object sender, EventArgs e) 
    { 
     int OwnerId = Convert.ToInt32(Session["OwnerId"]); 
     int VoteId = Convert.ToInt32(Grid_Vote.SelectedRow.Cells[0].Text); 
     DataTable dt = new DataTable(); 
     dt = ve.Select(0, VoteId, dropdownlist.SelectedValue); 
     int AnswerId = Convert.ToInt32(dt.Rows[0][0]); 
     DateTime ClientDateTime = DateTime.Now; 
     string PersianDate = GetPersianDate(ClientDateTime); 
     ue.Insert(0, VoteId, OwnerId, AnswerId, PersianDate); 
    } 
} 

public static class PersionCalendarExtension 
{ 
    public static string GetPersianDate(this DateTime date) 
    { 
     PersianCalendar jc = new PersianCalendar(); 
     return string.Format("{0:0000}/{1:00}/{2:00}", jc.GetYear(date), jc.GetMonth(date), jc.GetDayOfMonth(date)); 
    } 
} 
相關問題