0

I'm having some issues with posting back a RowCommand from a GridView. Not sure if my logic is correct so if somebody could point out where I am going wrong that would be great.

It seems there are lot of similar problems but none of the solutions have a default set of results in the gridview then rebinded with search results like this scenario.

The problem is when the RowCommand is fired I have the wrong result. On default load the button works correctly but if I search for customers and then use the RowCommand, the page posts back and rebinds the grid with the default customers always sending me to the wrong customer.

  1. Page Load: Fill GridView with logged in users default clients
  2. Search Box: Search companies entire client list and repopulate gridview
  3. RowCommand: Send users to the customer

Postback:

                if(!IsPostBack)
        {
            //Check if user logged in
            User A_User = new User();

            if(!A_User.Check_Logged_In())
            {
                if(A_User.Should_Redirect(System.IO.Path.GetFileName(Request.PhysicalPath)))
                {
                    //Redirect user to login page
                    Response.Redirect(A_User.Login_Page());
                }
            }

            //Modify nav buttons
            HtmlGenericControl nav = (HtmlGenericControl)this.Page.Master.FindControl("UserPanel").FindControl("li_nav_address_book");

            nav.Attributes["class"] = "active";

            //Load logged in users customers
            BindGrid(false);

        }else
        {
            //Check for request
            if(Request.Params["__EVENTTARGET"] != null)
            {
                //Check for search string
                if(Request.Params["__EVENTTARGET"].ToString().Contains("SearchCustomers"))
                {
                    //Load customers by search results
                    BindGrid(true);

                }
                //else
                //{

                //    if(Request.Params["__EVENTTARGET"].ToString().Contains("btn2"))
                //    {

                //        Console.WriteLine("SENDER: ", "btn2 RowCommand");
                //        BindGrid(true);
                //    }
                //}

            }

        }

Search Button:

    protected void btn_Search_Click(object sender, EventArgs e)
    {
        BindGrid(true);

    }

Grid Binding:

    private void BindGrid(bool Search)
    {

        if(!Search)
        {
            //Load customers by rep ID
            Contacts Contact_Manager = new Contacts();

            gvCustomers.DataSource = null;

            DataSet dsCustomers = Contact_Manager.Get_Customers_By_UserID((int)Session["User_ID"]);
            DataTable tblCustomers = dsCustomers.Tables[0];

            gvCustomers.DataSource = tblCustomers;
            gvCustomers.DataBind();
        }
        else
        {
            //Load customers by search terms
            Contacts Contact_Manager = new Contacts();

            //Search by replacing spaces to do a rainbow database search as whole text instead of tags
            DataSet dsCustomers = Contact_Manager.Get_Customers_By_Tags(tb_GetContacts.Text.Replace(" ", ""));
            DataTable tblCustomers = dsCustomers.Tables[0];

            gvCustomers.DataSource = null;

            gvCustomers.DataSource = tblCustomers;
            gvCustomers.DataBind();
        }

    }

RowCommand:

    protected void gvCustomers_RowCommand(object sender,
        GridViewCommandEventArgs e)
    {
        if(e.CommandName == "Customer_Detail")
        {

            int Customer_ID = Convert.ToInt32(e.CommandArgument);

            Response.Redirect("~/Customer/" + Customer_ID);

        }

    }
ScottC
  • 162
  • 1
  • 14
  • What is the problem here? What do you expect us to do? – Jakub Szumiato Nov 14 '16 at 11:20
  • Would help if I added what the problem is! I've updated the question. – ScottC Nov 14 '16 at 11:23
  • And when debugging, are you entering the `!IsPostBack` part of you `Page_Load` event before your `gvCustomers_RowCommand` is executed? – Jakub Szumiato Nov 14 '16 at 11:59
  • I have an if/else statement and it enters else. – ScottC Nov 14 '16 at 12:59
  • Do you enter then the `BindGrid(true)` statement from else? To me it looks like the grid is unnecessarily bound again to the data without the search criteria being applied. A bit of debugging would be necessary to see how it enters different DataBinds while it runs. – Jakub Szumiato Nov 14 '16 at 13:53
  • I first bind on page load with all customers related to X. User can then search and I re-bind with all customers related to Y. It's had me stumped for a couple of days. – ScottC Nov 14 '16 at 13:58

0 Answers0