Retain Listview datakey value within insert event handling.

peter liles 556 Reputation points
2022-05-16T23:34:48.003+00:00

I have a Listview bound to sqldatasource. I open the insert template from a add button within selected itemtemplate from page behind. I would like to store some of these itemtemplate values within insert template or retrieve these values when i insert a record.
So how may i retrieve them from within the ADD button click event or else populate parameters during insert event ?
Obtain the datakey value when i click the add button would also be useful too?

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,248 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Lan Huang-MSFT 25,471 Reputation points Microsoft Vendor
    2022-05-17T02:40:07.58+00:00

    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;  
                }  
      
            }  
    

    202611-1.gif
    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.

    0 comments No comments

  2. peter liles 556 Reputation points
    2022-05-23T19:15:05.69+00:00

    Thats one way of doing it by declaring the property.But how may i refer to itemtemplate values from the insert event?
    I guess that it not possible to obtain the datakey value from the insert event too?

    Thanks


  3. peter liles 556 Reputation points
    2022-05-26T19:50:29.027+00:00

    What if i was in insert mode and wanted to retrieve values that reside in itemtemplate before i transferred to Insert mode.
    I don't think that is possible?