asp.net C # event onblur recharge full page

0

Good day, I have a .aspx page where, inside an asp: UpdatePanel, there is an asp: TextBox to which I assigned an event onblur, in the following way:

.aspx:

<asp:UpdatePanel ID="updatePanel1" runat="server" UpdateMode="Conditional">
  <ContentTemplate>
    <asp:TextBox ID="txt1" runat="server"></TextBox>
    <asp:Button ID="btn1OnBlur" runat="server" Visible="false" OnClick="btn1OnBlur_Click"></asp:Button>
  </ContentTemplate>
</asp:UpdatePanel>

.aspx.cs:

protected void Page_Load(object sender, EventArgs e) {
  //Asignar evento onblur
txt1.Attributes.Add("onblur",Page.ClientScript.GetPostBackEventReference(btn1,""));
}

protected void btn1OnBlur_Click(object sender,EventArgs e){
  if(txt1.Text.Equals("")){
    LlenarControles(); //...
  }
}

The event works fine, but the problem is that it reloads the entire page, and not just the UpdatePanel . If I have another function with another control and the same event is triggered, reload the page once again and delete the changes that the previous event had made ...

    
asked by Luis Pizarro Ramírez 29.09.2017 в 17:24
source

3 answers

1

It is not possible to see if you have the ScriptManager defined

<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>

On the other hand it may be the absence of the trigger, I would recommend doing this kind of work today with conventional ajax and not with Microsoft controls, but in case it is necessary to use it, this page clearly explains how you should do it.

    
answered by 29.09.2017 в 18:56
0

(In addition to adding the ScriptManager as mentioned by the user manu_rit in your answer) , you must declare in your UpdatePanel the element Triggers and in turn, the element AsyncPostBackTrigger .

Modifying your code, it would look like this:

<%-- Recuerda colocar la etiqueta ScriptManager: -- %>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="updatePanel1" runat="server" UpdateMode="Conditional">
  <ContentTemplate>
    <asp:TextBox ID="txt1" runat="server"></TextBox>
    <asp:Button ID="btn1OnBlur" runat="server" Visible="false" OnClick="btn1OnBlur_Click"></asp:Button>
  </ContentTemplate>
  <Triggers>
    <asp:AsyncPostBackTrigger ControlID="btn1OnBlur" EventName="Click" />
  </Triggers>
</asp:UpdatePanel>

Check this link to understand more about the use of UpdatePanel .

Understanding ASP.NET AJAX UpdatePanel Triggers

    
answered by 29.09.2017 в 19:59
0

The problem you may have is because you are not finding the control reference. Maybe with this small modification is the answer.

    txt1.Attributes.Add("onblur",Page.ClientScript.GetPostBackEventReference(btn1OnBlur,""));

I hope and it works. Greetings.

    
answered by 29.09.2017 в 20:06