Home > OS >  Get value of a TextBox inside a DataList
Get value of a TextBox inside a DataList

Time:04-28

I have a DataList in ASP.NET that brings me the products from the "Products" table, so with the "Eval" statement 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 an idProductText.Text.Trim(); , but for some reason it doesn't work, any solution? I leave the complete DataList below.

Code to fill the DataList:

public void loadStockProducts()
        {
            OracleConnection connection = new OracleConnection(with);
            OracleCommand command = new OracleCommand("SHOW_PRODUCTS_BUY", connection);
            command.CommandType = System.Data.CommandType.StoredProcedure;
            command.Parameters.Add("registers", OracleDbType.RefCursor).Direction = ParameterDirection.Output;
            OracleDataAdapter d = new OracleDataAdapter();
            d.SelectCommand = command;
            DataTable dt = new DataTable();
            d.Fill(dt);
            DataList1.DataSource = dt;
            DataList1.DataBind();
            connection.Close();
        }

Full DataList in ASP.NET

<asp:DataList ID="DataList1" runat="server">
                    <ItemTemplate>
                        <div  style="max-width: 1400px">
                            <div >
                                <div >
                                    <img
                                        src="../../img/armchair.jpg"
                                        
                                        alt="product" />
                                </div>
                                <div >
                                    <div >
                                        <!-- THE PRODUCT ID IS HIDDEN, IT WILL ONLY BE USED TO ADD TO CART -->
                                        <asp:TextBox ID="idProductoText" runat="server" type="hidden" value='<%# Eval("PRO_ID") %>'></asp:TextBox>
                                        <asp:Label ID="PRO_NAMELabel"  runat="server" Text='<%# Eval("PRO_NAME") %>' Font-Bold="true" Font-Size="Large" Visible="True" />
                                        <br />
                                        <br />
                                        Q<asp:Label ID="PRO_PRICELabel"  runat="server" Text='<%# Eval("PRO_PRICE") %>' Font-Size="Large" />
                                        <br />
                                        <br />
                                        <div >
                                          <asp:Button ID="moreInformation" runat="server" Text="More Information"  />
                                          <asp:TextBox ID="quantidadBuy" runat="server" type="number"  placeholder="Quantity to Buy"></asp:TextBox>
                                          <asp:Button ID="addCart" runat="server" Text="Add to Cart" / OnClick="addCart_Click"/>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <br />
                    </ItemTemplate>
                </asp:DataList>

CodePudding user response:

Ok, while there are some events of the data list, you can just grab and get all the information you need/want from that button click you have.

However, before we write that code, I see you need a database row PK id, so, I suggest you remove this:

<!-- THE PRODUCT ID IS HIDDEN, IT WILL ONLY BE USED TO ADD TO CART -->
<asp:TextBox ID="idProductoText" runat="server" 
 type="hidden" value='<%# Eval("PRO_ID") %>'></asp:TextBox>

Assuming PRO_ID is the PK database id, then we really don't want to have that information in the markup - (even hidden).

So, use the so called "data keys" feature. You COULD leave the above, but remove it - we really don't need it.

So, in datalist, add this setting:

 <asp:DataList ID="DataList1" runat="server" DataKeyField = "PRO_ID" >

Ok, so now the click event for the button.

Say we have this button:

 <asp:Button ID="cmdView" runat="server" Text="View" CssClass="btn"
     OnClick="cmdView_Click" />

Code behind:

    protected void cmdView_Click(object sender, EventArgs e)
    {
        Button cmdView = sender as Button;
        DataListItem gRow = cmdView.NamingContainer as DataListItem;

        // get row index
        Debug.Print("row click = "   gRow.ItemIndex.ToString());

        // get database primary key (data keys)
        int? PkID = DataList1.DataKeys[gRow.ItemIndex] as int?;
        Debug.Print("Database PK id = "   PkID);

        // get value of single control - say hotel label called 

        Label lHotelName = gRow.FindControl("HotelNameLabel") as Label;
        Debug.Print("Hotel name = "   lHotelName.Text);
    }

Output:

enter image description here

So note several things:

We grab/get current row - naming container. This works for Gridview, repeater, listview - quite much all of the data bound types of controls.

From that row, then we can get:

Row index -

From Row index, we reference into data keys. Note that datalist ONLY supports ONE data key, so in MOST cases, we have to do this .DataKeys[ some index]["SOME KEY"]

But datalist is a exception - only one key, so .DataKeys[some index] is all you require.

And to pull controls out, use .FindControl as I show above.

FYI: debug.print - this requires using System.Diagnostics; (and I have my outputs re-directed to Immediate window).

  • Related