繁体   English   中英

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

[英]Insert using a submit button in ASP.NET doesn't work?

您好,我在下面有一些ASP.Net代码。 我试图弄清楚如何使我的提交按钮:

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

好好工作。 当我尝试添加AccessDataSource1.Insert()时,出现错误消息:

CS1061:“ ASP.main_aspx”不包含“ AccessDataSource1”的定义,并且找不到找到接受类型为“ ASP.main_aspx”的第一个参数的扩展方法“ AccessDataSource1”(您是否缺少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>

您的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 或者,您是否以不同的方式命名而没有意识到呢?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM