2010-05-21 43 views
4

我正在開發一個C#VS 2008/SQL Server 2008網站。我有一個Gridview。我在下面包含了Default.aspx和aspx.cs文件。但是當我建立這個時,我得到下面的錯誤:類型'_Default'已經包含一個定義

The Type '_Default' already contains a definition for 'btnOWrite'

我需要做什麼來解決這個問題?我現在沒有得到任何錯誤;只是這個網格不顯示。謝謝!

ASPX文件:

<%@ Page Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" Title="Untitled Page" %> 

<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="Server"> 
    <asp:Panel runat="server" ID="AuthenticatedMessagePanel"> 
     <asp:Label runat="server" ID="WelcomeBackMessage"></asp:Label> 
     <table> 
      <tr> 
       <td> 
        <asp:Label ID="tableLabel" runat="server" Font-Bold="True" Text="Select target table:"></asp:Label> 
       </td> 
       <td> 
        <asp:Label ID="inputLabel" runat="server" Font-Bold="True" Text="Select input file:"></asp:Label> 
       </td> 
      </tr> 
      <tr> 
       <td valign="top"> 
        <asp:Label ID="feedbackLabel" runat="server"></asp:Label> 
        <asp:SqlDataSource ID="SelectTables" runat="server" ConnectionString="<%$ ConnectionStrings:AdventureWorks3_SelectTables %>" 
         SelectCommand="getTableNames" SelectCommandType="StoredProcedure"> 
         <SelectParameters> 
          <asp:QueryStringParameter DefaultValue="Person" Name="SchemaName" QueryStringField="SchemaName" 
           Type="String" /> 
         </SelectParameters> 
        </asp:SqlDataSource> 
        <asp:GridView ID="GridView1" DataSourceID="SelectTables" runat="server" Style="width: 400px;" 
         CellPadding="4" ForeColor="#333333" GridLines="None" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" 
         AutoGenerateSelectButton="True" DataKeyNames="TABLE_NAME"> 
         <RowStyle BackColor="#F7F6F3" ForeColor="#333333" /> 
         <Columns> 
          <asp:BoundField HeaderText="TABLE_NAME" DataField="TABLE_NAME" /> 
         </Columns> 
         <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" /> 
         <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" /> 
         <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" /> 
         <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" /> 
         <EditRowStyle BackColor="#999999" /> 
         <AlternatingRowStyle BackColor="White" ForeColor="#284775" /> 
        </asp:GridView> 
       </td> 
       <td valign="top"> 
        <input id="uploadFile" type="file" size="26" runat="server" name="uploadFile" title="UploadFile" class="greybar" enableviewstate="True" /> 
       </td> 
      </tr> 
     </table> 

     <table> 
<tr> 
<td style="width:150px; height:50px"></td> 
<td valign="bottom" style="width:150px; height:50px"> 
    <input id="btnOWrite" type="submit" value="Overwrite Data" runat="server" class="greybar" 
         onserverclick="btnOWrite_Click" name="btnOWrite" />&nbsp; 
         </td> 
<td style="width:100px"></td> 

<td valign="bottom" style="width:150px; height:50px"> 
    <input id="btnAppend" type="submit" value="Append Data" runat="server" class="greybar" onserverclick="btnAppend_Click" name="btnAppend" /> 
</td> 
</tr> 
</table> 

    </asp:Panel> 
    <asp:Panel runat="Server" ID="AnonymousMessagePanel"> 
     <asp:HyperLink runat="server" ID="lnkLogin" Text="Log In" NavigateUrl="~/Login.aspx"> 
     </asp:HyperLink> 
    </asp:Panel> 
</asp:Content> 

ASPX.CS文件:

public partial class _Default : System.Web.UI.Page 

