0

I have this button event:

protected void AddDetails_Click(object sender, EventArgs e)
    {


        DataSetTableAdapters.SelectFriendsTableAdapter sft = new DataSetTableAdapters.SelectFriendsTableAdapter();
        try
        {
            sft.AddFriend(current, newFriend, false);
        }
        catch
        {
            error.Text = "Something happened. Bummer!";
        }

    }

in the try section, I'm adding entries in the Database. In the page there are Labels / Textboxes with the corresponding values.

Everything works fine. However, I need to refresh the page in order to see the changes after I click on this submit button.

I have added if(!IsPostBack) at the beginning of the PageLoad code, but I still need to visit another page / come back to see the changes.

Any ideas?

Thanks.


Thank you for your replies. I'm using a ListView:

protected void Page_Load(object sender, EventArgs e)
    {
     if (!IsPostBack)
        {
            DataSetTableAdapters.SelectFriendsTableAdapter sdt = new DataSetTableAdapters.SelectFriendsTableAdapter();
            DataTable tab = sdt.SelectFriends();
            ListView1.DataSource = tab;
            ListView1.DataBind();

        }
    }

, so the ListView content should get updated.

Brad Larson
  • 170,088
  • 45
  • 397
  • 571
Tudor Gafiuc
  • 145
  • 1
  • 2
  • 8
  • What should change exactly? Values in the textbox? Do you have a grid that you want to refresh? – Ben Narube Jun 16 '13 at 22:56
  • What binds the values to the labels/textboxes on the page? Also, if(!IsPostBack) will only execute the logic once, as the initial page load is the only time it is NOT a postback, subsequent requests to the page without navigating elsewhere are considered postbacks. What you want is to trigger the mechanism that binds the values to the controls (labels/textboxes) on the page once you successfully complete a database update (the try logic in your posted code). – Karl Anderson Jun 17 '13 at 01:48

2 Answers2

1

If you don't want to refresh the page and you're using asp.net webforms you can use an UpdatePanel and place all your controls inside of it, etc... That will keep the changes made in the form / display when you submit the info. I'm assuming the form is on submitting the changed/new data as you didn't specifically state how the data was being updated/changed.

If you don't want to use an update panel, then when the page is posting back you will have to set the values for the UI controls that you want to update with the values from the 'newFriend' object (I'm assuming that has the changed values).

bbqchickenrobot
  • 3,592
  • 3
  • 45
  • 67
-1

Found it!

The solution for this issue is to add this code at the end of your submit button click event:

Server.Transfer("currentpage.aspx");
Tudor Gafiuc
  • 145
  • 1
  • 2
  • 8
  • That is the wrong way to do it - you're executing the page twice when you do that. IT IS absolutely NOT the correct way to do it. Just cause you can drive a car with your feet doesn't mean you should ;) – bbqchickenrobot Jun 19 '13 at 00:50