There is a form Catalog In Catalog.aspx there is a DataList with the element Label2.

<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT [Id], [CatId], [Name], [Price], [Description], [Image] FROM [Products]"> </asp:SqlDataSource> <asp:DataList ID="DataList1" runat="server" DataKeyField="Id" DataSourceID="SqlDataSource1" RepeatColumns="4" RepeatLayout="Flow"> <ItemTemplate> <div class="Item"> <div class="name"> <asp:Label ID="NameLabel" runat="server" Text='<%# Eval("Name") %>' /> </div> <div> Код:<asp:Label ID="Label2" runat="server" Text='<%# Eval("Id") %>' /> </div> <img src="<%# Eval("Image") %>" height="115" alt="item"/> <div class="price"> Цена: <asp:Label ID="PriceLabel" runat="server" Text='<%# Eval("Price")%>' />p. <asp:Button ID="Button2" runat="server" ForeColor="Black" onclick="Button2_Click" Text="В КОРЗИНУ" /> </div> <div class="desc"> <asp:Label ID="DescriptionLabel" runat="server" Text='<%# Eval("Description") %>' /> <br /> </div> </div> </ItemTemplate> </asp:DataList> 

In Catalog.aspx.cs I transfer the data in a DB

 sqlCon.Open(); SqlCommand cmd_SQL = new SqlCommand("INSERT INTO Cart(ClientId,ProductId,Amount) VALUES (@ClientId,@ProductId,@Amount)", sqlCon); cmd_SQL.Parameters.Add("@ClientId", SqlDbType.NVarChar).Value =Membership.GetUser().ProviderUserKey.ToString(); cmd_SQL.Parameters.Add("@ProductId", SqlDbType.NVarChar).Value =Label2.Text; cmd_SQL.Parameters.Add("@Amount", SqlDbType.NVarChar).Value = 1; cmd_SQL.CommandType = CommandType.Text; cmd_SQL.ExecuteNonQuery(); 

Underlines Label2 in Catalog.aspx.cs, writes that it does not exist. How do I transfer the value from Label2 (Id from the Products table)?

  • Which class owns a method with cmd_SQL.ExecuteNonQuery(); ? Object method or static? Do you have that "Amount" string? - Igor
  • Amount with type int - LEgXr

1 answer 1

Label2 emphasized because there really is no such element.
You use asp:DataList which you specify an ItemTemplate - this means that the elements described inside the template are created and, accordingly, are available after data binding.

Now by code: I suspect that the code

 sqlCon.Open(); SqlCommand cmd_SQL = new SqlCommand("INSERT INTO Cart(ClientId,ProductId,Amount) VALUES (@ClientId,@ProductId,@Amount)", sqlCon); cmd_SQL.Parameters.Add("@ClientId", SqlDbType.NVarChar).Value =Membership.GetUser().ProviderUserKey.ToString(); cmd_SQL.Parameters.Add("@ProductId", SqlDbType.NVarChar).Value =Label2.Text; cmd_SQL.Parameters.Add("@Amount", SqlDbType.NVarChar).Value = 1; cmd_SQL.CommandType = CommandType.Text; cmd_SQL.ExecuteNonQuery(); 

Located in the Button2_Click event handler and called here.

 <asp:Button ID="Button2" runat="server" ForeColor="Black" onclick="Button2_Click" Text="В КОРЗИНУ" /> 

In this case, the handler accepts the sender button as a parameter.

you can get it: var btn = (Button)sender;

Further, the web control has a Parent field; in this case, it points to a DataListItem object. This is the container that contains controls from the ItemTemplate markup. It remains only to find the desired item using the FindControls method FindControls

All together will be:

 var btn = (Button)sender; var dli = (DataListItem)btn.Parent; var Label2 = (Label)dli.FindControl('Label2'); 

or one line

 var Label2 = (Label)(((DataListItem)((Button)sender).Parent).FindControl("Label2"));