Conditional Processing

W

Wayne Wengert

In a Datagrid I have a field (<td>) in which I want to display a link if a
database field is still Null but I want to display a blank (&nbsp;) if that
DB field has a value. If I try to put the "..If ...Then..) within the TD it
simply displays that as text. I am not sure how to code this conditon? What
I am trying to accomplish is something like the following. I want to test
the "status" field for the condition.

<td>
If <%# DataBinder.Eval(Container.DataItem,"status") %> Is Null Then
<asp:HyperLink id="XX" runat="server" NavigateUrl= '<%#
"httpxyz.com/evaljudge.asp?name=" +
DataBinder.Eval(Container.DataItem,"JudgeName") + "&caption=" +
DataBinder.Eval(Container.DataItem,"Caption") + "&evalid=" + evalID %>'
Text= "Evaluate" >
</asp:HyperLink>
Else
&nbsp;
End If
</td>
 
W

Wayne Wengert

Thanks Bill;

I didn't realize you can use IIF in that case. The variations in .NET syntax
have me big time confused!!

I'll give that a try

Wayne

Bill Borg said:
Wayne, there are a couple ways to do it, but basically you need in your
databinding expression a single function that returns a string (your
expression can't contain "logic" per se). You can write your own function,
or easier in this case might be to use an immediate if (iif in vb, or
tertiary operator in C# I think), something like::
NavigateURL = '<%# iif(condition,string if true, string if false) %>'

... where "condition" is the comparison of your database field to null,
"string if true" is your url with the data fields filled into the
querystring, and "string if false" is &nbsp;
 
G

Guest

P.S. Momentary brain cramp...you don't want your NavigateURL to evaluate to &nbsp in the false condition. If you do it this way, if false you want NavigateURL="" and Text="" (or visible=false). Or, write a function, or, as Steve suggests, trap ItemDataBound or ItemCreated and do it all in the code-behind.

----- Bill Borg wrote: -----

Wayne, there are a couple ways to do it, but basically you need in your databinding expression a single function that returns a string (your expression can't contain "logic" per se). You can write your own function, or easier in this case might be to use an immediate if (iif in vb, or tertiary operator in C# I think), something like::

NavigateURL = '<%# iif(condition,string if true, string if false) %>'

... where "condition" is the comparison of your database field to null, "string if true" is your url with the data fields filled into the querystring, and "string if false" is &nbsp;

Bill

----- Wayne Wengert wrote: -----

In a Datagrid I have a field (<td>) in which I want to display a link if a
database field is still Null but I want to display a blank (&nbsp;) if that
DB field has a value. If I try to put the "..If ...Then..) within the TD it
simply displays that as text. I am not sure how to code this conditon? What
I am trying to accomplish is something like the following. I want to test
the "status" field for the condition.

<td>
If <%# DataBinder.Eval(Container.DataItem,"status") %> Is Null Then
<asp:HyperLink id="XX" runat="server" NavigateUrl= '<%#
"httpxyz.com/evaljudge.asp?name=" +
DataBinder.Eval(Container.DataItem,"JudgeName") + "&caption=" +
DataBinder.Eval(Container.DataItem,"Caption") + "&evalid=" + evalID %>'
Text= "Evaluate" ></asp:HyperLink>
Else
&nbsp;
End If
</td>
 
W

Wayne Wengert

Bill;

I tried the code below but the compiler complains that an expression is
expected. I can't see the problem here?

NavigateUrl= '<%# IIF(DataBinder.Eval(Container.DataItem,"ProposedStatus") >
1, "http://wengert.org/evaljudge.asp?name=" +
DataBinder.Eval(Container.DataItem,"JudgeName") + "&caption=" +
DataBinder.Eval(Container.DataItem,"Caption") + "&evalid=" + evalID, &nbsp;)
%>'

Wayne

Bill Borg said:
Wayne, there are a couple ways to do it, but basically you need in your
databinding expression a single function that returns a string (your
expression can't contain "logic" per se). You can write your own function,
or easier in this case might be to use an immediate if (iif in vb, or
tertiary operator in C# I think), something like::
NavigateURL = '<%# iif(condition,string if true, string if false) %>'

... where "condition" is the comparison of your database field to null,
"string if true" is your url with the data fields filled into the
querystring, and "string if false" is &nbsp;
 
W

Wayne Wengert

I did figure out he issue with &nbsp; and plugged in an empty string. I also
adjusted the code to:


NavigateUrl= '<%# IIF(DataBinder.Eval(Container.DataItem,"ProposedStatus")
IS NOT System.DBNull, "http://wengert.org/evaljudge.asp?name=" +
DataBinder.Eval(Container.DataItem,"JudgeName") + "&caption=" +
DataBinder.Eval(Container.DataItem,"Caption") + "&evalid=" + evalID, " ")
%>'

I originally used "Is Not Null" but the compiler complained and said to use
System.DBNull. When I do that the compiler complains that System.DBNull is a
Type and cannot be used in an expression? Catch 22

Wayne
 
W

Wayne Wengert

Bill;

Thanks for the input. Actually I went to the DataBinder.Eval because the
Container.DataItem("myfield") did not work?

Wayne

Bill Borg said:
You should be able to use system.dbnull.value, although I've not tried
that in my example. You can also simplify the syntax a bit by using just
*Container.Dataitem("ProposedStatus")* in place of the whole
*DataBinder.Eval(Container.Dataitem, "ProposedStatus")*, etc. And I presume
that you found you need that around your "+ evalID" too. Enjoy.
----- Wayne Wengert wrote: -----

I did figure out he issue with &nbsp; and plugged in an empty string. I also
adjusted the code to:


NavigateUrl= '<%# IIF(DataBinder.Eval(Container.DataItem,"ProposedStatus")
IS NOT System.DBNull, "http://wengert.org/evaljudge.asp?name=" +
DataBinder.Eval(Container.DataItem,"JudgeName") + "&caption=" +
DataBinder.Eval(Container.DataItem,"Caption") + "&evalid=" + evalID, " ")
%>'

I originally used "Is Not Null" but the compiler complained and said to use
System.DBNull. When I do that the compiler complains that System.DBNull is a
Type and cannot be used in an expression? Catch 22

Wayne

evalID, false) display a
link within want
DataBinder.Eval(Container.DataItem,"status") %> Is
 
W

Wayne Wengert

Bill;

After re-reading your post I see you specified "System.DBNull.Value". I had
left off the ".Value" Making that change has changed the error I get, Now
the compiler gives the following error:

BC30455: Argument not specified for parameter 'TruePart' of 'Public Function
IIf(Expression As Boolean, TruePart As Object, FalsePart As Object) As
Object'.

I read somewhere that the true and false parts of IIF should be strings? I
am not sure what the references to Objects in the error message are.
The statement I am currently trying is as follows:

<asp:HyperLink id="XX" runat="server" NavigateUrl= '<%#
IIF(DataBinder.Eval(Container.DataItem,"ProposedStatus") NOT IS
System.DBNull.Value, "http://wengert.org/evaljudge.asp?name=" +
DataBinder.Eval(Container.DataItem,"JudgeName") + "&caption=" +
DataBinder.Eval(Container.DataItem,"Caption") + "&evalid=" + evalID, " ")
%>' Text= "Evaluate" ></asp:HyperLink>

Wayne

Bill Borg said:
You should be able to use system.dbnull.value, although I've not tried
that in my example. You can also simplify the syntax a bit by using just
*Container.Dataitem("ProposedStatus")* in place of the whole
*DataBinder.Eval(Container.Dataitem, "ProposedStatus")*, etc. And I presume
that you found you need that around your "+ evalID" too. Enjoy.
----- Wayne Wengert wrote: -----

I did figure out he issue with &nbsp; and plugged in an empty string. I also
adjusted the code to:


NavigateUrl= '<%# IIF(DataBinder.Eval(Container.DataItem,"ProposedStatus")
IS NOT System.DBNull, "http://wengert.org/evaljudge.asp?name=" +
DataBinder.Eval(Container.DataItem,"JudgeName") + "&caption=" +
DataBinder.Eval(Container.DataItem,"Caption") + "&evalid=" + evalID, " ")
%>'

