I have a DataList in ASP.NET that brings me the products from the "Products" table, so with the instruction "Eval"
I assign the product ID:
<asp:TextBox ID="idProductoText" runat="server" type="hidden" value='<%# Eval("PRO_ID") %>'></asp:TextBox>
So in my C# code I need to get the value of that TextBox by its ID, for example a idProductoText.Text.Trim();
, but for some reason it doesn't work, any solution? I leave the full DataList below.
Code to fill the DataList:
public void cargarStockProductos()
{
OracleConnection conexion = new OracleConnection(con);
OracleCommand command = new OracleCommand("MOSTRAR_PRODUCTOS_COMPRAR", conexion);
command.CommandType = System.Data.CommandType.StoredProcedure;
command.Parameters.Add("registros", OracleDbType.RefCursor).Direction = ParameterDirection.Output;
OracleDataAdapter d = new OracleDataAdapter();
d.SelectCommand = command;
DataTable dt = new DataTable();
d.Fill(dt);
DataList1.DataSource = dt;
DataList1.DataBind();
conexion.Close();
}
Full DataList in ASP.NET
<asp:DataList ID="DataList1" runat="server">
<ItemTemplate>
<div class="card mb-6" style="max-width: 1400px">
<div class="row g-0">
<div class="col-md-4">
<img
src="../../img/sillon.jpg"
class="img-fluid rounded-start"
alt="producto" />
</div>
<div class="col-lg-5 m-4 form-floating">
<div class="card-body">
<!-- EL ID DEL PRODUCTO ESTÁ OCULTO, SOLO SE USARÁ PARA AGREGAR AL CARRITO -->
<asp:TextBox ID="idProductoText" runat="server" type="hidden" value='<%# Eval("PRO_ID") %>'></asp:TextBox>
<asp:Label ID="PRO_NOMBRELabel" class="card-title" runat="server" Text='<%# Eval("PRO_NOMBRE") %>' Font-Bold="true" Font-Size="Large" Visible="True" />
<br />
<br />
Q<asp:Label ID="PRO_PRECIOLabel" class="card-text" runat="server" Text='<%# Eval("PRO_PRECIO") %>' Font-Size="Large" />
<br />
<br />
<div class="input-group">
<asp:Button ID="masInformacion" runat="server" Text="Más Información" class="btn btn-dark m-2" />
<asp:TextBox ID="cantidadComprar" runat="server" type="number" class="form-control m-2" placeholder="Cantidad a Comprar"></asp:TextBox>
<asp:Button ID="agregarCarrito" runat="server" Text="Agregar al Carrito" class="btn btn-success m-2"/ OnClick="agregarCarrito_Click"/>
</div>
</div>
</div>
</div>
</div>
<br />
</ItemTemplate>
</asp:DataList>
In the end I solved it in the following way:
A button type variable is created with the ID of the button that executes the action and the "sender" event, that is, send.
An object of type DataListItem is created when the sender event of the variable "button" or addCart is pressed
With the item element of the datalist, the ID of the Textbox is taken and the object of type textbox (TextBox) is created
It is assigned to a string variable and the result is converted to type string.