Question

I have a page with a gridview (actually two, but they are identical and the same thing happens with either).

New rows are added to the gridview from the footerrow via a link button.

When using the page to edit existing data, a strange thing happens. When I create the page in edit mode (i.e. to add/remove rows from an existing set of data in the table) the first time I try to add another row to the gridview, the IsPostBack is set to false and thus the gridviews re-initialise / rebind and my change is lost. The second and further time that I try to add rows, it works fine as the PostBack is recognised.

What could be causing the PostBack not to be recognised as one?

I'm not sure it's worth posting code but I will post my Page_Load for what it is worth:

querystring: a=n is "new" (for creating a page to add a new set of records) a=e is "edit" (populate page with existing data to allow for adding / removing rows.

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            String action = Request.QueryString["a"];
            if (action == "n")
            {
                BindBuyGrid("create");
                BindSellGrid("create");
            }
            else if(action == "e")
            {
                PopulatePage();
            }
        }
    }

Update 1

protected void gvwBuyConditions_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "InsertNew")
        {
            AddNewRowToBuyConditionsGrid();
        }
    }

Update 2 The link button code. Surely this should trigger a postback?

<FooterTemplate>
    <asp:LinkButton
        ID="lnkInsert"
        runat="server"
        CommandName="InsertNew"
        ToolTip="Add New Entry to List" >
        <img src="../Images/Add.ico" height="20" width="20"/></asp:LinkButton>
</FooterTemplate>

Update 3 The AddNewRowToBuyConditionsGrid code:

 private void AddNewRowToBuyConditionsGrid()
    {
        if (ViewState["BuyConditions"] != null)
        {
            DataTable dtCurrentTable = (DataTable)ViewState["BuyConditions"];
            DataRow drCurrentRow;

            GridViewRow row = gvwBuyConditions.FooterRow;
            DropDownList ddlSelectedCondition = row.FindControl("ddlConditionNew") as DropDownList;

            try
            {
                if (ddlSelectedCondition.Text != "Select a Buy Condition")
                {
                    drCurrentRow = dtCurrentTable.NewRow();
                    drCurrentRow["ConditionName"] = ddlSelectedCondition.SelectedItem.Text;
                    drCurrentRow["ConditionID"] = Convert.ToInt32(ddlSelectedCondition.SelectedItem.Value);
                    dtCurrentTable.Rows.Add(drCurrentRow);
                    drCurrentRow = null;
                }
                // take out the dummy "Select a Condition"
                DataRow drtodie = dtCurrentTable.Rows.Find(-1);
                if (drtodie != null)
                {
                    dtCurrentTable.Rows.Find(-1).Delete();
                }

                //Store the current data to ViewState
                ViewState["BuyConditions"] = dtCurrentTable;

                //Rebind the Grid with the current data
                gvwBuyConditions.DataSource = dtCurrentTable;
                gvwBuyConditions.DataBind();

            }
            catch (Exception)
            {

                if (ddlSelectedCondition.SelectedItem.Value == String.Empty)
                {
                    lblBuyMessage.Text = "Please select a Buy Condition.";
                }
                else
                {
                    lblBuyMessage.Text = "That condition already exists in the list. Please select another condition.";
                }
            }
        }
        else
        {
            Response.Write("ViewState is null");
        }
    }

Update 4 Code on previous page that calls this page. e is just configured in string. Is it maybe the Server.Transfer?

 protected void gvwStrategyList_RowCommand(Object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "UseEditForm")
    {
        //row index
        int index = Convert.ToInt32(e.CommandArgument);

        //retrieve StrategyID  
        int StrategyID = Convert.ToInt32(gvwStrategyList.DataKeys[index].Value);

        Server.Transfer("~/AddStrategy.aspx?a=e&z=" + StrategyID, true);
    }
}

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top