简体   繁体   English

将参数传递给OnClick事件

[英]Pass parameter to OnClick event

I have an aspx page with a table. 我有一个带表的aspx页面。 Each row in the table represents a row of data base table (which represents a place). 表中的每一行代表数据库表的一行(代表一个位置)。

I need a way to pass to OnClick event (or another event) a parameter. 我需要一种将参数传递给OnClick事件(或另一个事件)的方法。 I can't use the CommandArgument becouse then I will need to know the idPlace string and I just don't know it. 我不能使用CommandArgument,因为我将需要知道idPlace字符串,而我只是不知道。 I buld the table with FOR loop. 我可以用FOR循环来创建表格。

I need something like: 我需要类似的东西:

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

The main idea that for each place (row) in the table there would be a link to map- a different page which would get the placeId and get the coordinates of the googleMap from another table in data base. 对于表中的每个位置(行)的主要思想是,将有一个映射到地图的链接-一个不同的页面,该页面将获取placeId并从数据库中的另一个表获取googleMap的坐标。

I'm working on it a couple of hours and it's frustrating. 我正在努力几个小时,令人沮丧。

Thanks, Hila 谢谢,希拉

Here is some parts from the code: 这是代码中的一些部分:

<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>

What I want is that when user clicks spesific row (place) I will go to OnClick event IN C# code with the SPECIFIC placeId- and there I'll connect to the data-base, get the coordinates of the place, and Respondr.Rediret to another aspx page- which displays the place on the map. 我想要的是,当用户单击特殊行(地点)时,我将使用带有SPECIFIC placeId-的C#代码进入OnClick事件,然后将在那里连接到数据库,获取地点的坐标,并获取Respondr.Rediret到另一个aspx页面-在地图上显示该地点。 I just need the placeId... 我只需要placeId ...

you cam use Repeater itemcommand event to get the commandargument. 您可以使用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);

    }
}

for more detail have a look at 更多细节请看

http://dotnetrush.blogspot.com/2006/12/using-repeater-itemcommand.html http://dotnetrush.blogspot.com/2006/12/using-repeater-itemcommand.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM