0

我需要替换用 MaskedEditExtender 包装的 TextBox 中的“_”字符,以在编辑 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>

当我尝试编辑行时。文本框值为 "__12.4" 。在 GridView1_RowUpdating 我试图用“”替换“_”而没有结果。

        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();
    }

我的 TemplateField 看起来像这样

      <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>

我不知道在更新行后在哪里更新 TextBox1.text 值并将其发送到 MS SQL 数据库。

4

2 回答 2

1

我认为问题是当你选择行时,因为你选择了行,取文本框但从不改变值:

    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();
}

我认为是(见下面的代码)

    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();
}

但是这里只有你会替换字符并放入标签,它不会更新 texbox。

欢呼

于 2013-05-24T20:40:54.723 回答
1

如果你有一个数据源为你处理更新,你不需要在 rowupdating 中绑定你的gridview,你可以试试这个看看会发生什么?

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, "_", "");
}
于 2013-05-24T20:50:05.133 回答