我试图让我的提交按钮将输入的数据插入几个文本框和一个隐藏字段。现在,它给了我一个错误,说名称“txtComments”、“txtName”和“datePosted”在当前上下文中不存在。我相对确定我必须创建变量,但我如何确保它们等于相应 ASP 控件中的内容?这是我的代码:
protected void submitButton_Click(object sender, EventArgs e)
{
string constr = @"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=~\App_Data\TravelJoansDB.accdb";
string cmdstr = "INSERT INTO Comments VALUES (@txtComments, @datePosted, @personName)";
OleDbConnection con = new OleDbConnection(constr);
OleDbCommand com = new OleDbCommand(cmdstr, con);
con.Open();
com.Parameters.AddWithValue("@txtComments", txtComments.TextBox);
com.Parameters.AddWithValue("@datePosted", datePosted.DateTime);
com.Parameters.AddWithValue("@personName", txtName.TextBox);
com.ExecuteNonQuery();
con.Close();
}
那么如何确保将变量设置为 asp 控件呢?在这里:
<InsertItemTemplate>
Name: <asp:TextBox ID="txtName" runat="server" Text='<%# Bind("personName") %>'></asp:TextBox><br />
Comments:<br />
<asp:TextBox ID="txtComments" runat="server" Text='<%# Bind("commentText") %>'
TextMode="MultiLine" Rows="4" Columns="50"></asp:TextBox><br />
<asp:HiddenField ID="hidTimeDate" runat="server" Value='<%# Bind("datePosted") %>' />
<asp:Button ID="InsertButton" runat="server" CausesValidation="True"
CommandName="Insert" Text="Submit" OnClick="submitButton_Click" />
</InsertItemTemplate>