• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 214
  • Last Modified:

Inline Boolean Condition Not Being Met in ASP DataGrid

I inherited a web application written in c# which contains a datagrid. This dg has ItemTemplates, one of which has the following code:

			<ItemTemplate>
				<asp:textbox id="NewQuantity" onkeydown="processKeystroke(event);" runat="server" width="35px" columns="4" maxlength="3" text="" cssclass="EditTextBox" 
				Visible='<%# (DataBinder.Eval(Container.DataItem, "ProductID") == "") || (DataBinder.Eval(Container.DataItem, "ProductID") == DBNull.Value)? false:true  %>'>
				</asp:textbox>
			</ItemTemplate>

Open in new window


If I understand the code correctly, the textbox will either be Visible or not Visible dependant on whether or not the ProductID is Empty || Null.

Trouble is, the condition always seems to be 'True' even when the data is an Empty String or Null.

An example of the data from my db:
PL_ID      ProductID      ModelName
14292324                 ***MELONS***

What could be causing the condition to always result in True?
Jason
0
jsvb1977
Asked:
jsvb1977
  • 5
  • 4
1 Solution
 
HainKurtSr. System AnalystCommented:
lets try something

Visible='<%# false %>'

what happens if you do this?
0
 
HainKurtSr. System AnalystCommented:
or just try this

Visible='<%# (DataBinder.Eval(Container.DataItem, "ProductID") == "") ? false:true  %>
0
 
jsvb1977Author Commented:
I tried 3 scenerios:

1. Visible='<%# false %>'
    - Resulted in all textboxes NOT Visible

2. Visible='<%# (DataBinder.Eval(Container.DataItem, "ProductID") == "") ? false:true  %>
    - Resulted in all textboxes Visible

3. Visible='<%# (DataBinder.Eval(Container.DataItem, "ProductID") == DBNull.Value)? false:true  %>'>
    - Resulted in all textboxes Visible
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
HainKurtSr. System AnalystCommented:

ok, try

Visible='<%# true %>' mydata='<%# DataBinder.Eval(Container.DataItem, "ProductID")%>'

and check view source of page for mydata
what do you see...
0
 
HainKurtSr. System AnalystCommented:
looks like it is working and your records have all ProductID populated...
maybe it is not null, maybe it is some space, try trim

 Visible='<%# (DataBinder.Eval(Container.DataItem, "ProductID").Trim() == "") ? false:true  %>
0
 
jsvb1977Author Commented:
Good call... but this:

Compiler Error Message: CS0117: 'object' does not contain a definition for 'Trim'

So i wrapped it in a convert.tostring(), but i am still getting the error:

Here is the code:
Visible='<%# Convert.ToString((DataBinder.Eval(Container.DataItem, "ProductID")).Trim() == "") || (DataBinder.Eval(Container.DataItem, "ProductID") == DBNull.Value)? false:true  %>'>

Maybe the convert.tostring needs to be somewhere else?
Jason
0
 
jsvb1977Author Commented:
Ahhh, that was it...

Visible='<%# (Convert.ToString(DataBinder.Eval(Container.DataItem, "ProductID")).Trim() == "") ? false:true  %>'>

Open in new window

0
 
HainKurtSr. System AnalystCommented:
what about this

Visible='<%# (DataBinder.Eval(Container.DataItem, "ProductID").ToString().Trim() == "") ? false:true  %>'>

is trim solving your issues?
0
 
jsvb1977Author Commented:
Yes, using trim with the convert.tostring is what did it. Like this:

Visible='<%# (Convert.ToString(DataBinder.Eval(Container.DataItem, "ProductID")).Trim() == "") ? false:true  %>'>
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now