ASP.NET displays ListView InsertITemtemplate on button click

I want to display ListView InsertITemtemplate item on button click (i.e. not display by default), but was unable to do so. I tried InsertItemPosition.None

, but then it doesn't show up on button click. I'm sure it should be very simple, but I'm running out of time to complete this task. Any help would be greatly appreciated.

Ali

+2


source to share


3 answers


Try the following:

    <asp:ListView ID="ListView1" runat="server" DataSourceID="LinqDataSource1" InsertItemPosition="None">
    <LayoutTemplate>
      <div runat="server" id="lstProducts">
        <asp:Button ID="btnAddProduct" runat="server" Text="Add Product" 
              CommandName="Create" OnClick="btnAddProduct_Click" />
        <div runat="server" id="itemPlaceholder" />
      </div>
    </LayoutTemplate>
    <ItemTemplate>
            <div><%# Eval("ProductName") %></div>
    </ItemTemplate>
    <InsertItemTemplate>
        <asp:TextBox ID="txtProductName" runat="server" ></asp:TextBox>
    </InsertItemTemplate>
</asp:ListView>

      



Then in the code behind the onclick event

    protected void btnAddProduct_Click(object sender, EventArgs e)
    {
        ListView1.InsertItemPosition = InsertItemPosition.FirstItem;
    }

      

+7


source


I am handling this scenario on the client side, with something like the following:

<InsertItemTemplate>
  <tr id="InsertRow" style="display:none">
    ...
  </tr>
</InsertItemTemplate>

      

Then you can customize the button (either a regular html button or a Button control) using a client-side script like:



<input 
    type="button" 
    value="Show Insert" 
    onclick="document.getElementById('InsertRow').style.display='';" /> 

      

to make the string appear when you want.

+1


source


Add CommandName = "Paste" to your ASP.NET button.

0


source







All Articles