0

这是有效的代码:

protected void submitForMail(object sender, EventArgs e)
{
    string constr = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\TravelJoansDB.mdb;";
    string cmdstr = "INSERT INTO EmailList(FirstName,LastName,EmailAddress) VALUES (@FirstName, @LastName, @EmailAddress)";

    OleDbConnection con = new OleDbConnection(constr);
    OleDbCommand com = new OleDbCommand(cmdstr, con);

    TextBox tFirstName = (TextBox)FormView1.FindControl("FirstName");
    TextBox tLastName = (TextBox)FormView1.FindControl("LastName");
    TextBox tEmail = (TextBox)FormView1.FindControl("EmailAddress");

    con.Open();
    com.Parameters.AddWithValue("@FirstName", tFirstName.Text);
    com.Parameters.AddWithValue("@LastName", tLastName.Text);
    com.Parameters.AddWithValue("@EmailAddress", tEmail.Text);
    com.ExecuteNonQuery();
    con.Close();
}

这是没有的代码:

protected void UpdatePic(object sender, EventArgs e)
{
    string constr = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\TravelJoansDB.mdb;";
    string cmdstr = "INSERT INTO BlogEntryItems(Picture) VALUES (@UpdatedPic)";

    OleDbConnection con = new OleDbConnection(constr);
    OleDbCommand com = new OleDbCommand(cmdstr, con);

    TextBox uPic = (TextBox)DataList1.FindControl("BEIPictureField");

    con.Open();
    com.Parameters.AddWithValue("@UpdatedPic", uPic.Text);
    com.ExecuteNonQuery();
    con.Close();
}

下面是包含 Datalist1 控件的代码:

<asp:AccessDataSource ID="AccessDataSource1" runat="server" 
    DataFile="~/App_Data/TravelJoansDB.mdb" 
    SelectCommand="SELECT * FROM Table2 INNER JOIN [BlogEntryItems] ON Table2.ID=BlogEntryItems.BlogID WHERE ID=@ID" >
    <SelectParameters>
        <asp:QueryStringParameter Name="ID" QueryStringField="ID" />
    </SelectParameters>
</asp:AccessDataSource>
<asp:DataList ID="DataList1" runat="server" DataSourceID="AccessDataSource1">
<ItemStyle />
<ItemTemplate>
    <table>
        <tr>
            <td>
                <br />
                <asp:Image ID="Image1" CssClass="placePicCenter" runat="server" 
                BorderWidth="1px"
                BorderColor="#EEEEEE"
                ImageUrl='<%# "PlaceImages/" + Eval("Picture") %>' /><br />
                <asp:TextBox ID="BEIPictureField" runat="server" Text='<%# Bind("Picture") %>' /><br />
                <asp:Button ID="UpdatePicButton" runat="server" Text="Update" OnClick="UpdatePic" />
                <br />
            </td>
        </tr>
        <tr>
            <td>
                <asp:Label ID="Label4" CssClass="placeBodyStyle" runat="server" Text='<%# Eval("PicText1") %>' />
            </td>
        </tr>
        <tr>
            <td>
                &nbsp;
            </td>
        </tr>
    </table>
</ItemTemplate>
</asp:DataList>

这些代码块用于两个不同页面上的两个不同按钮。运行第二个代码块时出现的错误是“对象引用未设置为对象的实例”。任何帮助,将不胜感激。

4

1 回答 1

4

此行是可能的原因,它未能找到控件 BEIPictureField,因此“对象引用未设置为对象的实例”。错误

TextBox uPic = (TextBox)DataList1.FindControl("BEIPictureField");

编辑 1

尝试这个:

TextBox uPic = (TextBox)DataList1.Items[1].FindControl("BEIPictureField");

您将不得不重写您的逻辑以在每个项目而不是 DataList 中找到控件,因为它是您不会在那里找到它的父项。

于 2013-10-02T02:30:45.973 回答