laspalmos
laspalmos

Reputation: 149

Updating GridView row in edit mode from text box

I need to replace "_" character from my TextBox which is wraped with MaskedEditExtender to validate data entry when editing TextBox.

            <asp:MaskedEditExtender ID="TextBox1_MaskedEditExtender" runat="server"
                CultureAMPMPlaceholder="" CultureCurrencySymbolPlaceholder=""
                CultureDateFormat="" CultureDatePlaceholder="" CultureDecimalPlaceholder=""
                CultureThousandsPlaceholder="" CultureTimePlaceholder="" Enabled="True"
                InputDirection="RightToLeft" Mask="9999.9" MaskType="Number"
                TargetControlID="TextBox1" ClearTextOnInvalid="False>
            </asp:MaskedEditExtender>

When I try to edit row. Textbox value is "__12.4" . In GridView1_RowUpdating I am trying to replace "_" with "" with no results.

        protected void GridView1_RowUpdating(object sender, System.Web.UI.WebControls.GridViewUpdateEventArgs e)
    {

        TextBox Textbox1 = (TextBox)GridView1.Rows[e.RowIndex].FindControl("Textbox1 ");
        Label Label2 = new Label();
        Label2.Text = TextBox1.Text.Replace("_", "");
        GridView1.DataBind();
    }

My TemplateField looks like this

      <asp:TemplateField HeaderText="Decimal Value" SortExpression="DecimalValue">
        <EditItemTemplate>
            <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("DecimalValue","{0:F1}") %>' Height="20px" MaxLength="6" Width="40px"></asp:TextBox>
            <%-- Formated to display 9999.9 per requirement --%>
            <asp:MaskedEditExtender ID="TextBox1_MaskedEditExtender" runat="server"
                CultureAMPMPlaceholder="" CultureCurrencySymbolPlaceholder=""
                CultureDateFormat="" CultureDatePlaceholder="" CultureDecimalPlaceholder=""
                CultureThousandsPlaceholder="" CultureTimePlaceholder="" Enabled="True"
                InputDirection="RightToLeft" Mask="9999.9" MaskType="Number"
                TargetControlID="TextBox1" ClearTextOnInvalid="False">
            </asp:MaskedEditExtender>

        </EditItemTemplate>
        <ItemTemplate>
            <asp:Label ID="Label2" runat="server" Text='<%# Bind("DecimalValue", "{0:F1}") %>'></asp:Label>
        </ItemTemplate>
    </asp:TemplateField>

I don't know where to update TextBox1.text value after row is update and send this to MS SQL database.

Upvotes: 0

Views: 2473

Answers (2)

Wilfredo P
Wilfredo P

Reputation: 4076

I think the problem is when you select the row, becouse you select the row, take the textbox but never change the value:

    protected void GridView1_RowUpdating(object sender, System.Web.UI.WebControls.GridViewUpdateEventArgs e)
{

    TextBox Textbox1 = (TextBox)GridView1.Rows[e.RowIndex].FindControl("Textbox1 ");
    Label Label2 = new Label();
    Label2.Text = GridView1.Text.Replace("_", "");
    GridView1.DataBind();
}

I think is (see code below)

    protected void GridView1_RowUpdating(object sender, System.Web.UI.WebControls.GridViewUpdateEventArgs e)
{

    TextBox Textbox1 = (TextBox)GridView1.Rows[e.RowIndex].FindControl("Textbox1 ");
    Label Label2 = new Label();
    Label2.Text = Textbox1.Text.Replace("_", ""); //Replace Gridview for the text box
    GridView1.DataBind();
}

But here only you'll replace the Character and put in a Label, it won't be update the texbox.

Cheer

Upvotes: 1

Emre
Emre

Reputation: 573

you dont need to databind your gridview in rowupdating if you have a datasource handling updates for you, can you try this and see what happens?

protected void GridView1_RowUpdating(object sender, System.Web.UI.WebControls.GridViewUpdateEventArgs e)
{
    TextBox Textbox1 = (TextBox)GridView1.Rows[e.RowIndex].FindControl("Textbox1");
    Textbox1.Text = Regex.Replace(Textbox1.Text, "_", "");
}

Upvotes: 1

Related Questions