Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to avoid page refresh after selectedindexchanged of dropdown list?

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>
like image 743
Aqeel Avatar asked Jul 03 '12 12:07

Aqeel


2 Answers

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> 
like image 95
Ashwin Singh Avatar answered Sep 28 '22 10:09

Ashwin Singh


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... :(";
    }
}
like image 32
Jaime Torres Avatar answered Sep 28 '22 10:09

Jaime Torres