我在 ASP.NET 中有一個 DataList,它為我帶來了“產品”表中的產品,因此使用“Eval”陳述句我分配了產品 ID:
<asp:TextBox ID="idProductoText" runat="server" type="hidden" value='<%# Eval("PRO_ID") %>'></asp:TextBox>
因此,在我的 C# 代碼中,我需要通過其 ID 獲取該 TextBox 的值,例如 an idProductText.Text.Trim();,但由于某種原因它不起作用,有什么解決方案嗎?我將完整的 DataList 留在下面。
填充 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();
}
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/armchair.jpg"
class="img-fluid rounded-start"
alt="product" />
</div>
<div class="col-lg-5 m-4 form-floating">
<div class="card-body">
<!-- 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" class="card-title" runat="server" Text='<%# Eval("PRO_NAME") %>' Font-Bold="true" Font-Size="Large" Visible="True" />
<br />
<br />
Q<asp:Label ID="PRO_PRICELabel" class="card-text" runat="server" Text='<%# Eval("PRO_PRICE") %>' Font-Size="Large" />
<br />
<br />
<div class="input-group">
<asp:Button ID="moreInformation" runat="server" Text="More Information" class="btn btn-dark m-2" />
<asp:TextBox ID="quantidadBuy" runat="server" type="number" class="form-control m-2" placeholder="Quantity to Buy"></asp:TextBox>
<asp:Button ID="addCart" runat="server" Text="Add to Cart" class="btn btn-success m-2"/ OnClick="addCart_Click"/>
</div>
</div>
</div>
</div>
</div>
<br />
</ItemTemplate>
</asp:DataList>
uj5u.com熱心網友回復:
好的,雖然資料串列中有一些事件,但您只需單擊該按鈕即可獲取并獲取您需要/想要的所有資訊。
但是,在我們撰寫該代碼之前,我看到您需要一個資料庫行 PK id,所以,我建議您洗掉它:
<!-- 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>
假設 PRO_ID 是 PK 資料庫 ID,那么我們真的不想在標記中包含該資訊 - (甚至隱藏)。
因此,請使用所謂的“資料密鑰”功能。您可以保留上述內容,但將其洗掉 - 我們真的不需要它。
因此,在 datalist 中,添加以下設定:
<asp:DataList ID="DataList1" runat="server" DataKeyField = "PRO_ID" >
好的,現在按鈕的點擊事件。
假設我們有這個按鈕:
<asp:Button ID="cmdView" runat="server" Text="View" CssClass="btn"
OnClick="cmdView_Click" />
后面的代碼:
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);
}
輸出:

所以要注意幾點:
我們抓取/獲取當前行 - 命名容器。這適用于 Gridview、repeater、listview - 幾乎所有的資料系結型別的控制元件。
從那一行,我們可以得到:
行索引 -
從行索引,我們參考到資料鍵。請注意,datalist 僅支持一個資料鍵,因此在大多數情況下,我們必須這樣做 .DataKeys[ some index]["SOME KEY"]
但是 datalist 是一個例外 - 只有一個鍵,所以 .DataKeys[some index] 就是你所需要的。
要拉出控制元件,請使用 .FindControl,如上所示。
僅供參考:debug.print - 這需要使用 System.Diagnostics;(并且我將輸出重定向到即時視窗)。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/466781.html
