We help IT Professionals succeed at work.

"next" button using DataView

uconnfb13 asked

I have recently creating certain aspx pages that i refer to as "form screen".  These contain the specific information of a given record in a database table.  I would like there to be a button on these form screens that will navigate to the next record in the given table.  As my code currently stands, when I click on the next button It will move to the next record only on the first click.  Every click thereafter does nothing.  

I have attached my current code.  On the page load I create the dataview and use a record that has the same row_id as a session variable declared on a separate page.  The next_record method is where I would like to navigate to the next record.  I have tried to look at the same dataview, find the same record, add one to the previous index, then set my text fields on my aspx page equal to the information from that new index's corresponding record.

Thanks a lot!

public partial class school_form_2 : System.Web.UI.Page
    protected void Page_Load(object sender, EventArgs e)
        string row_id = (string)(Session["LastSchoolID"]);
        school_id.Text = row_id;
        DataView School = (DataView)SchoolInfo.Select(DataSourceSelectArguments.Empty);

        School.Sort = "ROW_ID";
        int foundIndex = School.Find(row_id);
        Session["last_school"] = foundIndex;

            school_id.Text = School[foundIndex]["row_id"].ToString();
            school_name.Text = School[foundIndex]["name"].ToString();
            division.Text = School[foundIndex]["Division_ID"].ToString();
            email.Text = School[foundIndex]["email"].ToString();
            fax.Text = School[foundIndex]["fax"].ToString();
            phone1.Text = School[foundIndex]["phone_number_1"].ToString();
            phone2.Text = School[foundIndex]["phone_number_2"].ToString();
            ext1.Text = School[foundIndex]["ext_1"].ToString();
            ext2.Text = School[foundIndex]["ext_2"].ToString();
            address1.Text = School[foundIndex]["address_1"].ToString();
            address2.Text = School[foundIndex]["address_2"].ToString();
            city.Text = School[foundIndex]["city"].ToString();
            zip.Text = School[foundIndex]["zip"].ToString();
            zip4.Text = School[foundIndex]["zip_four"].ToString();
            StateDropDown.SelectedValue = School[foundIndex]["state"].ToString();
           // School.Dispose();

    protected void next_record (object sender,EventArgs e)
        DataView School = (DataView)SchoolInfo.Select(DataSourceSelectArguments.Empty);
        School.Sort = "ROW_ID";
        int foundIndex = School.Find(school_id.Text);
       int new_index = foundIndex + 1;
            school_id.Text = School[new_index]["row_id"].ToString();
            school_name.Text = School[new_index]["name"].ToString();
            division.Text = School[new_index]["Division_ID"].ToString();
            email.Text = School[new_index]["email"].ToString();
            fax.Text = School[new_index]["fax"].ToString();
            phone1.Text = School[new_index]["phone_number_1"].ToString();
            phone2.Text = School[new_index]["phone_number_2"].ToString();
            ext1.Text = School[new_index]["ext_1"].ToString();
            ext2.Text = School[new_index]["ext_2"].ToString();
            address1.Text = School[new_index]["address_1"].ToString();
            address2.Text = School[new_index]["address_2"].ToString();
            city.Text = School[new_index]["city"].ToString();
            zip.Text = School[new_index]["zip"].ToString();
            zip4.Text = School[new_index]["zip_four"].ToString();
            StateDropDown.SelectedValue = School[new_index]["state"].ToString();


Open in new window

Watch Question

Most Valuable Expert 2012
Top Expert 2008

The usual problem with data-binding in the Page_Load, is that you need to take into account when the page posts back, and handle that differently.  It looks like you are using an SqlDataSource, and then trying to circumvent what it normally handles just fine (i.e. session parameter).


Thank you for your comment TheLearnedOne.  If i have my database table accessible do you have a reccomended procedure for accomplishing something along those lines.  A high level response is all that I need, would be very helpful and appreciated.

Thanks a lot!
Most Valuable Expert 2012
Top Expert 2008
          SelectCommand="SELECT OrderId, CustomerId, OrderDate
                         FROM Orders
                         WHERE EmployeeID = ?
                         ORDER BY CustomerId ASC;">
                DefaultValue="5" />

Open in new window


thank you