0

I need a DropDownlist which can cause SelectedIndexChanged event but do not do post back(do not need page_load event when ddl index changed).

After looking into solutions in Stack here is my code:

  <asp:UpdatePanel runat="server">

            <ContentTemplate>
                <asp:DropDownList ID="a" runat="server" OnSelectedIndexChanged="a_SelectedIndexChanged">
                    <asp:ListItem Text="a"></asp:ListItem>
                    <asp:ListItem Text="b"></asp:ListItem>
                </asp:DropDownList>

            </ContentTemplate>
            <Triggers>
                <asp:AsyncPostBackTrigger ControlID="a" EventName="SelectedIndexChanged" />
            </Triggers>
        </asp:UpdatePanel>

and C# code:

public partial class TestApp : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        txtDate.Text = DateTime.Now.ToString("yyyy-MM-dd");
    }

    protected void a_SelectedIndexChanged(object sender, EventArgs e)
    {
        int a = 1;
    }
}

But the break point in put in a_SelectedIndexChanged never gets fired. What could be the issue here?

As you see the above is an accpeted answer on stack, then how its not workining: How DropDownList's SelectedIndexChanged() works without PostBack?

Edit: While the below answer correctly explains that page_load should fire all the times. I still want to know why SelectedIndexChanged never gets fired. So what EventName="SelectedIndexChanged" in the Trigger does?

S Nash
  • 2,363
  • 3
  • 34
  • 64

1 Answers1

2

While a update panel (UP for this post) will "ajax" up, and ONLY post-back that part of the page. (and code behind for that UP can only update controls in that UP?).

The page load even will ALWAYS fire each time.

In fact, page load fires for any button click, post-back, changed event or anything at all.

Thus, as a STANDARD design pattern?

If your page load event loads up a drop down list, listbox, gridview etc.?

Then you ONLY want that code to run on the "real" first page load.

As a result, the last 200+ web pages I have built?

They have this:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            LoadData();
    }

    void LoadData()
    {
        Repeater1.DataSource = General.MyRst("SELECT * from tblHotelsA");
        Repeater1.DataBind();

    }

So, in above example, I am loading up a "repeater" with some data.

However, REGARDLESS if you using a UP or not?

Your page load code, first time MUST quite much do the above.

If you don't do the above, then simple code that say loads up a dropdown list (combo box) will fire each and every time.

If then if you select a combo box value, then say click a button on the page?

the page load fires first, AND THEN your button code. But, if page load re-loads the combo box, then you lose your selection.

So, update panel or not?

Your page load event fires each and every time - including when you use that UP.

So, even without a UP, it stands to reason you can't really even build a working web page if you page load event EVERY time loads up data and controls, since then any changes to such controls will be "blowen" out, and over written each time.

So, while the UP results in what "appears" to not be a post-back?

it is in fact what we call a partial post-back. And the standard page life cycle does occur, but ONLY the bits and parts in the UP are posted back to the server, and in that case, ,then the code behind of course can only change/modify the controls inside of that UP (quite much the whole idea).

So, while the UP will "eliminate" the browser spinner, and while it KEEP your current scroll position on the page?

Make no mistake here - that partial post-back is in fact a page post-back, but just a smaller one, and one that looks to the user that no post-back occurred.

so, it is fine and dandy to have auto-post back for that dropdown list. (and you do NOT even require a trigger, and I suggest you remove the trigger, and put back in the auto post-back.

So, the UP does not "eliminate" the post-back, but hides this from the end user, and for the most part it can be rather nice, quick, and of course will not refresh the whole page. But, page load still fires each and every time.

So, for your code to work correctly, and only fire on the VERY first time, and what amounts to the "real" first page load?

Then your code has to be this:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            txtDate.Text = DateTime.Now.ToString("yyyy-MM-dd");
        }
    }

Now, if you really don't want any kind of post back?

Then you have to start writing some JavaScript, and start making ajax calls to some web methods.

Now, a ajax call certainly is "less" of a post-back then a UP, which in turn is less of a full page post-back.

If you absolute have a "poor" page design in which a UP can't be used, then yes, you have to start writing and adding client side js code, and thus you can setup a ajax call, and no page post-back will occur. it is quite rare that a UP can't be used, but ajax calls are better, but they also take significantly more time to setup, write and adopt.

However, keep in mind that a ajax call to server code does not have any use of controls on the page. So, you have to return values, and then have your js code update the controls on the page.

So, if a UP can suffice, then that's the least effort. The fantastic part is you don't have to write up and wire up a whole bunch of JavaScript code in place of using that UP.

