2013-08-25 62 views
2

我想讓我的提交按鈕插入數據輸入到幾個文本框和一個隱藏的字段。現在,它給了我一個錯誤,說當前上下文中不存在名稱「txtComments」,「txtName」和「datePosted」。我相對肯定的是我必須創建變量,但我如何確保它們與相應的ASP控件中的內容相同?這是我的代碼:提交方法無法識別AddWithValue

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> 
+0

內部控制着這一'位於InsertItemTemplate'控制? – Damith

+0

更新面板,它包含在內容佔位符中,因爲我使用母版頁。 – Joseph

+0

我的意思是gridview或listview? – Damith

回答

1

代碼:

protected void submitButton_Click(object sender, EventArgs e) 
{ 

    //string constr = @"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=~\App_Data\TravelJoansDB.accdb"; 
    string constr = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\Database21.accdb;"; 
    string cmdstr = "INSERT INTO Comments(commentText,datePosted,personName) VALUES (@txtComments, @datePosted, @personName)"; 
    OleDbConnection con = new OleDbConnection(constr); 
    OleDbCommand com = new OleDbCommand(cmdstr, con); 
    TextBox tComments = (TextBox)FormView1.FindControl("txtComments"); 
    HiddenField tDate = (HiddenField)FormView1.FindControl("hidTimeDate"); 
    TextBox tName = (TextBox)FormView1.FindControl("txtName"); 
    con.Open(); 
    com.Parameters.AddWithValue("@txtComments", tComments.Text); 
    com.Parameters.AddWithValue("@datePosted", DateTime.Now.ToString()); 
    //com.Parameters.AddWithValue("@datePosted", tDate.Value.ToString()); 
    com.Parameters.AddWithValue("@personName", tName.Text); 
    com.ExecuteNonQuery(); 
    con.Close(); 
} 
protected void FormView1_ItemInserting(object sender, FormViewInsertEventArgs e) 
{ 
} 

注:更改您的構造!

ASPX:

<asp:FormView ID="FormView1" runat="server" DefaultMode="Insert" 
    oniteminserting="FormView1_ItemInserting"> 
    <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> 
</asp:FormView> 
+0

使用「DateTime.Now」調用,我需要ASP代碼中的隱藏字段嗎?否則,它仍然告訴txtComments在當前上下文中不存在。 – Joseph

+0

另外,這裏是我正在使用的命名空間的列表。我確定其中有些是無關的: using System; using System.Collections.Generic;使用System.Linq的 ; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data.OleDb; – Joseph

+0

我的回答編輯 –