-1
如何點擊按鈕,並傳輸到變量之後訪問數據列表中的小區...單擊按鈕後訪問數據列表中的單元格?
如何點擊按鈕,並傳輸到變量之後訪問數據列表中的小區...單擊按鈕後訪問數據列表中的單元格?
<ItemTemplate>
<asp:Label ID="ProductNameLabel" runat="server" Text='<%# Eval("Name") %>' /><br />
<asp:ImageButton ID="ImageButton1" runat="server" ImageUrl='<%# Eval("ImageURL","~/Images/{0}") %>' />
<asp:Label ID="PriceLabel" runat="server" Text='<%# Eval("Price", "{0:C}") %>'></asp:Label><br />
<asp:Button ID="buyNowBtn" runat="server" Text="buy Now" OnClick="buyNowBtn_Click" />
</ItemTemplate>
</asp:DataList>
下面我也有打算,以顯示產品名稱和價格
2 Label控件<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<br />
<asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
protected void BuyNowButton_Click(object sender, EventArgs e) { Button BuyNowButton = (Button)sender; DataListItem item = (DataListItem)BuyNowButton.NamingContainer; Label NameLabel = (Label)item.FindControl("NameLabel"); Label ValueLabel = (Label)item.FindControl("ValueLabel"); ResultsLabel.Text += String.Format("Click: {0} - {1}
", NameLabel.Text, ValueLabel.Text); }
上面的代碼看起來整潔,但是當你點擊位於DataGrid中的任何控制,首先觸發的事件將是protected void DataListName_ItemCommand(object source, DataListCommandEventArgs e)
,因此,將代碼放入事件中將是精確的。
然後你可以找到以同樣的方式
ImageButton img = (ImageButton)e.Item.FindControl("ImageButton1");
img.(property that you wanna access)
項目