在编辑模式下禁用文本框

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在编辑模式下禁用文本框相关的知识,希望对你有一定的参考价值。

我在gridview中有一个编辑功能。但是我想要做的是当我点击特定数据绑定的更新功能时禁用文本框的可见性。这是我的aspx代码:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ProductID" 
OnPageIndexChanging="GridView1_PageIndexChanging" OnRowCancelingEdit="GridView1_RowCancelingEdit" 
OnRowDeleting="GridView1_RowDeleting" OnRowEditing="GridView1_RowEditing" 
        OnRowUpdating="GridView1_RowUpdating">
<Columns>
    <asp:TemplateField HeaderText="ProductName">
        <ItemTemplate>
            <asp:Label ID="Label1" runat="server" Text='<%# Bind("ProductName") %>'></asp:Label>
        </ItemTemplate>
        <EditItemTemplate>
            <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("ProductName") %>'></asp:TextBox>
            <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"
             ErrorMessage="Invalid" ForeColor="Red" ControlToValidate="txtboxProductName"
            ValidationExpression="^[a-zA-Z ]+$"></asp:RegularExpressionValidator>
        </EditItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="ProductDescription">
        <ItemTemplate>
            <asp:Label ID="Label2" runat="server" Text='<%# Bind("ProductDescription") %>'></asp:Label>
        </ItemTemplate>
        <EditItemTemplate>
            <asp:TextBox ID="txtboxProductDescription" runat="server" Text='<%# Bind("ProductDescription") %>'></asp:TextBox>
            <asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server" ErrorMessage="Invalid" 
            ForeColor="Red" ControlToValidate="txtboxProductDescription"
            ValidationExpression="^[a-zA-Z ]+$"></asp:RegularExpressionValidator>
        </EditItemTemplate>
    </asp:TemplateField>
    <asp:ImageField HeaderText ="ProductImage" DataImageUrlField="ProductImage" SortExpression="ProductImage" ControlStyle-Width ="10">

        <ControlStyle Width="50px"></ControlStyle>

        </asp:ImageField>
    <asp:TemplateField HeaderText="ProductQuantity">
        <ItemTemplate>
            <asp:Label ID="Label3" runat="server" Text='<%# Bind("ProductQuantity") %>'></asp:Label>
        </ItemTemplate>
        <EditItemTemplate>
            <asp:TextBox ID="txtboxProductQuant" runat="server" Text='<%# Bind("ProductQuantity") %>'></asp:TextBox>
            <asp:RegularExpressionValidator ID="RegularExpressionValidator3" runat="server" ErrorMessage="Invalid" ControlToValidate="txtboxProductQuant"
            ForeColor="Red" ValidationExpression=^[0-9]*$></asp:RegularExpressionValidator>
        </EditItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="ProductPrice">
        <ItemTemplate>
            <asp:Label ID="Label4" runat="server" Text='<%# Bind("ProductPrice") %>'></asp:Label>
        </ItemTemplate>
        <EditItemTemplate>
            <asp:TextBox ID="txtboxProductPrice" runat="server" Text='<%# Bind("ProductPrice") %>'></asp:TextBox>
            <asp:RegularExpressionValidator ID="RegularExpressionValidator4" runat="server" ErrorMessage="Invalid" ControlToValidate="txtboxProductPrice"
            ForeColor="Red" ValidationExpression=^[0-9]*$></asp:RegularExpressionValidator>
        </EditItemTemplate>
    </asp:TemplateField>
    <asp:CommandField ShowEditButton="true" />
    <%--<asp:CommandField ShowDeleteButton="true" />--%>
    <asp:TemplateField>
        <ItemTemplate>
        <asp:LinkButton ID="lnkdel" runat="server" Text="Delete" CommandName="Delete" 
        OnClientClick="return confirm('Confirm Delete?');"></asp:LinkButton>

        </ItemTemplate>
        <ItemStyle Width="100px" />

        </asp:TemplateField>
    </Columns>
</asp:GridView>

这是我的更新代码:

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        //Finding the controls from Gridview for the row which is going to update  
        //Label id = GridView1.Rows[e.RowIndex].FindControl("lbl_ID") as Label;
        int userid = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString());
        TextBox ProductName = GridView1.Rows[e.RowIndex].FindControl("txtboxProductName") as TextBox;
        TextBox ProductDescription = GridView1.Rows[e.RowIndex].FindControl("txtboxProductDescription") as TextBox;
        TextBox ProductQuantity = GridView1.Rows[e.RowIndex].FindControl("txtboxProductQuant") as TextBox;
        TextBox ProductPrice = GridView1.Rows[e.RowIndex].FindControl("txtboxProductPrice") as TextBox;
        conn = new SqlConnection("Data Source = 'PAULO'; Initial Catalog=Authorship;Integrated Security =True");
        conn.Open();
        //updating the record  
        SqlCommand cmd = new SqlCommand("Update Products set ProductName='" + ProductName.Text + "',ProductDescription='" + ProductDescription.Text + "',ProductQuantity='" + ProductQuantity.Text + "', ProductPrice='" + ProductPrice.Text + "' where ProductID='" + userid + "'", conn);
        cmd.ExecuteNonQuery();
        conn.Close();
        //Setting the EditIndex property to -1 to cancel the Edit mode in Gridview  
        GridView1.EditIndex = -1;
        //Call ShowData method for displaying updated data  
        gvbind(); 
}

我没有在编辑模板中的产品图像,并且产品图像不包含在我的代码后面。无法理解为什么当我点击更新功能时文本框在产品图像上变得可见。请帮忙。

答案

如果(e.Row.RowState和DataControlRowState.Edit)> 0那么

例如Protected Sub GridView1_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles GridView1.RowDataBound If e.Row.RowState = DataControlRowState.Edit Then ctype(e.Row.Cells(1).Controls(0),TextBox).Enabled=False End If End Sub

以上是关于在编辑模式下禁用文本框的主要内容,如果未能解决你的问题,请参考以下文章

在编辑模式下显示带有编辑按钮的 mvc html 表和数据作为标签,同时显示和数据作为文本框

如何取消编辑/禁用缓存输入(文本类型)值?

几种设置表单元素中文本输入框不可编辑的方法

26个jQuery代码片段使用技巧

如何启用禁用的文本框 onclick 使用角度的按钮

WIN10下命令行禁用编辑模式