leoinlios
leoinlios

Reputation: 831

changing textbox value in code behind does not post back new value

I have a gridview (ASP.NET Web Form project) with 4 columns in it: A CommandField for editing of the rows, and 3 TemplateField>EditItemTemplate>Textbox columns called Total_Plan, Mth_1 and Mth_2. The 3 textbox columns are bound to an SqlDataSource.

Total_Plan is defined as:

              <asp:TemplateField HeaderText="Total Plan">
                    <EditItemTemplate>
                        <asp:TextBox ID="txbTotalPlan" runat="server" Text='<%# Bind("Total_Plan") %>'>                                                        
                        </asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="idNotUsedButNeeded0" runat="server" Text='<%# Bind("Total_Plan") %>' />
                    </ItemTemplate>
                </asp:TemplateField>

When the user presses Edit, changes the values of Mth_1 or 2, then presses Update, the following code runs:

protected void gridDetail_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
    double totPlan =0;
    totPlan += double.Parse(((TextBox)gridDetail.Rows[e.RowIndex].FindControl("txbMth_1")).Text);
    totPlan += double.Parse(((TextBox)gridDetail.Rows[e.RowIndex].FindControl("txbMth_2")).Text);
    ((TextBox)gridDetail.Rows[e.RowIndex].FindControl("txbTotalPlan")).Text = totPlan.ToString();
}

The values entered in Mth_1 and Mth_2 are then updated to to SQL, however, the value of Total_Plan does NOT get updated in the database. The line of code

((TextBox)gridDetail.Rows[e.RowIndex].FindControl("txbTotalPlan")).Text = totPlan.ToString();

does change the value of the textbox, because when I check ((TextBox)gridDetail.Rows[e.RowIndex].FindControl("txbTotalPlan")).Text I can see the NEW value assigned, however this assignment does not seem to be posted back to the server and so upon Updating the SQL table, the old value is preserved (I inspected the e.Command.Parameters[0].Value of the SqlDataSource_Updating event and indeed, the assignment I do programmatically to the textbox is not picked up when doing the Update to SQL.

I tried adding AutoPostBack=true on the txbTotalPlan but this did not work.

Why if I am doing an assignment, server side, to a textbox in a row of a grid, at the time the update is taking place (i.e. not After), the new value is not used for the Update operation? Am I doing this assignment too late? The alternative I thought was to make the assigmment to the Total Plan column in the OnTextChanged event of each Mth_1 and 2, but if I can write the assignment in a central place and only at a time before updating, it seems less messy and less frequent.

I put a stop in the protected void Page_Load(object sender, EventArgs e) event and indeed at the time of making the assigmment to the txbTotalPlan textbox, this method is not called.

Thank you for your help.

Upvotes: 0

Views: 1945

Answers (2)

leoinlios
leoinlios

Reputation: 831

I still dont know about why this doesnt work in the grid's RowUpdating event, but the alternative solution I used was to do the assignment by subscribing to the OnUpdating event of the SqlDataSource and assigning as shown below.

protected void SqlData_Updating(object sender, SqlDataSourceCommandEventArgs e)
        {            
            //calculate Total Plan (sum of all mth columns)           
            double mth_AddedUp = 0;
            foreach(System.Data.Common.DbParameter p in e.Command.Parameters)
                if(p.ParameterName.Length > 5 &&
                    p.ParameterName.Substring(0,5)=="@Mth_" && 
                    p.Value != null)
                    mth_AddedUp += double.Parse(p.Value.ToString());                           
            // set Plan parameter value
            e.Command.Parameters["@Plan"].Value = (mth_AddedUp).ToString();           

        }

Upvotes: 1

afzalulh
afzalulh

Reputation: 7943

You are updating data using SqlDataSource. You need to update it's parameter rather than the textbox value. Do like this:

protected void gridDetail_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
    double mth1=0;
    double mth2=0;
    mth1 = double.Parse(((TextBox)gridDetail.Rows[e.RowIndex].FindControl("txbMth_1")).Text);
    mth2 = double.Parse(((TextBox)gridDetail.Rows[e.RowIndex].FindControl("txbMth_2")).Text);
    yourDataSource.UpdateParameters["Mth_1"].DefaultValue = mth1;
    yourDataSource.UpdateParameters["Mth_2"].DefaultValue = mth2;
    yourDataSource.UpdateParameters["Total_Plan"].DefaultValue = mth1 + mth2;
    yourDataSource.Update();//yourDataSource is your SqlDataSource
}

Upvotes: 0

Related Questions