[英]Pass parameter to OnClick event
我有一個帶表的aspx頁面。 表中的每一行代表數據庫表的一行(代表一個位置)。
我需要一種將參數傳遞給OnClick事件(或另一個事件)的方法。 我不能使用CommandArgument,因為我將需要知道idPlace字符串,而我只是不知道。 我可以用FOR循環來創建表格。
我需要類似的東西:
<asp:ImageButton ID="ImageButton1"
runat="server"
ImageUrl="Maps.ico"
**CommandArgument = '<%=placesDataTable[0]%>'**
/>
對於表中的每個位置(行)的主要思想是,將有一個映射到地圖的鏈接-一個不同的頁面,該頁面將獲取placeId並從數據庫中的另一個表獲取googleMap的坐標。
我正在努力幾個小時,令人沮喪。
謝謝,希拉
這是代碼中的一些部分:
<body dir="rtl">
<form id="form1" runat="server" style="background-color:Fuchsia">
<!-- Here I will display all the results:-->
<p style="font-style:italic">Here are the results:</p>
<table width="100%" border="1" cellpadding="0" cellspacing="0">
<%
System.Data.DataTable placesDataTable = (System.Data.DataTable)Session["PlacesDataTable"]; // The table of all the places from the dataBase.
System.Data.DataRow rowOfPlace;
for (int i = 0; i < placesDataTable.Rows.Count; i++)
{
<tr>
<%
for (int j = 1; j < placesDataTable.Columns.Count; j++)
{%>
<td>
<% Response.Write(rowOfPlace[j].ToString()); %>
</td>
<%
} // end FOR LOOP over the columns
// insert MAP column content:
%>
<td>
<asp:ImageButton ID="ImageButton1"
runat="server"
ImageUrl="Maps.ico"
CommandArgument = '<%placesDataTable[i]%>'
/>
</td>
</tr>
<%
}
%>
</table>
</form>
</body>
我想要的是,當用戶單擊特殊行(地點)時,我將使用帶有SPECIFIC placeId-的C#代碼進入OnClick事件,然后將在那里連接到數據庫,獲取地點的坐標,並獲取Respondr.Rediret到另一個aspx頁面-在地圖上顯示該地點。 我只需要placeId ...
您可以使用Repeater itemcommand事件獲取命令參數。
<asp:ImageButton ID="ImageButton1"
runat="server"
ImageUrl="Maps.ico"
CommandName="MapIt"
CommandArgument = '<%#Eval("ID")%>'
/>
protected void rptComments_ItemCommand(object source, RepeaterCommandEventArgs e) {
if(e.CommandName.ToLower().Equals("mapit")) {
var id = int.Parse(((ImageButton)e.CommandSource).CommandArgument);
}
}
更多細節請看
http://dotnetrush.blogspot.com/2006/12/using-repeater-itemcommand.html
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.