开发者

Findcontrol Listview Edit - NullreferenceException

开发者 https://www.devze.com 2023-03-08 15:52 出处:网络
I have a rather odd problem. I have an editable ListView, however - when I click edit I usually get the NullreferenceException once I try to get the textboxes. BUT the error is not consistent, sometim

I have a rather odd problem. I have an editable ListView, however - when I click edit I usually get the NullreferenceException once I try to get the textboxes. BUT the error is not consistent, sometimes it is there, sometimes not. I'm confused.

This is the code snippit:

public void test_ItemEditing(Object sender, ListViewEditEventArgs e)
{

    // Liest die SpeiseID mit der die Daten aus der Datenbank extrahiert werden können
    DataKey currentDataKey = speiseplanListView.DataKeys[e.NewEditIndex];

    TextBox editDatum  = (TextBox)speiseplanListView.EditItem.FindControl("txtDatum");


}

this is the asp.net file:

<EditItemTemplate>
    <tr>
        <td><asp:TextBox ID="txtDatum" runat="server"></asp:TextBox></td>
        <td><asp:TextBox ID="txtSpeise" runat="server"><开发者_如何学C;/asp:TextBox></td>

    </tr>
</EditItemTemplate>


replace your test_ItemEditing code with below one

public void test_ItemEditing(Object sender, ListViewEditEventArgs e)
{
    lvEmployee.EditIndex = e.NewEditIndex;

    test.DataSource = GetData("Select * from Table");

    test.DataBind();
}

and handle the test_ItemUpdating event for finding the control and updating the data.

0

精彩评论

暂无评论...
验证码 换一张
取 消