Adding Records with InsertItemTemplate

Adding Records with InsertItemTemplate

The InsertItemTemplate of the ListView control is used to define the layout of the form that allows users to add new records to the underlying data source.

To add records with the InsertItemTemplate of the ListView control, follow these steps:

Define the InsertItemTemplate in the markup of the ListView control, which will contain the input controls for the user to enter the data. Here’s an example:

<asp:ListView ID=”MyListView” runat=”server” DataSourceID=”MyLinqDataSource”>

    <InsertItemTemplate>

        <tr>

            <td>

                <asp:TextBox ID=”ProductNameTextBox” runat=”server” Text='<%# Bind(“ProductName”) %>’ />

            </td>

            <td>

                <asp:TextBox ID=”UnitPriceTextBox” runat=”server” Text='<%# Bind(“UnitPrice”) %>’ />

            </td>

            <td>

                <asp:TextBox ID=”UnitsInStockTextBox” runat=”server” Text='<%# Bind(“UnitsInStock”) %>’ />

            </td>

            <td>

                <asp:Button ID=”InsertButton” runat=”server” CommandName=”Insert” Text=”Insert” />

            </td>

        </tr>

    </InsertItemTemplate>

    …

</asp:ListView>

Handle the ItemCommand event of the ListView control, and check if the CommandName property of the ListViewCommandEventArgs parameter is set to “Insert”. If it is, retrieve the values entered by the user from the input controls of the InsertItemTemplate, and insert them into the underlying data source. Here’s an example:

protected void MyListView_ItemCommand(object sender, ListViewCommandEventArgs e)

{

    if (e.CommandName == “Insert”)

    {

        TextBox productNameTextBox = (TextBox)e.Item.FindControl(“ProductNameTextBox”);

        TextBox unitPriceTextBox = (TextBox)e.Item.FindControl(“UnitPriceTextBox”);

        TextBox unitsInStockTextBox = (TextBox)e.Item.FindControl(“UnitsInStockTextBox”);

        // Insert the new record into the data source

        using (MyDataContext db = new MyDataContext())

        {

            Product newProduct = new Product

            {

                ProductName = productNameTextBox.Text,

                UnitPrice = decimal.Parse(unitPriceTextBox.Text),

                UnitsInStock = short.Parse(unitsInStockTextBox.Text)

            };

            db.Products.InsertOnSubmit(newProduct);

            db.SubmitChanges();

        }

    }

} Note that this is just an example, and you may need to modify it to fit the specifics of your application and data source. Additionally, it’s important to validate the user input and handle any errors that may occur during the insertion process.

Apply for ASP.NET Certification Now!!

https://www.vskills.in/certification/certified-aspnet-programmer

Back to Tutorial

Editing Records with EditItemTemplate
Advising Users there’s no Data with EmptyDataTemplate

Get industry recognized certification – Contact us

keyboard_arrow_up
Open chat
Need help?
Hello 👋
Can we help you?