views:

6073

answers:

8

I have code in an Update Panel and even though on a button click i am inserting data into a db and simply calling Updatepanel.Update() the whole page is reloaded:

Gifts.ASPX

<table style="width:100%;">
            <tr>
                <td>
                    <asp:Label ID="Label2" runat="server" Text="Gift"></asp:Label>
                </td>
                <td>
<asp:UpdatePanel ID="UpdatePanel3" runat="server" UpdateMode="Conditional">
    <ContentTemplate>
                    <asp:TextBox ID="txtNewGift" runat="server"></asp:TextBox>
    </ContentTemplate>
</asp:UpdatePanel>
                </td>
            </tr>
            <tr>

Gifts.aspx.CS

protected void cmdAddGift_Click(object sender, EventArgs e)
{
    OleDbConnection objConn = new OleDbConnection(DataSource);

    Random r = new Random();
    int giftID = r.Next(1200, 14000);

    OleDbCommand objCommand = new OleDbCommand("Insert into Gifts (GiftID, Description) values (" + giftID + ",'" + txtNewGift.Text + "')", objConn);
    ExecuteCommand(objCommand);

    PopulateGifts(objConn);

    txtNewGift.Text = "";
    UpdatePanel3.Update();
}

Any ideas why this whole page would reload instead of just the textbox getting update?

+2  A: 

Where is the button on Gifts.ASPX? If you put the button inside the UpdatePanel or use triggers you don't need to call UpdatePanel3.Update(); from the code behind.

jesperlind
A: 

Are there any controls in other UpdatePanels or not in UpdatePanels that have a postback event pending?

tsilb
+1  A: 

Also, You need to have a ScriptManager object on your page. Do you have one?

bendewey
A: 

Where is the button in the above example? Inside or outside the UpdatePanel. If it is outside you will need to add it to the triggers collection of the UpdatePanel.

Also you only need to call UpdatePanel.Update() if you are changing the content of an UpdatePanel other than the one that caused the (Partial) postback.

As a side note (and personal crusade), it is recommended that a using statement is put around your DB connection.

With the markup below, the following will happen:

  • btnInnerPart is inside the update panel, so it will automatically cause a partial postback
  • btnInnerFull will cause a full postback as it has a PostBackTrigger in the trigger collection
  • btnOuterPart will cause a partial postback as it has an AsyncPostBackTrigger in the trigger collection
  • btnOuterFull will cause a full postback as it is outside the UpdatePanel

Markup:

<asp:UpdatePanel runat="server">
    <ContentTemplate>
        <!-- Content -->
        <asp:Button runat="server" ID="btnInnerPart" Text="Inner Part" />
        <asp:Button runat="server" ID="btnInnerFull" Text="Inner Full" />
    </ContentTemplate>
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="btnOuterPart" />
        <asp:PostBackTrigger ControlID="btnInnerFull" />
    </Triggers>
</asp:UpdatePanel>
<asp:Button runat="server" ID="btnOuterFull" Text="Outer Full" />
<asp:Button runat="server" ID="btnOuterPart" Text="Outer Part" />
Robert Wagner
A: 

please check tag of update panel...you have to specify the trigger controls for update panel on on which the update panel will get update

A: 

isn't using the trigger in aspx just a markup for Update() method in behind code? if not, show msdn-reference stating otherwise.

stse
A: 
        UpdateMode = UpdatePanelUpdateMode.Conditional;
asdf
A: 

Not able to update the label in the update panel. Below is the code:

public class OnJobCompleteEventArgs : EventArgs { public string JobName { get; set; } public string Message { get; set; }

    public OnJobCompleteEventArgs(string jobName)
    {
        this.JobName = jobName;
        this.Message = string.Format("Job {0} completed at {1}", jobName, DateTime.Now);
    }
}

public class Job
{
    public delegate void JobCompleted(object sender, OnJobCompleteEventArgs e);
    public event JobCompleted OnJobComplete;

    public void StartJob()
    {
        //Get Job
        //Run Job
        //job is complete
        ThreadStart sleep = new ThreadStart(RunJob);
        Thread t = new Thread(sleep);
        t.Start();            
    }

    public void RunJob()
    {
        Thread.Sleep(2000);

        if (OnJobComplete != null)
            OnJobComplete(this, new OnJobCompleteEventArgs("ExportBill"));
    }
}

public partial class _Default : System.Web.UI.Page
{
    protected void Button1_Click(object sender, EventArgs e)
    {
        Job myJob = new Job();
        myJob.OnJobComplete +=new Job.JobCompleted(myJob_OnJobComplete);
        myJob.StartJob();           
    }

    public void myJob_OnJobComplete(object sender, OnJobCompleteEventArgs e)
    {
        Label1.Text += e.JobName + ": " + e.Message;
        UpdatePanel1.Update();
    }


}
Dhawal