Ontextchanged called several times - autopostback

Asked

Viewed 897 times

1

I’ve got, like, 50 Textboxes with this guy:

<asp:TextBox ID="txt_DetImovel0" runat="server"  AutoPostBack="true" OnTextChanged="txt_DetImovel0_TextChanged"></asp:TextBox>
<asp:TextBox ID="txt_DetImovel1" runat="server"  AutoPostBack="true" OnTextChanged="txt_DetImovel0_TextChanged"></asp:TextBox>
<asp:TextBox ID="txt_DetImovel2" runat="server"  AutoPostBack="true" OnTextChanged="txt_DetImovel0_TextChanged"></asp:TextBox>

I have a javascript function that changes the Textbox value when it selects a color (Color Picker)

The problem is that in debug mode I realize that this function "txt_DetImovel0_TextChanged" is being called one for each field! same as the . js is only changing 1 field. (I put an Alert('test'); and it only really appears once, but when I am debugging I see that it is called several times.

Doubts:

Is there any way I know or monitor who is triggering this function? whereas in debug mode I only see the actions of code Behind.

How to do for only the field that has the value modified by . js actually call this function.?

NOTE: I found that when I have autopostback=true it will really send to each field, even if I only changed the value of 1 field!! This is a . NET BUG ?

the image below shows what cadas textbox does, it is the color selector, every time you change the color it gets the value.

Tela de demostração


Complement of codes to facilitate understanding. There is a step that it mounts some JS to be triggered as soon as it changes the value of Textbox:

txt.Append(@"<script type=text/javascript>");
txt.Append("Corescolhe(" + txt_DetImovel0.ClientID + ");");
txt.Append("Corescolhe(" + txt_DetImovel1.ClientID + ");");
txt.Append("Corescolhe(" + txt_DetImovel2.ClientID + ");");
txt.Append("Corescolhe(" + txt_DetImovel3.ClientID + ");");
txt.Append("Corescolhe(" + txt_DetImovel4.ClientID + ");");
txt.Append("Corescolhe(" + txt_DetImovel5.ClientID + ");");
txt.Append("Corescolhe(" + txt_DetImovel6.ClientID + ");");
txt.Append("Corescolhe(" + txt_DetImovel7.ClientID + ");");
txt.Append("Corescolhe(" + txt_DetImovel8.ClientID + ");");
txt.Append("Corescolhe(" + txt_DetImovel9.ClientID + ");");
txt.Append("</script>");

and the JS

function Corescolhe(elemento) {
    $(elemento).spectrum({
        showPaletteOnly: false,
        className: "full-spectrum",
        showInitial: true,
        chooseText: "OK",
        cancelText: "cancelar",
        showPalette: true,
        preferredFormat: "hex",
        localStorageKey: "spectrum.demo",
        move: function (color) {
            updateBorders(color);
        },
        hide: function (color) {
            updateBorders(color);
            //  alert('irá mudar');
            $(elemento).val(color);
        }
};

But the problem is that the JS really changes only the Textobox X but all Textbox trigger autopostBack at the same time, if I am debugging, I see that the function txt_DetImovel0_TextChanged is called once for each component and not for which it was just changed the value!

  • It would not be better to migrate to pure HTML and use ajax to control saves?

  • tried.. but did not give...rs...until pq the values, be this description, these colors, etc.. comes from the database.. is mounted dynamically. I tried to use javascript by manipulating postback to try to save only that element at a time, but it didn’t work very well.. thus working, but edited a color, it saves all again.. resource waste

1 answer

3


In Webforms, you have a form with several controls.

When you set the autopostback on one of the controls, it means that the entire form will be posted when one of the assimilated events occurs in the control.

This is not a bug, it is the default behavior of Webforms.

In your case, I suggest removing autopostback and events from all text boxes and adding a button at the end of the form to save. By clicking this button, all the form fields will be sent, but at once only.

  • is a good option, but that the form would be less intelligent than the current, would be, I tried to do with javascript the manipulation of the elements via postback and also did not work.

  • Dorathoto took better look at your question, and really is wrong. Only one POSTBACK should be executed for each modified textbox, not 50 postbacks for each change. Surely the error must be in your javascript, put a relevant snippet of the code that we will try to help you.

  • place where the code snippet in the comments, edit the question? or add an answer ?

  • Editing the question

Browser other questions tagged

You are not signed in. Login or sign up in order to post.