Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to detect the page reload by ajax and if the page have been reloaded only then i want to fill my DistrictId variabl. This reload is happening when a user select value from dropdownlist.

Please note that i am writing this code in View and this is for a dropdownlist named Districtddl
Also
C#
@if (Request.HttpMethod=="POST") {}
is not working


C#
<select class="ddl" id="Districtddl" name="Districtddl">
                            <option>----Select District----</option>
                            @foreach (System.Data.DataRow dr in FarmerRegisterFormAndPrint.Models.StoredProcedures.GetAllDistrict().Rows)
                            {
                                <option id="@dr["DistrictId"].ToString()">@dr["DistrictName"].ToString()</option>
                            }
                        </select>


Quote:
This reload is happening when a user select value from dropdownlist.

C#
@{var DistrictId = "";}
            @if (Request.HttpMethod=="POST") {
                var DistrictId = Request.Form["Districtddl"].ToString();
            }


Quote:
This variable then will be used to pass as parameter to another dropdownlist.
How i can do this.


HTML
<select class="ddl" id="Tehsilddl">
<option id="0">----Select District----</option>
@if (!string.IsNullOrEmpty(DistrictId))
{
foreach (System.Data.DataRow dr in FarmerRegisterFormAndPrint.Models.StoredProcedures.GettehsilAccToDistrict(DistrictId).Rows)
    {
        <option id="@dr["TehsilId"].ToString()">@dr["TehsilName"].ToString()</option>
    }
}
</select>


JavaScript
$("#Districtddl").on('change', function () {
      var valueDistrictDDl = $(this).find('option:selected').attr('id');
      alert(valueDistrictDDl);
      var urlInUse = window.location.href;
      $.ajax({
          url: urlInUse,
          dataType: "html",
          success: function (data) {
              alert("reloaded")

          }
      });


And if any of this doesnot make any sense to you then please tell me how to set jquery variable to here
C#
(System.Data.DataRow dr in FarmerRegisterFormAndPrint.Models.StoredProcedures.GettehsilAccToDistrict(DistrictId).Rows)
it is a lot easy to get the selected id in jquery but do not know how to pass that to DistrictId
Posted
Updated 16-Jul-15 21:13pm
v6
Comments
Mohibur Rashid 17-Jul-15 2:50am    
try the code. Request.Headers["X-Requested-With"]
found on google search
lokopi.nagar 17-Jul-15 3:17am    
Thank you so much i use Request.IsAjaxRequest() but searching your comment gave me a wiki page which have Request.IsAjaxRequest()

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900