Hello developers! In asp.net, I use jQuery to make an ajax request. It works, new data is displayed. But when I want to do a postback , the server doesn’t see the changes I made using jQuery ajax. In particular, I changed the selected item in the dropdownlist-е using jQuery, with a postback thrown out (there is no selected item). Why? And how to make the server with postback see the changes made using jQuery ajax? Oh yes ... The code in the studio! This is jQuery:

  <script type="text/javascript" language="javascript"> $(document).ready(function() { $("#DropDownList1").change(function (){ var url = "Default.aspx #data2"; $("#data2").load( url, { "data1":$("#DropDownList1").val() }); }); }); </script> 

This is the form:

  <form id="form1" runat="server"> <div> <asp:DropDownList ID="DropDownList1" runat="server"> <asp:ListItem>1</asp:ListItem> <asp:ListItem>2</asp:ListItem> <asp:ListItem>3</asp:ListItem> <asp:ListItem>4</asp:ListItem> <asp:ListItem>5</asp:ListItem> </asp:DropDownList> <br /> <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label> <br /> <br /> <div id="data2"> <asp:DropDownList ID="DropDownList2" runat="server"> </asp:DropDownList> </div> <br /> <br /> <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="PostBack" /> <br /> </div> </form> 

And this is the server part:

  protected void Page_Load(object sender, EventArgs e) { if (Request.Params["data1"] != null) { string s = Request.Params["data1"]; for (int i = 0; i < 5; i++) { DropDownList2.Items.Add(s+i); } return; } } protected void Button1_Click(object sender, EventArgs e) { Label1.Text = DropDownList2.SelectedItem.Text; //вот здесь NullReferenceException } 
  • one
    What exactly is a NullReferenceException? - Specter
  • I can reach the necessary data via Request.Param ['mydata']. But it is not that. Already another logic is tied exactly to the values ​​of the controls. Rewriting is not interesting - DmitryBLR

1 answer 1

I can assume the following happens:

  1. When you first load the page, the DropDownList2 control is empty;
  2. Execute ajax request using jQuery .load () . This method loads a whole new page, searches DropDownList2 (already filled in) and inserts it into the old one, nothing more.
  3. Because DropDownList2 is dynamically populated, its contents are saved in ViewState. When ajax is requested, the ViewState of the current page does not change, i.e. still contains a description of the empty DropDownList.
  4. When postback is on the server, the content of the controls is populated from ViewState, in which for DropDownList2 is empty.

Alternatively, it is possible on the server side to repeat the logic of filling the controls exactly as on the client.

  • Yes, most likely it is. That is, you want to say that with the postback you need to go all over again? Call the same methods in the same order as when calling ajax? - DmitryBLR
  • Or you can use javascript to make changes to the viewstate - DmitryBLR
  • Something I do not believe that ViewState is reasonably editable from JavaScript. There is no guarantee that ASP.NET will always generate the same ViewState format, and changes on the page can break the binding to ViewState. - Tolyandre
  • I, too, somehow do not dare to go for it. That is, to make an ajax request, and then, if you need to edit it with javascript ViewState, it is easier to immediately make changes from the ajax request to the ViewState, in some way. - DmitryBLR