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 }