Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

asp dropdownlist postback not working in update panel when using <base> url in masterpage

I am using, <base href="http://localhost:80/"> in my master page for base url,

now when I am using dropdownlist control on content page (located in localhost:80/directory1/directory2) within update panel, selectedindexchanged event is not working.

I tried to figure out, but in network tab of firefox console I found that request is looking for content page at base url only that is at localhost:80/contenpage.aspx instead of localhost:80/directory1/directory2/contenpage.aspx and giving error

The resource cannot be found.

like image 756
Aniket Bhansali Avatar asked Jan 28 '16 02:01

Aniket Bhansali


3 Answers

The relevant circumstance is that the form action is set to a relative URL in ASP.NET by default:

<form id="ctl01" action="./webform1" method="post">
    <!-- ... -->
</form>

If you use the base tag, you change the base path that the page uses to interpret relative URLs. In your case, the base URL points to a path that is obviously not able to serve the pages of the application. In order to fix this, I'd reconsider whether the base tag is necessary. If it is, it should point to a URL that is able to serve the pages of the application. In order to set the base path to the base path of the application dynamically, you can use the following code:

protected void Page_Load(object sender, EventArgs e)
{
    baseCtrl.Attributes["href"] = new Uri(Request.Url, "/").OriginalString;
}

As a work-around if you need to keep the base tag as it is, I've also tried to set the form action to an absolute URL by integrating the following code into the master page:

protected void Page_Load(object sender, EventArgs e)
{
    Page.Form.Action = Request.Url.OriginalString;
}

This worked in my small sample so that the page could execute the PostBack successfully. Though the form is posted back to the same location as if there was no base tag, there might be some side-effects because other resources on the page (e.g. CSS, JavaScript files and the like) might also be referenced with a relative URL and therefore be retrieved from another location. So I'd still propose to reevaluate the base tag before resorting to this solution.

like image 58
Markus Avatar answered Oct 03 '22 05:10

Markus


if you have some asp component with Autopostback="true" and ClientIdMode="Static", you have to use the triger!

like this:

<asp:UpdatePanel ID="upPrinceOffuce" runat="server">
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="myCustomDropdown" EventName="SelectedIndexChanged" />
    </Triggers>
    <ContentTemplate>
         <asp:DropDownList ID="myCustomDropdown" runat="server" ClientIDMode="Static" AutoPostBack="true"
</asp:DropDownList>
    </ContentTemplate>
</asp:UpdatePanel>
like image 21
Srinivas Ramakrishna Avatar answered Oct 03 '22 07:10

Srinivas Ramakrishna


Try specifiing absolute url in form's action.

like image 36
Qwertiy Avatar answered Oct 03 '22 06:10

Qwertiy