Loading ...

How to disable button in gridview based on condition | CodeAsp.Net

How to disable button in gridview based on condition

(9271)
0
/5
Avg: 0/5: (0 votes)
Published: 10/4/2009 by Raghav Khunger

In this article i will discuss how to disable a button in gridview based on condition.I have used RowDataBound event of Gridview.Below i have bind the grid with records in my table , for the grid i have a condition that if the url is empty we have to disable the button  that is present for each row.Below is the complete code:

 

 

 

 

 

In Sql

 

CREATE TABLE [Test_Grid]

([ID] INT IDENTITY ,[NAME] VARCHAR(20) ,[EMAIL] VARCHAR(20),[URL] VARCHAR(50))

GO

INSERT INTO [Test_Grid]

SELECT 'NAME1','EMAIL1','http://test123.com'

UNION ALL

SELECT 'NAME1','EMAIL1','http://test1234.com'

UNION ALL

SELECT 'NAME1','EMAIL1','http://test12345.com'

UNION ALL

SELECT 'NAME1','EMAIL1',''

UNION ALL

SELECT 'NAME1','EMAIL1','http://test1234567.com'

UNION ALL

SELECT 'NAME1','EMAIL1',''

GO

 

In Aspx

 

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

<title></title>

</head>

<body>

<form id="form1" runat="server">

<div>

<asp:GridView ID="GridView1" runat="server" BackColor="White" BorderColor="#3366CC"

BorderStyle="solid" BorderWidth="1px" CellPadding="4" AutoGenerateColumns="false"

OnRowDataBound="GridView1_RowDataBound">

<FooterStyle BackColor="#99CCCC" ForeColor="#003399" />

<RowStyle BackColor="White" ForeColor="#003399" />

<SelectedRowStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" />

<PagerStyle BackColor="#99CCCC" ForeColor="#003399" />

<HeaderStyle BackColor="#003399" Font-Bold="True" ForeColor="#CCCCFF" />

<Columns>

<asp:TemplateField HeaderText="Id">

<ItemTemplate>

<asp:Literal ID="litId" runat="server" Text='<%# Eval("ID")%>'> </asp:Literal>

</ItemTemplate>

</asp:TemplateField>

<asp:TemplateField HeaderText="Name">

<ItemTemplate>

<asp:Literal ID="litName" runat="server" Text='<%# Eval("NAME")%>'> </asp:Literal>

</ItemTemplate>

</asp:TemplateField>

<asp:TemplateField HeaderText="Email">

<ItemTemplate>

<asp:Literal ID="litEmail" runat="server" Text='<%# Eval("EMAIL")%>'> </asp:Literal>

</ItemTemplate>

</asp:TemplateField>

<asp:TemplateField HeaderText="URL">

<ItemTemplate>

<asp:Literal ID="litUrl" runat="server" Text='<%# Eval("URL")%>'> </asp:Literal>

</ItemTemplate>

</asp:TemplateField>

<asp:TemplateField HeaderText="Download">

<ItemTemplate>

<asp:Button ID="btnDownload" runat="server" Text="Download" />

</ItemTemplate>

</asp:TemplateField>

</Columns>

</asp:GridView>

</div>

</form>

</body>

</html>

 

 

In Aspx.cs(Codebehind)

 

using System;

using System.Data;

using System.Data.SqlClient;

using System.Web.UI.WebControls;

 

namespace TestProject

{

public partial class _Default : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{


using (SqlConnection connection = new SqlConnection("xxxxxxxxxx—-Your Connection String—xxxxx"))

{

using (SqlCommand command = new SqlCommand("SELECT [ID],[NAME],[EMAIL],[URL] FROM [Test_Grid]", connection))

{

connection.Open();

using (SqlDataReader dataReader = command.ExecuteReader(CommandBehavior.CloseConnection))

{

if (dataReader != null)

if (dataReader.HasRows)

{

GridView1.DataSource = dataReader;

GridView1.DataBind();

}

}

}

}

}

 

protected void GridView1_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)

{

if (e.Row.RowType == DataControlRowType.DataRow)

{

Literal literalUrl = e.Row.FindControl("litUrl") as Literal;

 

if (literalUrl != null)

{

string url = literalUrl.Text;

if (string.IsNullOrEmpty(url))

{

Button btnDownload = e.Row.FindControl("btnDownload") as Button;

if (btnDownload != null) btnDownload.Enabled = false;

}

}

//

}

 

}

}

}

 

 

So above we discuss how to disable button based on condition.We can apply the same logic to enable or disable other controls.

Do let me know your feedback,comments.

 

Comments (3)

raghav_khunger
Do let me know your feedback,comments.
10/12/2009
 · 
 
by
 said:
Good, simple to understand article. Cheers
11/4/2009
 · 
 
by
sanjayjgoswami

Hi..

We can also achive on ASPX Side.

<asp:Button  ID="btnDownload" Enabled='<%# (Convert.ToString(Eval("URL")) == "")?true:false %>'  runat="server"  Text="Download"  />

 

Might be this one is fast coz it reduce "If" Condition, Null or Empty check also not require coz null is convert in to Empty (Convert.ToString()).

 

:)

8/17/2010
 · 
 
by

Top articles

Quick Vote

What kind of email newsletter would you prefer to receive from CodeAsp.Net?