将参数传递给 OnClick 事件

Pass parameter to OnClick event

本文关键字:事件 OnClick 参数传递      更新时间:2023-09-26

我有一个带有表格的aspx页面。表中的每一行表示一行数据库基表(表示一个位置(。

我需要一种方法将参数传递给 OnClick 事件(或其他事件(。我不能使用 CommandArgument becouse,那么我需要知道 idPlace 字符串,而我只是不知道它。我用 FOR 循环在桌子上。

我需要这样的东西:

<asp:ImageButton ID="ImageButton1"
                     runat="server" 
                     ImageUrl="Maps.ico"
                     **CommandArgument = '<%=placesDataTable[0]%>'**         
                     />

主要思想是,对于表中的每个位置(行(,将有一个指向map的链接 - 一个不同的页面,该页面将获取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>

我想要的是,当用户单击特定行(地点(时,我将使用特定的 placeId 转到 C# 代码中的 OnClick 事件,在那里我将连接到数据库,获取该地点的坐标,并将 Respondr.Rediret 连接到另一个 aspx 页面 - 在地图上显示该地点。我只需要地方 ID...

您可以使用 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