[SOLVED] How to store information in the view list in the database

Issue

How can I save rows created in the list view in my database? How can I access the values ​​in the (list view)? How to save the values ​​created in the view list to the database?

<asp:ListView ID="ListView1" runat="server">
    <ItemTemplate runat="server">
        <tr class="text-center">
            <td class="product-remove"></td>

            <td class="image-prod">
                <div class="">
                    <asp:Image ID="Image1" CssClass=" img" ImageUrl='<%# "../img/" + Eval("picture" )%>' runat="server" />
                </div>
            </td>

            <td class="product-name"><%# Eval("namebook") %> </td>


           
                <td id="t_Plural" runat="server" class="price "><%# Eval("Price") %> </td>
                
            



            <td class="quantity"><%# Eval("titel") %></td>



            <td class="col-2">


                <asp:Button ID="delete" CssClass="btn btn-outline-danger" CommandArgument='<%# Eval("id")%>' OnClick="delete_Click" runat="server" Text="حذف کالا" />


            </td>

            <td>
                <%--                                            <input id="quantity2" runat="server" type="number" AutoPostBack="true" oninput="lod_gheymat" onserverclick="lod_gheymat" value="" min="1" max="20" />--%>
                <asp:TextBox ID="quantity2" runat="server" CssClass="input-wrap" AutoPostBack="true" OnTextChanged="lod_gheymat" Text="1" min="1" max="20"></asp:TextBox>
            </td>

            <td >
                <div class="row">
               <asp:Label ID="lbl_Plural" runat="server" Text="0"></asp:Label>&nbsp;&nbsp;&nbsp;&nbsp; <span class="row"> تومان</span>
                    </div>
            </td>
        </tr>

    </ItemTemplate>
</asp:ListView>

enter image description here

Solution

protected void savetodatabase_Click(object sender, EventArgs e)
{

    System.Web.UI.HtmlControls.HtmlGenericControl Labelid;
    TextBox quantity2;
    foreach (ListViewItem rRow in ListView1.Items)
    {
        itemlist iteml = new itemlist();
        quantity2 = (TextBox)rRow.FindControl("quantity2");
        Labelid = (System.Web.UI.HtmlControls.HtmlGenericControl)rRow.FindControl("Labelid");
        iteml.quantity = quantity2.Text;
        iteml.bookid = Convert.ToInt32(Labelid.InnerText) ;
        iteml.Date = DateTime.Now;
        iteml.userid =  Convert.ToInt32(Session["userid"].ToString()) ;
        DatabaseContext context = new DatabaseContext();

        context.itemlists.Add(iteml);
        context.SaveChanges();

    }

}

Answered By – Stephen sf

Answer Checked By – Senaida (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *