简体   繁体   English

使用带有传递参数的jquery重定向到asp.net中的页面

[英]Redirecting to a page in asp.net using jquery with passing parameters

How do I call a C# method in jquery script with passing parameters? 如何在带有传递参数的jquery脚本中调用C#方法? I have a jquery accordion with icons implemented dynamically in code behind, once the user clicks the icon it should redirect to a certain page with passing parameter in the url. 我有一个jQuery手风琴,其图标在后面的代码中动态实现,一旦用户单击该图标,它便应重定向到某个带有URL中传递参数的页面。 The data inside the accordion will be populated from the database and what I want to do is for example, when I click on the edit icon, it'd redirect to the editing page with the id of that specific record. 手风琴内部的数据将从数据库中填充,例如,当我单击编辑图标时,它将重定向到具有该特定记录ID的编辑页面。

This is how my jQuery accordion looks like: 这是我的jQuery手风琴的样子:

屏幕截图

Accordion code: 手风琴代码:

<div id="accordion">
    <h3>Section 1 <span class="ui-icon ui-icon-lightbulb"></span></h3>
<div>
<p>
Mauris mauris ante, blandit et, ultrices a, suscipit eget, quam. Integer
ut neque. Vivamus nisi metus, molestie vel, gravida in, condimentum sit
amet, nunc. Nam a nibh. Donec suscipit eros. Nam mi. Proin viverra leo ut
odio. Curabitur malesuada. Vestibulum a velit eu ante scelerisque vulputate.
</p>
</div>

An example of code when clicking the icon in jquery accordion: 单击jQuery手风琴中的图标时的代码示例:

$('span.ui-icon-lightbulb').click(function(e) {
    alert('Lightbulb');
    e.preventDefault();
    e.stopPropagation();
});

It is similar to the following href functionality 它类似于以下href功能

<a href='EditRecord.aspx?recordID=" + record.ID + "'>Edit record</a>

but how to do it in jquery by passing the record id from asp.net c# code behind? 但是如何通过传递来自asp.net c#代码的记录ID在jquery中做到这一点呢?

I've had success in the past using C# and static [Web Methods] with jQuery/Ajax. 过去,我在jQuery / Ajax上使用C#和静态[Web Methods]取得了成功。 From what I remember the method had to be declared as a public static [WebMethod]. 据我public static该方法必须声明为public static [WebMethod]。

For example: 例如:

 [WebMethod]
 public static FooObj GetFooObj ()
 {
     // some code that returns FooObj 
 }

This thread may be of interest. 该线程可能是您感兴趣的。

ASP.NET calling non-static webmethod from JS AJAX ASP.NET从JS AJAX调用非静态Web方法

I found another simple way of doing it, thanks anyway guys. 谢谢大家,我找到了另一种简单的方法。

I'd post it just in case anyone needs it so here it is: first of all, I set the ID of the span tag in code behind to the record's ID. 我将其发布,以防万一有人需要,所以在这里:首先,我将代码后面的span标签的ID设置为记录ID。 Then, I used e.target.id to get the id of the span element which in the previous step I set it to the record's ID. 然后,我使用e.target.id来获取span元素的ID,在上一步中,我将其设置为记录的ID。 Finally, I used var url = "EditRecord.aspx?recordID=" + e.target.id; $(location).attr('href', url); 最后,我使用var url = "EditRecord.aspx?recordID=" + e.target.id; $(location).attr('href', url); var url = "EditRecord.aspx?recordID=" + e.target.id; $(location).attr('href', url); to redirect to the edit page. 重定向到编辑页面。

  <script>
            $(function () {
                $("#accordion").accordion();
                $('span.ui-icon-pencil').click(function (e) {
                    e.preventDefault();
                    e.stopPropagation();
                    var url = "EditRecord.aspx?recordID=" + e.target.id;
                    $(location).attr('href', url);

                });
            });

   </script>

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

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