//namespace AddFileToSQL 
{ 
     //protected System.Web.UI.HtmlControls.HtmlInputFile uploadFile; 
     protected System.Web.UI.HtmlControls.HtmlInputButton btnOWrite; 
     protected System.Web.UI.HtmlControls.HtmlInputButton btnAppend; 
     protected System.Web.UI.WebControls.Label Label1; 
     protected static string inputfile = ""; 
     public static string targettable; 
     public static string selection; 

     // Number of controls added to view state 
     protected int default_NumberOfControls 
     { 
      get 
      { 
       if (ViewState["default_NumberOfControls"] != null) 
       { 
        return (int)ViewState["default_NumberOfControls"]; 
       } 
       else 
       { 
        return 0; 
       } 
      } 
      set 
      { 
       ViewState["default_NumberOfControls"] = value; 
      } 
     } 

     protected void uploadFile_onclick(object sender, EventArgs e) 
     { 
     } 

     protected void Load_GridData() 
     { 
      //GridView1.DataSource = ADONET_methods.DisplaySchemaTables(); 
      //GridView1.DataBind(); 
     } 

     protected void btnOWrite_Click(object sender, EventArgs e) 
     { 
      if (uploadFile.PostedFile.ContentLength > 0) 
      { 
       feedbackLabel.Text = "You do not have sufficient access to overwrite table records."; 
      } 
      else 
      { 
       feedbackLabel.Text = "This file does not contain any data."; 
      } 
     } 

     protected void btnAppend_Click(object sender, EventArgs e) 
     { 
      string fullpath = Page.Request.PhysicalApplicationPath; 

      string path = uploadFile.PostedFile.FileName; 

      if (File.Exists(path)) 
      { 
       // Create a file to write to. 
       try 
       { 
        StreamReader sr = new StreamReader(path); 
        string s = ""; 
        while (sr.Peek() > 0) 
         s = sr.ReadLine(); 
        sr.Close(); 
       } 
       catch (IOException exc) 
       { 
        Console.WriteLine(exc.Message + "Cannot open file."); 
        return; 
       } 
      } 

      if (uploadFile.PostedFile.ContentLength > 0) 
      { 

       inputfile = System.IO.File.ReadAllText(path); 
       Session["Message"] = inputfile; 
       Response.Redirect("DataMatch.aspx"); 
      } 
      else 
      { 
       feedbackLabel.Text = "This file does not contain any data."; 
      } 
     } 

     protected void Page_Load(object sender, EventArgs e) 
     { 
      if (Request.IsAuthenticated) 
      { 
       WelcomeBackMessage.Text = "Welcome back, " + User.Identity.Name + "!"; 

       // Reference the CustomPrincipal/CustomIdentity 
       CustomIdentity ident = User.Identity as CustomIdentity; 
       if (ident != null) 
        WelcomeBackMessage.Text += string.Format(" You are the {0} of {1}.", ident.Title, ident.CompanyName); 

       AuthenticatedMessagePanel.Visible = true; 
       AnonymousMessagePanel.Visible = false; 
       if (!Page.IsPostBack) 
       { 
        Load_GridData(); 
       }         
      } 
      else 
      { 
       AuthenticatedMessagePanel.Visible = false; 
       AnonymousMessagePanel.Visible = true; 
      } 
     } 

     protected void GridView1_SelectedIndexChanged(object sender, EventArgs e) 
     { 
      GridViewRow row = GridView1.SelectedRow; 
      targettable = row.Cells[2].Text; 
     } 
    } 

回答

6

當你有一個控制做出declaritively如

<asp:Button ID="someID" runat="server" /> 

它實例化一個名爲someID一個私有成員變量。如果你看一下你的類,你會看到這條線

protected System.Web.UI.HtmlControls.HtmlInputButton btnOWrite; 

基本上,你已經宣佈2個控件使用相同的名稱。一個在標記中,一個在代碼後面。只需從代碼背後的代碼中刪除聲明。

2

也許,在文件Default.Designer.cs類包含的btnOWrite兩個聲明。

嘗試刪除至少一個或兩個變量。

或者如果您的項目是一個web應用程序,刪除整個設計器文件,並在Default.aspx的上下文菜單中選擇'轉換爲web應用程序',這將重新生成設計器文件。

+0

良好的呼叫。我現在解決了這個問題。問題出在我的ASPX.CS文件中: // protected System.Web.UI.HtmlControls.HtmlInputButton btnOWrite; //受保護的System.Web.UI.HtmlControls.HtmlInputButton btnAppend; – salvationishere 2010-05-22 00:08:34

0

如果在向現有Web項目添加新文件時添加了文件(.aspx),並且它們正確移動並且在新位置添加對該文件的引用而不刪除舊引用,則會發生此錯誤。在整個項目(搜索文件)中搜索重複的符號,這將顯示重複參考文件存在於哪些文件中。

相關問題