Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/441.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将参数传递给OnClick事件_C#_Javascript_Asp.net - Fatal编程技术网

C# 将参数传递给OnClick事件

C# 将参数传递给OnClick事件,c#,javascript,asp.net,C#,Javascript,Asp.net,我有一个带表格的aspx页面。表中的每一行表示一行数据库表(表示一个位置) 我需要一种向OnClick事件(或另一个事件)传递参数的方法。 我不能使用CommandArgument,因为我需要知道idPlace字符串,而我只是不知道它。 我在桌子上搭了一个圈 我需要像这样的东西: <asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="Map

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

我需要一种向OnClick事件(或另一个事件)传递参数的方法。 我不能使用CommandArgument,因为我需要知道idPlace字符串,而我只是不知道它。 我在桌子上搭了一个圈

我需要像这样的东西:

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

以下是结果:


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

您可以使用Repeater itemcommand事件来获取commandargument

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

    }
}

您能在onclick或任何其他事件中显示更多的代码和您真正想要的对象吗?我在原始消息中添加了它(这是一条注释的大量文本)。为什么不使用repeater创建表呢?在您的情况下,单击ImageButton时不会唯一标识,commandARguments与数据绑定控件(如grid、datalist等)一起使用。。。我真的不知道中继器是什么意思。您是否有使用此命令的示例,以及如何将CommandArguments与之一起使用?顺便说一下,除了ImageButton,我还可以使用其他控件。这只是标识的问题…检查一下中继器的使用情况。问题是我不需要asp控件ID。我需要位置的ID(sataBase中的PlacesTable有一个主键,名为placeId)。如何传递此参数?是否有一种简单的方法可以将参数(placeId)传递给事件?!我将不得不改变我的所有项目为这个中继器。。。commandArgument中的Thank.ID不是控件ID,而是绑定到repeater的datatable的ID列(如果datatable中存在名为ID的列)。