Click here to Skip to main content
15,936,876 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:

How to check the boolean value through Eval function and set it to enabled property of a button in grid view.

I have tried something below

<asp:Button ID="btnEmail" Text="Send" runat="server" CommandArgument='<%#Eval("CandidateId")%>' Enabled='<%#(Eval("IsEmail").ToString().Equals("0"))?"false":"true" %>'   CommandName="Email" />

But it shows error "Cannot convert type string to bool"

Please help me.

I have tried other way round by finding the button control on rowdatabound event of grid view but then also I could not achieve the desired output.

Your suggestions much appreciated.


Try this code:
<asp:button id="btnEmail" text="Send" runat="server" commandargument='<%# IsEnabled(Eval("IsEmail")) %>' commandname="Email" />

and in your code behind write this method:
public bool IsEnabled(object isEmail)
   return isEmail.ToString().Equals("0");

Share this answer
Ahamed Azeem 14-Apr-12 9:50am    

Thanks for your reply

Your function seems to be okay but for ex if I have 2 rows in a datatable and return 0 and another return 1, but both buttons on the grid view is disabled.

What may be the issue

Reza Ahmadi 14-Apr-12 10:16am    
I do not know the detail of your implementation to enable/disable buttons but Your question was to get rid of "Cannot convert type string to bool" error, which above code works fine.
Ahamed Azeem 14-Apr-12 10:29am    

I have tried below code as well but the result was same as before.

protected void grdApplicants_RowDataBound(object sender, GridViewRowEventArgs e)
if (!IsPostBack)
DataTable dtApplicants = ((DataTable)Session["dtApplicants"]);
if (e.Row.RowType == DataControlRowType.DataRow)

for (int i = 0; i <= dtApplicants.Rows.Count - 1; i++)
Boolean IsEmail = Convert.ToBoolean(dtApplicants.Rows[i]["IsEmail"]);

Button btnSend = (Button)e.Row.FindControl("btnEmail");
if (IsEmail)
btnSend.Enabled = false;
btnSend.Enabled = true;
Ahamed Azeem 15-Apr-12 0:03am    
Hello Anyone,

Could you please help me to sort out this issue. I am running behind this issue for last few days.

Just some changes in the markup did the job, no need of other events in the code behind

below is the code for your reference
Enabled='<%# (Boolean.Parse(Eval("IsEmail").ToString())? Boolean.Parse("false"): Boolean.Parse("true"))%>'

Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900