How to avoid page refresh after selectedindexchanged of dropdown list?

43,470

Solution 1

Add this, if you want the dropdownlist to trigger Ajax call without refreshing the page and do not remove AutoPostBack="true"

<Triggers> 
<asp:AsyncPostBackTrigger ControlID="ddList" EventName="SelectedIndexChanged" /> 
</Triggers> 

Solution 2

The crux of your question, I believe, is:

"when i select any value from drop down i load some data from database that depends on this selected value, i am facing a problem whenever selection changes page will be refreshed."

There are many ways to accomplish this, but it might require some restructuring to produce the desired effect. A relatively simple way to do this would be:

(1) Reorganize your page as follows:

<asp:DropDownList ID="ddList" CssClass="dropdown" Style="width: 200px !important;" runat="server" AutoPostBack="false">
</asp:DropDownList>

<asp:UpdatePanel ID="OuterUpdatePanel" runat="server">
        <ContentTemplate>
        <!-- Content goes here -->
        </ContentTemplate>
</asp:UpdatePanel>

(2) Add script as follows:

<script type="text/javascript">
function handleDDLChange() {
  __doPostBack($('div[id$="OuterUpdatePanel"]').attr('id'), 'ddlList_Changed_Or_Anything_Else_You_Might_Want_To_Key_Off_Of');
}

$('input[id$="ddlList"]').change( handleDDLChange );
</script>

This is a more "old-school" approach, but it should solve your issue.

EDIT: The following illustrates a "non-jQuery" approach, with the above idea a little more fleshed out:

ASCX:

<asp:ScriptManager runat="server" />

<asp:DropDownList ID="ddlList" runat="server" onchange="handleDDLChange()">
    <asp:ListItem Text="text1" />
    <asp:ListItem Text="text2" />
</asp:DropDownList>

<script type="text/javascript">
    function handleDDLChange() {
        __doPostBack("<%= ddlList.ClientID %>", "ddlList_Changed_Or_Anything_Else_You_Might_Want_To_Key_Off_Of");
    }
</script>

 <asp:UpdatePanel ID="UpdatePanel1" runat="server">
   <ContentTemplate>
       <asp:Literal ID="litTest" runat="server" />
   </ContentTemplate>
</asp:UpdatePanel>

Code-Behind:

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        litTest.Text = "No postback";
    }
    else if (Request["__EVENTARGUMENT"] == "ddlList_Changed_Or_Anything_Else_You_Might_Want_To_Key_Off_Of")
    {
        litTest.Text = string.Format("Dropdown changed: {0}", ddlList.Text);
    }
    else
    {
        litTest.Text = "Postback for some other reason... :(";
    }
}
Share:
43,470
Aqeel
Author by

Aqeel

A highly motivated Senior Software Engineer and Application Development professional, having 10+ years of experience with proven expertise in using new tools and technical developments to drive improvements throughout an entire application development life-cycle. Having extensive industry and full life cycle experience in a .Net base environment, along with exceptional analytic, design, and problem-solving capabilities.

Updated on March 15, 2020

Comments

  • Aqeel
    Aqeel about 4 years

    I am using update panel and ASP drop down. When I select any value from a drop down list I load some data from a database that depends on this selected value. However, whenever this selection changes the page will be refreshed. How can I avoid this page refresh? I have also tried AsyncPostBackTrigger but still this problem occurs.

    <asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server" EnablePartialRendering="false">
       </asp:ToolkitScriptManager>
            <asp:UpdatePanel ID="OuterUpdatePanel" runat="server">
                <ContentTemplate>
                    <asp:DropDownList ID="ddList" CssClass="dropdown" Style="width: 200px !important;"
                     runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddList_SelectedIndexChanged">
                    </asp:DropDownList>
                </ContentTemplate>
            </asp:UpdatePanel>