Hi @peter liles ,
Based on your description, I guess you want to achieve the effect of the example below.
The following example shows how to get the insert item by using the InsertItem property to access controls that are contained in the InsertItemTemplate template.
Use the InsertItem
property to access the insert item of a ListView control. An insert item is a ListViewItem
object whose ItemType is equal to ListViewItemType.InsertItem. The content of an insert item is defined by the InsertItemTemplate property.
https://learn.microsoft.com/en-us/dotnet/api/system.web.ui.webcontrols.listview.insertitem?view=netframework-4.8
<asp:ListView ID="CountriesListView"
DataSourceID="CountryDataSource"
DataKeyNames="CountryRegionCode"
InsertItemPosition="LastItem"
runat="server"
oniteminserting="CountriesListView_ItemInserting">
<LayoutTemplate>
<table cellpadding="4" width="500" runat="server" id="tblCountries">
<tr runat="server">
<th runat="server">Code</th>
<th runat="server">Name</th>
</tr>
<tr runat="server" id="itemPlaceholder" />
</table>
<asp:DataPager ID="CountriesPager" runat="server" PageSize="20">
<Fields>
<asp:NumericPagerField ButtonCount="10" />
</Fields>
</asp:DataPager>
</LayoutTemplate>
<ItemTemplate>
<tr class="item" runat="server">
<td>
<asp:Label ID="CountryCodeLabel" runat="server"
Text='< % # Ev al("CountryRegionCode")%>' />
</td>
<td>
<asp:Label ID="NameLabel" runat="server"
Text='< % # Ev al("Name") %>' />
</td>
</tr>
</ItemTemplate>
<InsertItemTemplate>
<tr class="insertItem">
<td align="right">Code:</td>
<td align="left">
<asp:TextBox ID="CountryCodeTextBox" runat="server"
Text='< % # Bi nd("CountryRegionCode")%>'
MaxLength="3" />
</td>
</tr>
<tr class="insertItem">
<td align="right">Name:</td>
<td align="left">
<asp:TextBox ID="NameTextBox" runat="server"
Text='< % # Bi nd("Name")%>'
MaxLength="50" />
</td>
</tr>
<tr class="insertItem" runat="server">
<td colspan="2" align="center">
<asp:Button ID="InsertButton" runat="server"
CommandName="Insert" Text="Insert" />
<asp:Button ID="CancelButton" runat="server"
CommandName="Cancel" Text="Clear" />
</td>
</tr>
</InsertItemTemplate>
</asp:ListView>
<br /><br />
<asp:Label ID="MessageLabel"
ForeColor="Red"
runat="server" />
<asp:SqlDataSource ID="CountryDataSource" runat="server"
ConnectionString="< % $ ConnectionStrings:DBCS %>"
SelectCommand="SELECT [CountryRegionCode], [Name]
FROM [CountryRegion]"
InsertCommand="INSERT INTO CountryRegion(CountryRegionCode, Name)
VALUES (@CountryRegionCode, @Name)">
</asp:SqlDataSource>
code behind
protected void Page_Load(object sender, EventArgs e)
{
MessageLabel.Text = "";
}
protected void CountriesListView_ItemInserting(object sender, ListViewInsertEventArgs e)
{
// Get the controls that are contained in the insert item.
TextBox countryCodeTextBox =(TextBox)CountriesListView.InsertItem.FindControl("CountryCodeTextBox");
TextBox nameTextBox =(TextBox)CountriesListView.InsertItem.FindControl("NameTextBox");
//Check if the controls are empty.
if ((countryCodeTextBox.Text.Trim().Length == 0) ||(nameTextBox.Text.Trim().Length == 0))
{
MessageLabel.Text ="The system could not insert the item. All fields are required.";
e.Cancel = true;
return;
}
}
Best regards,
Lan Huang
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.