简体   繁体   English

如何用jQuery Ajax请求替换部分asp.net页面?

[英]How to replace partially asp.net page with a jquery ajax request?

Let's say, I have a Listview control on the page. 假设我在页面上有一个Listview控件。 Each Listview item has 2 dropdownlists and 1 textbox. 每个Listview项都有2个下拉列表和1个文本框。 When the first dropdownlist has a selected value changed, the second dropdownlist should be populated with city depending on the first one. 当第一个下拉列表的选定值已更改时,第二个下拉列表应根据城市填充第一个下拉列表。 Finaly, when a value is selected for the second dropdownlist, a cost should appear on the textbox. 最后,当为第二个下拉列表选择一个值时,费用应出现在文本框中。

<asp:ListView ID="_lsvCostFinder" runat="server">
    <LayoutTemplate>
        <table>
            <tr>
                <th>Country</th>
                <th>City</th>
                <th>Cost</th>
            </tr>
            <tr  runat="server" id="itemPlaceHolder">
            </tr>
        </table>
    </LayoutTemplate>
    <ItemTemplate>
        <tr>
            <td>
                <asp:DropDownList ID="_ddlCountry" runat="server" OnSelectedIndexChanged="ddlCountry_SelectedIndexChanged" AutoPostBack = "true">
                </asp:DropDownList>
            </td>
            <td>
                <asp:DropDownList ID="_ddlCity" runat="server" OnSelectedIndexChanged="ddlCity_SelectedIndexChanged" AutoPostBack = "true">
                </asp:DropDownList>
            </td>
            <td>
                <asp:TextBox ID="_txtCost" runat="server"></asp:TextBox>
            </td>
        </tr>
    </ItemTemplate>
</asp:ListView>

Now I want to use ajax when user selects a different value for the dropdownlist 现在我想在用户为下拉列表选择其他值时使用ajax

$('select').change(function () {
   $.ajax({
  type: "POST",
  url: "Default.aspx/DropDownList1_SelectedIndexChanged",
  data: "{}",
  success: function(msg) {
    // Replace the div's content with the page method's return.
    //
  }
 });

I want to replace only the entire tr element. 我只想替换整个tr元素。 Those are my questions: 这些是我的问题:

  1. What should I send in the data? 我应该发送什么数据? The value of the dropdownlist? 下拉列表的值?

  2. How do I replace only the row containing the dropdownlist that triggered the ajax call? 如何仅替换包含触发ajax调用的dropdownlist的

EDIT 编辑

All the methods have the following signature 所有方法都具有以下签名

protected void ddlCountry_SelectedIndexChanged(object sender, EventArgs e)

It's a very long and complex page. 这是一个非常漫长而复杂的页面。

Thanks for helping 感谢您的帮助

Firstly, looking at the sample code you posted and how you plan to make the Ajax call, you will need to enable PageMethods in your page. 首先,查看您发布的示例代码以及计划如何进行Ajax调用,您将需要在页面中启用PageMethods See this tutorial on how to do that part. 请参阅本教程 ,了解如何执行该部分。

The actual ajax call using jQuery can be as simple as this: 使用jQuery进行实际的Ajax调用可以很简单:

$('select').change(function () {
     $.post('Default.aspx/MethodName',{selectedVal:$(this).val()}, function(data){
         //as far as placing the result in the input text field, I think this will do:
         $(this).parents('tr').find('input:text').val(data);
     });
});

Now, your MethodName method would have to look like this: 现在,您的MethodName方法必须看起来像这样:

  [WebMethod]
  public static string MethodName(string selectedVal)
  {
     return "something";
  }

jsfiddle (minus the ajax call). jsfiddle(减去ajax调用)。

Update 更新

Sorry, I misread the question, the OP wants to replace the WHOLE row. 抱歉,我误解了问题,OP希望替换整个行。 This will do then: 然后将执行以下操作:

 $(this).parents('tr').replaceWith('<tr><td colspan="3"><div>'+data+'</div></td></tr>');

Full context: 全文:

$('select').change(function () {
         $.post('Default.aspx/MethodName',{selectedVal:$(this).val()}, function(data){
             //as far as replacing the row, this will do:
               $(this).parents('tr').replaceWith('<tr><td colspan="3"><div>'+$(this).val()+'</div></td></tr>');
         });
    });

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

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