Disable ASP.Net DetailsView ButtonField based on condition using jQuery

Last Reply 4 months ago By pandeyism

Posted 4 months ago

Hi all

I am trying to disable a ButtonField in a DetailsView based upon the value of a cell:

However, the "btn" is null when using the FindControl command. The first check is fine ("IsLock").

<asp:ButtonField CommandName="Lock" Text="Lock Group" ButtonType="Button" ControlStyle-Width="100px" ControlStyle-CssClass="btn-main" ItemStyle-HorizontalAlign="Center" />

This is my code:

protected void dvLockUnlock_DataBound(object sender, EventArgs e)
{
    if (DataBinder.Eval(dvLockUnlock.DataItem, "IsLocked").ToString() == "True")
    {
        Button btn = dvLockUnlock.FindControl("Lock") as Button;
        btn.Enabled = false;
    }
}

Does anybody know how to do this?

Thanks

You are viewing reply posted by: pandeyism 4 months ago.
Posted 4 months ago Modified on 4 months ago

Hi Sanxion,

Please refer below sample.

HTML

<asp:DetailsView ID="dvCustomers" runat="server" AutoGenerateRows="false" DataKeyNames="CustomerId">
        <Fields>
            <asp:TemplateField ItemStyle-CssClass="hide" HeaderStyle-CssClass="hide">
                <ItemTemplate>
                    <asp:HiddenField runat="server" ID="hfId" Value='<%# Eval("CustomerId")%>' />
                </ItemTemplate>
            </asp:TemplateField>
            <asp:ButtonField CommandName="Lock" Text="Lock Group" ButtonType="Button" />
            <asp:BoundField DataField="Name" HeaderText="Name" />
        </Fields>
    </asp:DetailsView>
</div>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript">
    $(function () {
        var id = $("#dvCustomers").find("tr").eq(0).find("[id*=hfId]").val();
        if (id == "1") {
            $("#dvCustomers").find("tr").eq(1).find("input[type=button]").prop("disabled", true);
        }
    });
</script>
<style type="text/css">
    .hide
    {
        display: none;
    }
</style>

Namespaces

C#

using System.Data;
using System.Data.SqlClient;
using System.Configuration;

VB.Net

Imports System.Data.SqlClient
Imports System.Data

Code

C#

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString))
        {
            using (SqlCommand cmd = new SqlCommand("SELECT * FROM Customers", con))
            {
                using (SqlDataAdapter da = new SqlDataAdapter(cmd))
                {
                    DataTable dt = new DataTable();
                    da.Fill(dt);
                    this.dvCustomers.DataSource = dt;
                    this.dvCustomers.DataBind();
                }
            }
        }
    }
}

VB.Net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    If Not Me.IsPostBack Then
        Using con As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("constr").ConnectionString)
            Using cmd As SqlCommand = New SqlCommand("SELECT * FROM Customers", con)
                Using da As SqlDataAdapter = New SqlDataAdapter(cmd)
                    Dim dt As DataTable = New DataTable()
                    da.Fill(dt)
                    Me.dvCustomers.DataSource = dt
                    Me.dvCustomers.DataBind()
                End Using
            End Using
        End Using
    End If
End Sub

Screenshot