从文本框更新编辑模式下的GridView行

时间:2013-05-24 20:29:26

标签: c# asp.net

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

2 个答案:

答案 0 :(得分: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();
}

但是这里只有你将替换Character并放入Label,它不会更新texbox。

啦啦队

答案 1 :(得分: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, "_", "");
}