For sure, a ajax call is best, but that takes more code, more time, and has more moving parts (and requires JavaScript skills on your part). Often this extra effort and time (cost) can't be justified to adopt ajax calls for the page, so a UP is a good middle ground. it is somewhat of a compromise compared to ajax calls, but it boatloads of less work, and thus often a great choice cost and time wise. In other words, in most cases a UP is "good enough".

Albert D. Kallal
  • 42,205
  • 3
  • 34
  • 51
  • I'm familiar with !isPostBack , but that is not my question. My question is why the code I presented (which is an accepted solution on Stack) is not working. – S Nash Feb 09 '23 at 13:03
  • 1
    As I explained, using a update panel does not prevent a post back, and using a update panel does not prevent page load from being triggered each and every time a post-back occurs, including when using a update panel. A UP does NOT prevent post-backs, it only creates a partial page post back (only that part of the UP is posted back to the server). However, that partial post-back still triggers the page load event each and every time - including when you use a UP and triggers . You have this "incorrect" idea that UP does not post-back Any trigger or button in UP will trigger page load each time. – Albert D. Kallal Feb 09 '23 at 14:56
  • 1
    So, I don't know what accepted solution you are talking about. But some people think that using a UP prevents a page post-back. This is 100% incorrect. The correct term is a "partial page" post back. And any button or triggers inside of a UP will cause a page life cycle to occur, including the page load event to fire every time. The page may well NOT look like a page life cycle is occurring, but it does!! So, UP's do case a post back - just not a full page post-back. So, as I noted, your page has to be designed BASED on this assumption. that assumption is page load should not matter. – Albert D. Kallal Feb 09 '23 at 15:01
  • 1
    So how to page load not matter? You put your code inside of of the !IsPostBack stub, so it only runs one time on the true first page load. After that, the page load event will not matter, since for every subsequent post-back, the code you have in the page load event will not matter. As a result, your design of your code thus means page load event will and does not matter. You have to assume that each and every button click that runs server side code (code behind) will ALSO trigger the page load event, including when UP's are used. If you really don't want a post-back, then use ajax calls. – Albert D. Kallal Feb 09 '23 at 15:16
  • 1
    In that accepted answer?User was "happy" with the results, and the page "looks" like it is not posting back, but that context is limited, since as I pointed out, the page in that answer really does STILL have a post-back, but not a full page post-back, and no where in that answer does anyone suggest that page load will not fire each time. So, yes, there is "some" misconceptions in that answer, since they leave out the details and explain above that I have provided here. In other words, because the browser "spinner" does not show, and scroll position don't change, that sufficient for most – Albert D. Kallal Feb 09 '23 at 15:20
  • So based on your answer page load will fire but can only affect items in that update panel since it is partial? – S Nash Feb 10 '23 at 12:56
  • Still one thing is not clear to me. As Said code I provided does not cause SelectedIndexChanged event to be fired. Why? so what that Trigger with EventName="SelectedIndexChanged" does at all? – S Nash Feb 10 '23 at 13:52
  • 1
    Well, the page load event only has this limitation when the post-back is due to a UP post-back, then yes, page load can only modify those controls. However, for any other post back (regular one), then all controls can be changed. As for using a trigger? You don't need nor want one if you have autopost-back for that drop down. And using a trigger does NOT change this, nor cause a autopostback when you use a trigger. Triggers are for (in most cases) to trigger OTHER UP panels to re-load also. You need both autopostback and a trigger to use a trigger. – Albert D. Kallal Feb 10 '23 at 15:29
  • 1
    You don't need nor want or even have to use a trigger for the SAME UP. However, even in that case you do use a trigger, you STILL ALWAYS need a autopostback = true for a dropdown. A trigger will not help nor change anything at all in this case anyway. If you have button in UP, then no trigger is required - button always does post-back. However, for dropdown, you still need autopostback=true, and a trigger does not help, nor change that no post back will occur unless you have autopost-back = true for the dropdown. But, as I stated, triggers are not for the SAME UP, so no need to use anyway. – Albert D. Kallal Feb 10 '23 at 15:32
  • 1
    Anything in UP that causes a post-back will then also fire the triggeres (not the other way around!!!!). No change in thinking how this works.. The rule is simply if you want a post-back in the UP, then you make sure the button or whatever (in this case dropdown) will cause a post-back. Triggers don't cause a post-back for the given UP, they are to be used to trigger OHTER UP's. So, if I have a button in UP-1, and I want to change some things in UP-2, then I put in a trigger in UP-1 to also load + fire UP-2. triggers are more of a "grouping" approach and not for causing any post backs – Albert D. Kallal Feb 10 '23 at 15:37
  • Can you edit your answer. it does not let me upvote unless question get edited. A small edit will be enough. – S Nash Feb 12 '23 at 19:04
  • 1
    How kind of you. I did edit the answer just now. – Albert D. Kallal Feb 12 '23 at 19:11