question

peterliles-4155 avatar image
0 Votes"
peterliles-4155 asked LanHuang-MSFT commented

Retain Listview datakey value within insert event handling.

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?

dotnet-aspnet-webpages
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

LanHuang-MSFT avatar image
0 Votes"
LanHuang-MSFT answered

Hi @peterliles-4155,
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://docs.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.



1.gif (98.8 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

peterliles-4155 avatar image
0 Votes"
peterliles-4155 answered LanHuang-MSFT commented

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

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @peterliles-4155,
My understanding is that your need is to get the itemtemplate value.
If the itemtemplate contains a control, you can use FindControl to get the control value.
If using EV AL() directly, get the cell's Control[0] or the control index to get the EV AL value according to your GridView.
Best regards,
Lan Huang

0 Votes 0 ·
peterliles-4155 avatar image
0 Votes"
peterliles-4155 answered LanHuang-MSFT commented

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?

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @peterliles-4155,
Do you want to get the DataKey value in ADD button, maybe you can try the following method.
Pass the RowIndex through the CommandArgument and use it to retrieve the desired DataKey value.
On the Button:

 CommandArgument='< % # DataBinder.Ev al(Container, "RowIndex") % >'

On the Se rver Event

 int rowIndex = int.Parse(e.CommandArgument.ToString());
 string val = (string)this.grid.DataKeys[rowIndex]["myKey"];

Best regards,
Lan Huang

0 Votes 0 ·