在 ASP.NET 中使用提交按钮插入不起作用

本文关键字:提交 按钮 插入 不起作用 ASP NET | 更新日期: 2023-09-27 18:18:46

你好,我在下面有一些 ASP.Net 代码。我正在尝试弄清楚如何制作我的提交按钮:

 <asp:Button ID="makepost" runat="server" Text="Post My Tip/Story" 
                ValidationGroup="createpost" />

正常工作。当我尝试添加 AccessDataSource1.Insert(( 时,我收到一个错误,说:

CS1061:"ASP.main_aspx"不包含 "访问数据源 1"和无扩展方法"访问数据源 1" 可以找到接受类型为"ASP.main_aspx"的第一个参数(是 您缺少 using 指令或程序集引用?

我需要输入什么才能完成这项工作?我的所有代码都在下面。

<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Main.aspx.cs" Inherits="Main" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <asp:LoginView ID="LoginView1" runat="server">
    <LoggedInTemplate>
        <asp:LoginStatus ID="LoginStatus1" runat="server" />
    <div style="font-family: Arial, Helvetica, sans-serif; font-size: 12px; color: #FFFFFF">
    <p style="font-size: 20px">Welcome Back!<br />
    Begin Sharing and Reading Space Tips Below!
    </p>

    <p>&nbsp;Current Posts:
        <asp:GridView ID="GridView1" runat="server" DataSourceID="AccessDataSource1" 
            AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" 
            BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" 
            CellPadding="3" CellSpacing="2" DataKeyNames="ID">
            <RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
            <Columns>
                <asp:BoundField DataField="User" HeaderText="User" SortExpression="User" />
                <asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />
                <asp:BoundField DataField="Timeof" HeaderText="Timeof" 
                    SortExpression="Timeof" />
                <asp:BoundField DataField="Post" HeaderText="Post" SortExpression="Post" />
                <asp:CommandField ButtonType="Button" ShowDeleteButton="True" />
                <asp:CommandField ButtonType="Button" ShowEditButton="True" />
            </Columns>
            <FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
            <PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
            <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />
        </asp:GridView>
        <asp:AccessDataSource ID="AccessDataSource1" runat="server" 
            DataFile="~/App_Data/Posts.mdb" 
            SelectCommand="SELECT * FROM [UserPost] ORDER BY [Timeof]" 
            DeleteCommand="DELETE FROM [UserPost] WHERE [ID] = ?" 
            InsertCommand="INSERT INTO [UserPost] ([ID], [User], [Title], [Timeof], [Post]) VALUES (?, ?, ?, ?, ?)" 
            UpdateCommand="UPDATE [UserPost] SET [User] = ?, [Title] = ?, [Timeof] = ?, [Post] = ? WHERE [ID] = ?">
            <DeleteParameters>
                <asp:Parameter Name="ID" Type="Int32" />
            </DeleteParameters>
            <UpdateParameters>
                <asp:Parameter Name="User" Type="String" />
                <asp:Parameter Name="Title" Type="String" />
                <asp:Parameter Name="Timeof" Type="DateTime" />
                <asp:Parameter Name="Post" Type="String" />
                <asp:Parameter Name="ID" Type="Int32" />
            </UpdateParameters>
            <InsertParameters>
                <asp:ControlParameter Name="ID" Type="Int32" ControlID="TextBox5" PropertyName="Text" />
                <asp:ControlParameter Name="User" Type="String" ControlID="TextBox1" PropertyName="Text" />
                <asp:ControlParameter Name="Title" Type="String" ControlID="TextBox2" PropertyName="Text" />
                <asp:ControlParameter Name="Timeof" Type="DateTime" ControlID="TextBox3" PropertyName="Text" ConvertEmptyStringToNull="true"/>
                <asp:ControlParameter Name="Post" Type="String" ControlID="TextBox4" PropertyName="Text"  />
            </InsertParameters>
        </asp:AccessDataSource>
    </p>
        <table><tr><td> <asp:Label ID="Label5" runat="server" Text="ID: "></asp:Label></td><td> 
            <asp:TextBox ID="TextBox5" runat="server" ValidationGroup="createpost"></asp:TextBox></td></tr>
        <tr><td> <asp:Label ID="Label1" runat="server" Text="User: "></asp:Label></td><td> 
            <asp:TextBox ID="TextBox1" runat="server" ValidationGroup="createpost"></asp:TextBox></td></tr>
        <tr><td><asp:Label ID="Label2" runat="server" Text="Title: "></asp:Label></td><td><asp:TextBox ID="TextBox2"
                runat="server" ValidationGroup="createpost"></asp:TextBox></td></tr>
        <tr><td><asp:Label ID="Label3" runat="server" Text="Date: "></asp:Label></td><td> <asp:TextBox ID="TextBox3"
                runat="server"></asp:TextBox></td></tr>
        <tr><td valign="top"><asp:Label ID="Label4" runat="server" Text="Story: "></asp:Label></td><td valign="top">
            <asp:TextBox ID="TextBox4" runat="server" Height="129px" Width="474px" TextMode="MultiLine"></asp:TextBox></td></tr>
        </table>
            <asp:Button ID="makepost" runat="server" Text="Post My Tip/Story" 
                ValidationGroup="createpost" /><br />
    </div>
    </LoggedInTemplate><AnonymousTemplate>
        <asp:Login ID="Login1" runat="server" ForeColor="White">
        </asp:Login><br /><div style="font-family: Arial, Helvetica, sans-serif; font-size: 12px; color:#FFFFFF">Please Register to be begin viewing and sharing your 
        <br />Star Wars Old Republic Tips and Stories!</div>
    </AnonymousTemplate>
    </asp:LoginView>
</asp:Content>

在 ASP.NET 中使用提交按钮插入不起作用

您的 AccessDataSource 位于 LoginView 中,您必须找到控件:

Dim AccessDataSource1 As AccessDataSource = DirectCast(LoginView1.FindControl("AccessDataSource1"), AccessDataSource)

问候。 在 C# 中,这将是这个

AccessDataSource AccessDataSource1 = (AccessDataSource)LoginView1.FindControl("AccessDataSource1");
'ASP.main_aspx' does not contain a definition for 'AccessDataSource1'

我认为您的 AccessDataSource 控件位于错误的位置:您将其放在 LoginView 中。错误消息指出运行时在页面的根目录中找不到该控件。因此,尝试将 AccessDataSource 放在 LoginView 之外。

此行。看看:

<asp:AccessDataSource ID="AccessDataSource1" runat="server" 

你看到AccessDataSource1了吗?您是否可能指向不正确的Datasource?或者,您是否以不同的方式命名并且没有意识到?