【问题标题】:disable textbox in edit mode在编辑模式下禁用文本框
【发布时间】:2019-02-27 18:24:37
【问题描述】:

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

我在编辑模板中没有产品图片,而且产品图片也不包含在我的代码中。无法理解为什么当我单击更新功能时产品图像上会出现文本框。请帮忙。

【问题讨论】:

  • 您的问题似乎是什么?只需在更新时将文本框可见性设置为 false
  • 先生,我没有在产品图像数据上声明任何文本框。
  • 您确实在代码中声明了一些变量。

标签: c# asp.net gridview


【解决方案1】:

如果 (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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-10-10
    • 1970-01-01
    • 1970-01-01
    • 2012-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-10
    相关资源
    最近更新 更多