I originally used "Is Not Null" but the compiler complained and said to use
System.DBNull. When I do that the compiler complains that System.DBNull is a
Type and cannot be used in an expression? Catch 22

Wayne

evalID, false) display a
link within want
DataBinder.Eval(Container.DataItem,"status") %> Is
 
G

Guest

Wayne, there are a couple things going on (I don't think the "not is" syntax is valid, you need to wrap the "evalid" with your same container reference, etc.), but part of the problem with doing it this way is that you can't get any help from the compiler when playing with the syntax

So, let's simplify and do it in the code-behind

Public Function GetJudge(ByVal data As DataRowView) As Strin
Dim strResult As Strin
If data("ProposedStatus") Is DBNull.Value The
strResult = String.Format(
"http://wengert.org/evaljudge.asp?name={0}&caption={1}&evalid={2}",
data("JudgeName"), data("Caption"), data("EvalID")
End I
Return strResul
End Functio

....then, in the html

<asp:TemplateColumn HeaderText="Wayne's Column"><itemtemplate><asp:HyperLink id="Hyperlink1" runat="server" NavigateUrl= '<%# GetJudge(container.dataitem) %>' Text= "Evaluate" ></asp:HyperLink></itemtemplate></asp:TemplateColumn

Notice how container.dataitem is sent to GetJudge as a datarowview, and from there you can get whatever fields you want from the datasource. Rather than write GetJudge, you could also put similar code behind on OnItemDataBound, which would also make it easier for you to set multiple attributes (e.g. do a findcontrol from within ItemDataBound to get your hyperlink control, and then set Text, NavigateURL, Visible, etc.) at the same time if you need to. Anyway, I think you'll find what's above will work

hth

Bil

----- Wayne Wengert wrote: ----

Bill

After re-reading your post I see you specified "System.DBNull.Value". I ha
left off the ".Value" Making that change has changed the error I get, No
the compiler gives the following error

BC30455: Argument not specified for parameter 'TruePart' of 'Public Functio
IIf(Expression As Boolean, TruePart As Object, FalsePart As Object) A
Object'

I read somewhere that the true and false parts of IIF should be strings?
am not sure what the references to Objects in the error message are
The statement I am currently trying is as follows

<asp:HyperLink id="XX" runat="server" NavigateUrl= '<%
IIF(DataBinder.Eval(Container.DataItem,"ProposedStatus") NOT I
System.DBNull.Value, "http://wengert.org/evaljudge.asp?name="
DataBinder.Eval(Container.DataItem,"JudgeName") + "&caption="
DataBinder.Eval(Container.DataItem,"Caption") + "&evalid=" + evalID, " "
%>' Text= "Evaluate" ></asp:HyperLink

Wayn

Bill Borg said:
You should be able to use system.dbnull.value, although I've not trie
that in my example. You can also simplify the syntax a bit by using jus
*Container.Dataitem("ProposedStatus")* in place of the whol
*DataBinder.Eval(Container.Dataitem, "ProposedStatus")*, etc. And I presum
that you found you need that around your "+ evalID" too. Enjoy
I als
adjusted the code to IIF(DataBinder.Eval(Container.DataItem,"ProposedStatus"
IS NOT System.DBNull, "http://wengert.org/evaljudge.asp?name="
DataBinder.Eval(Container.DataItem,"JudgeName") + "&caption="
DataBinder.Eval(Container.DataItem,"Caption") + "&evalid=" + evalID " "
%> to us
System.DBNull. When I do that the compiler complains tha System.DBNull is
Type and cannot be used in an expression? Catch 2 evalID, false) display a
link within want
DataBinder.Eval(Container.DataItem,"status") %> Is
 
W

Wayne Wengert

Thanks again Bill;

I am not using codebehind yet - can't get things working in some cases.
Anyway, I'll experiment with your suggest in my "one page" form.

Again, I really do appreciate the help.

Wayne

Bill Borg said:
Wayne, there are a couple things going on (I don't think the "not is"
syntax is valid, you need to wrap the "evalid" with your same container
reference, etc.), but part of the problem with doing it this way is that you
can't get any help from the compiler when playing with the syntax.
So, let's simplify and do it in the code-behind:

Public Function GetJudge(ByVal data As DataRowView) As String
Dim strResult As String
If data("ProposedStatus") Is DBNull.Value Then
strResult = String.Format( _
"http://wengert.org/evaljudge.asp?name={0}&caption={1}&evalid={2}", _
data("JudgeName"), data("Caption"), data("EvalID"))
End If
Return strResult
End Function

...then, in the html:

<asp:TemplateColumn HeaderText="Wayne's
Column"> said:
</asp:HyperLink></itemtemplate></asp:TemplateColumn>

Notice how container.dataitem is sent to GetJudge as a datarowview, and
from there you can get whatever fields you want from the datasource. Rather
than write GetJudge, you could also put similar code behind on
OnItemDataBound, which would also make it easier for you to set multiple
attributes (e.g. do a findcontrol from within ItemDataBound to get your
hyperlink control, and then set Text, NavigateURL, Visible, etc.) at the
same time if you need to. Anyway, I think you'll find what's above will
work.
 
W

Wayne Wengert

Bill;

I hate to be a pest but after modifying things as suggested I get the
following compiler error:

Specified cast is not valid.

Source Error:

Line 74: <tr bgcolor="lightcyan">
Line 75: <td>
Line 76: <asp:HyperLink id="XX"
runat="server" NavigateUrl= "<%# GetJudge(container.dataitem) %>" Text=
"Evaluate" ></asp:HyperLink>
Line 77: </td>



I can see that the function should return a string so I don't understand
what the cast error is referring to?

Wayne

Bill Borg said:
Wayne, there are a couple things going on (I don't think the "not is"
syntax is valid, you need to wrap the "evalid" with your same container
reference, etc.), but part of the problem with doing it this way is that you
can't get any help from the compiler when playing with the syntax.
So, let's simplify and do it in the code-behind:

Public Function GetJudge(ByVal data As DataRowView) As String
Dim strResult As String
If data("ProposedStatus") Is DBNull.Value Then
strResult = String.Format( _
"http://wengert.org/evaljudge.asp?name={0}&caption={1}&evalid={2}", _
data("JudgeName"), data("Caption"), data("EvalID"))
End If
Return strResult
End Function

...then, in the html:

<asp:TemplateColumn HeaderText="Wayne's
Column"> said:
</asp:HyperLink></itemtemplate></asp:TemplateColumn>

Notice how container.dataitem is sent to GetJudge as a datarowview, and
from there you can get whatever fields you want from the datasource. Rather
than write GetJudge, you could also put similar code behind on
OnItemDataBound, which would also make it easier for you to set multiple
attributes (e.g. do a findcontrol from within ItemDataBound to get your
hyperlink control, and then set Text, NavigateURL, Visible, etc.) at the
same time if you need to. Anyway, I think you'll find what's above will
work.
 
G

Guest

Happy to help. I presume that the "type" of container.dataitem does not match the input parameter on GetJudge. In my example, I'm presuming that your datasource is a dataset, which makes container.dataitem a datarowview, which is what my GetJudge is looking for. If your datasource were, say, an arraylist or something else, then you'd need to make sure that's what GetJudge is looking for. Using the debugger, you should be able to figure out the type of container.dataitem, or if you get stuck you can define GetJudge to accept a plain old object, and then from within GetJudge itself do a .gettype on what's passed to figure out what it is. Good luck

----- Wayne Wengert wrote: ----

Bill

I hate to be a pest but after modifying things as suggested I get th
following compiler error

Specified cast is not valid

Source Error

Line 74: <tr bgcolor="lightcyan"
Line 75: <td
Line 76: <asp:HyperLink id="XX
runat="server" NavigateUrl= "<%# GetJudge(container.dataitem) %>" Text
"Evaluate" ></asp:HyperLink
Line 77: </td



I can see that the function should return a string so I don't understan
what the cast error is referring to

Wayn

Bill Borg said:
Wayne, there are a couple things going on (I don't think the "not is
syntax is valid, you need to wrap the "evalid" with your same containe
reference, etc.), but part of the problem with doing it this way is that yo
can't get any help from the compiler when playing with the syntax
Dim strResult As Strin
If data("ProposedStatus") Is DBNull.Value The
strResult = String.Format(
"http://wengert.org/evaljudge.asp?name={0}&caption={1}&evalid={2}",
data("JudgeName"), data("Caption"), data("EvalID")
End I
Return strResul
End Functio
Column"> said:
</asp:HyperLink></itemtemplate></asp:TemplateColumn>>> Notice how container.dataitem is sent to GetJudge as a datarowview, an
from there you can get whatever fields you want from the datasource. Rathe
than write GetJudge, you could also put similar code behind o
OnItemDataBound, which would also make it easier for you to set multipl
attributes (e.g. do a findcontrol from within ItemDataBound to get you
hyperlink control, and then set Text, NavigateURL, Visible, etc.) at th
same time if you need to. Anyway, I think you'll find what's above wil
work
 
W

Wayne Wengert

Bill;

I think I understand - it looks like datarowview is a type(?) that only
exists in a datagrid (is that right?). I am using a datareader to retrieve
the data - I thought that was the preferred way for read only/forward only?

Anyway, I'll see if I can fond the datareader equivalent of datarowview.

Wayne

(when searching on datarowview in google the hits were usually way over my
head)
Bill Borg said:
Happy to help. I presume that the "type" of container.dataitem does not
match the input parameter on GetJudge. In my example, I'm presuming that
your datasource is a dataset, which makes container.dataitem a datarowview,
which is what my GetJudge is looking for. If your datasource were, say, an
arraylist or something else, then you'd need to make sure that's what
GetJudge is looking for. Using the debugger, you should be able to figure
out the type of container.dataitem, or if you get stuck you can define
GetJudge to accept a plain old object, and then from within GetJudge itself
do a .gettype on what's passed to figure out what it is. Good luck.
----- Wayne Wengert wrote: -----

Bill;

I hate to be a pest but after modifying things as suggested I get the
following compiler error:

Specified cast is not valid.

Source Error:

Line 74: <tr bgcolor="lightcyan">
Line 75: <td>
Line 76: <asp:HyperLink id="XX"
runat="server" NavigateUrl= "<%# GetJudge(container.dataitem) %>" Text=
"Evaluate" ></asp:HyperLink>
Line 77: </td>



I can see that the function should return a string so I don't understand
what the cast error is referring to?

Wayne

is"
syntax is valid, you need to wrap the "evalid" with your same container
reference, etc.), but part of the problem with doing it this way is that you
can't get any help from the compiler when playing with the syntax.
"http://wengert.org/evaljudge.asp?name={0}&caption={1}&evalid={2}", _
container.dataitem is sent to GetJudge as a datarowview, and
 
G

Guest

Yes, it's a type. It doesn't *only* exist in a datagrid, but it happens that if the datasource for the datagrid is a dataset, then that's the type it will return. In your case, the datasource is a datareader, and it returns a different type (you made me curious, so I traced through to find the type and learned something new; if you get stuck, come back, else you'd be on the right track if you let GetJudge call for a generic object and then check out the type once GetJudge has it). Have fun, and I'll check on you through the weekend

Bil

----- Wayne Wengert wrote: ----

Bill

I think I understand - it looks like datarowview is a type(?) that onl
exists in a datagrid (is that right?). I am using a datareader to retriev
the data - I thought that was the preferred way for read only/forward only

Anyway, I'll see if I can fond the datareader equivalent of datarowview

Wayn

(when searching on datarowview in google the hits were usually way over m
head

<snip>
 
W

Wayne Wengert

OK - I'll go play some more. FYI - I am using a datalist, not a datagrid -
does that change things?

Wayne

Bill Borg said:
Yes, it's a type. It doesn't *only* exist in a datagrid, but it happens
that if the datasource for the datagrid is a dataset, then that's the type
it will return. In your case, the datasource is a datareader, and it
returns a different type (you made me curious, so I traced through to find
the type and learned something new; if you get stuck, come back, else you'd
be on the right track if you let GetJudge call for a generic object and then
check out the type once GetJudge has it). Have fun, and I'll check on you
through the weekend.
 
W

Wayne Wengert

I've been trying to use the OnItemDataBound to simply change the Text part
of the hyperlink to an empty string when the condition is met but I cannot
seem to find the right syntax to refer to that specific item. In reading all
sorts of MSDN and ASP sites I am trying the following but it gives a compile
error

BC30456: 'Cells' is not a member of
'System.Web.UI.WebControls.DataListItem'.


Here is what I have for the OnItemDataBound sub:

Sub JudgeList_MyHandler(sender as Object, e as DataListItemEventArgs)
If e.Item.ItemType = ListItemType.Item OR _
e.Item.ItemType = ListItemType.AlternatingItem then
'Set the hyperlink text expression
e.Item.Cells(0).Text = "Evaluate"
If e.Item.DataItem, "proposedstatus") Is DBNull.Value then
e.Item.Cells(0).Text = ""
End If
End If
End Sub

Does it really have to be this compex??? <smile!>

Wayne


Bill Borg said:
Yes, it's a type. It doesn't *only* exist in a datagrid, but it happens
that if the datasource for the datagrid is a dataset, then that's the type
it will return. In your case, the datasource is a datareader, and it
returns a different type (you made me curious, so I traced through to find
the type and learned something new; if you get stuck, come back, else you'd
be on the right track if you let GetJudge call for a generic object and then
check out the type once GetJudge has it). Have fun, and I'll check on you
through the weekend.
 
G

Guest

Hi Wayne

First, in your example, datalist does not have a "cells" collection like the datagrid does, but you could use the "controls" collection

But second, using cells(x) or controls(0) is so dependent on the position of how you build your datalist and what controls are in the template, etc. I would always use "findcontrol" instead, which based on id will find a control in the current container e.g

dim myTextBox as textbo
myTextBox = ctype(e.item.findcontrol("TextBoxID"), textbox
myTextBox.Text = "Whatever...

....where TextBoxID is the ID of the control in the template

Third, from your earlier example, the datareader appears to return type 'dbdatarecord', which has an item property that you can use in the same way you were trying earlier, e.g. mydbdatarecord.item("MyField"), or just mydbdatarecord("MyField")

Fourth, seems complex now, but the end is in sight:

Bil

----- Wayne Wengert wrote: ----

I've been trying to use the OnItemDataBound to simply change the Text par
of the hyperlink to an empty string when the condition is met but I canno
seem to find the right syntax to refer to that specific item. In reading al
sorts of MSDN and ASP sites I am trying the following but it gives a compil
erro

BC30456: 'Cells' is not a member o
'System.Web.UI.WebControls.DataListItem'


Here is what I have for the OnItemDataBound sub

Sub JudgeList_MyHandler(sender as Object, e as DataListItemEventArgs
If e.Item.ItemType = ListItemType.Item OR
e.Item.ItemType = ListItemType.AlternatingItem the
'Set the hyperlink text expressio
e.Item.Cells(0).Text = "Evaluate
If e.Item.DataItem, "proposedstatus") Is DBNull.Value the
e.Item.Cells(0).Text = "
End I
End I
End Su

Does it really have to be this compex??? <smile!

Wayn


Bill Borg said:
Yes, it's a type. It doesn't *only* exist in a datagrid, but it happen
that if the datasource for the datagrid is a dataset, then that's the typ
it will return. In your case, the datasource is a datareader, and i
returns a different type (you made me curious, so I traced through to fin
the type and learned something new; if you get stuck, come back, else you'
be on the right track if you let GetJudge call for a generic object and the
check out the type once GetJudge has it). Have fun, and I'll check on yo
through the weekend
 
W

Wayne Wengert

Thanks;

I will take your input and try some redesign.

Wayne

Bill Borg said:
Hi Wayne,

First, in your example, datalist does not have a "cells" collection like
the datagrid does, but you could use the "controls" collection.
But second, using cells(x) or controls(0) is so dependent on the position
of how you build your datalist and what controls are in the template, etc. I
would always use "findcontrol" instead, which based on id will find a
control in the current container e.g.
dim myTextBox as textbox
myTextBox = ctype(e.item.findcontrol("TextBoxID"), textbox)
myTextBox.Text = "Whatever..."

...where TextBoxID is the ID of the control in the template.

Third, from your earlier example, the datareader appears to return type
'dbdatarecord', which has an item property that you can use in the same way
you were trying earlier, e.g. mydbdatarecord.item("MyField"), or just
mydbdatarecord("MyField").
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,062
Latest member
OrderKetozenseACV

Latest Threads

Top