Link to home
Start Free TrialLog in
Avatar of malarashok
malarashok

asked on

Partial post back for dropdown asp.net server control

I have asp.net dropdown list, which has list of country.  On select of some of the dropdown item, it should post back.
For example I have United State, India countries in Dropdown list.
On select of United State page should postback and call on select method.
On select of India page should not postback.

Please let me know how to do it.
Avatar of razo
razo

in javascript change event of the dropdownlist check the text and call the server function if needed
if you have India selected default and you change to US it will posback
but when you have india selected default and you again select India it will not post back bcoz you are actually not changing the item..

To resolve this issue add a blank field as first item in the dropdown!!

Regards
Renju
Avatar of malarashok

ASKER

hi Razo,
             javascript change event of the dropdownlist, may work. Programmatically how to post back for post back event  for dropdown select. you share code for java script for post back.

Thanks,
Ashokkumar M
razo & renjurdevan  Thank you for your effort . I have written code for my requirement. Since you have out some effort to find solution. I am sharing the code with you. Solution and code is given below.

Solution:
·  Custom post back has been implemented for country Dropdown; by adding custom postback we can avoid unnecessary page refresh.
·  When user is selecting the country, Java script function will validates if our application has list of state of selected country then page will be postback and populate the state list in dropdown  

 if user selects Canada, Mexico or United States from country dropdown then  corresponding State will be poppulated, for other countries Dropdown will not be populated and not referesh(postback) the page.

Code:
 
File Name:   Address.aspx
<script type="text/javascript" language="javascript">
   function CountrySelectedPostBack(eventTarget, eventArgument,ddlStateProvinceID)
   {
        var ddl = document.getElementById(eventTarget)
//        Canada = 32,
//        Mexico = 119,
//        UnitedStates = 190,
        var ddlStateProvince = document.getElementById(ddlStateProvinceID)
        //if the selected country is Canada , Mexico , UnitedState
        // or previous selected country is Canada , Mexico , UnitedState then
        //post back the page action
        if (ddl.value == "190" || ddl.value == "32" || ddl.value == "119"  || ddlStateProvince != null)
        {
            __doPostBack(eventTarget, eventArgument);
         }
   }        
</script>

 
//Auto postback is removed country dropdown list.
<asp:DropDownList ID="ddlCountry" runat="server" Width="306px" OnSelectedIndexChanged="ddlCountry_SelectedIndexChanged"  />

File Name:   Address.aspx.cs  
Code :
//added client side java script to dropdown control.
ddlCountry.Attributes.Add("onchange", "javascript:CountrySelectedPostBack('" + ddlCountry.ClientID + "','','" + ddlStateProvince .ClientID + "');");

 Thanks,
Ashokkumar M

ASKER CERTIFIED SOLUTION
Avatar of Computer101
Computer101
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial