Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Web Controls > Do not display a formview if one of its data bound fields is null.

Reply
Thread Tools

Do not display a formview if one of its data bound fields is null.

 
 
Morris Neuman
Guest
Posts: n/a
 
      04-15-2007
Hi,

I have a formview bound to an AccessDataSource.

AccessDataSource is defined as:
<asp:AccessDataSource ID="AccessDataSource1"
runat="server" DataFile="C:\Program Files\CallMaster\Data\CallMaster.mdb"
SelectCommand="SELECT [CompanyName], [Message],
[SalesContactPhone], [SalesContactEmail], [SupportContactPhone],
[SupportContactEmail], [CompanyWebsite] FROM
[Company]"></asp:AccessDataSource>



The formview is defined as:
<asp:FormView ID="FormView3" runat="server"
DataSourceID="AccessDataSource1">
<ItemTemplate>
Sales:<br />
<asp:Label ID="SalesContactPhoneLabel"
runat="server" Font-Bold="False" Font-Size="8pt"
Text='<%# Bind("SalesContactPhone")
%>'></asp:Label><br />
<asp:Label ID="SalesContactEmailLabel"
runat="server" Font-Bold="False" Font-Size="8pt"
Text='<%# Bind("SalesContactEmail")
%>'></asp:Label><br />
</ItemTemplate>
</asp:FormView>

If the formview field "SalesContactPhoneLabel" is null then I do not want to
display FormView3.

How can I do that?

Any help is appreciated.

--
Thanks
Morris
 
Reply With Quote
 
 
 
 
Steven Cheng[MSFT]
Guest
Posts: n/a
 
      04-16-2007
Hi Morris,

Regarding on this issue, I have also posted some suggest in your another
thread below:

Subject: Do not display table on form if field is null
Newsgroups: microsoft.public.dotnet.framework.aspnet.webcontro ls

You can use the similar approach to do the detection and show/hide in
FormView's PreRender event. e.g.

#first get the label and check the bound value
======================
protected void FormView1_PreRender(object sender, EventArgs e)
{
if (FormView1.CurrentMode == FormViewMode.ReadOnly)
{
Label lbl = FormView1.FindControl("idLabel") as Label;

if (lbl.Text.Contains("3"))
{
FormView1.Visible = false;
}
else
{
FormView1.Visible = true;
}
}
}
====================

How do you think?


Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead



==================================================

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.



Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.

==================================================



This posting is provided "AS IS" with no warranties, and confers no rights.


 
Reply With Quote
 
 
 
 
Morris Neuman
Guest
Posts: n/a
 
      04-16-2007
Hi Steven,

Thank you again for your help. I modified the logic a bit as follows:

Label SalesPhone3 =
FormView3.Row.FindControl("SalesContactPhoneLabel" ) as Label;
if (SalesPhone3.Text.ToString() == "")
{
FormView3.Visible = false;
}
else
{
FormView3.Visible = true;
}

A few questions:
1) why do I have to assign the control to a local variable as a label and
then get the text value of the local label? How can I get the string value
directly of the formview control?
2) what is the difference between null, "" and " "

--
Thanks again for your help.

Morris


"Steven Cheng[MSFT]" wrote:

> Hi Morris,
>
> Regarding on this issue, I have also posted some suggest in your another
> thread below:
>
> Subject: Do not display table on form if field is null
> Newsgroups: microsoft.public.dotnet.framework.aspnet.webcontro ls
>
> You can use the similar approach to do the detection and show/hide in
> FormView's PreRender event. e.g.
>
> #first get the label and check the bound value
> ======================
> protected void FormView1_PreRender(object sender, EventArgs e)
> {
> if (FormView1.CurrentMode == FormViewMode.ReadOnly)
> {
> Label lbl = FormView1.FindControl("idLabel") as Label;
>
> if (lbl.Text.Contains("3"))
> {
> FormView1.Visible = false;
> }
> else
> {
> FormView1.Visible = true;
> }
> }
> }
> ====================
>
> How do you think?
>
>
> Sincerely,
>
> Steven Cheng
>
> Microsoft MSDN Online Support Lead
>
>
>
> ==================================================
>
> Get notification to my posts through email? Please refer to
> http://msdn.microsoft.com/subscripti...ult.aspx#notif
> ications.
>
>
>
> Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
> where an initial response from the community or a Microsoft Support
> Engineer within 1 business day is acceptable. Please note that each follow
> up response may take approximately 2 business days as the support
> professional working with you may need further investigation to reach the
> most efficient resolution. The offering is not appropriate for situations
> that require urgent, real-time or phone-based interactions or complex
> project analysis and dump analysis issues. Issues of this nature are best
> handled working with a dedicated Microsoft Support Engineer by contacting
> Microsoft Customer Support Services (CSS) at
> http://msdn.microsoft.com/subscripti...t/default.aspx.
>
> ==================================================
>
>
>
> This posting is provided "AS IS" with no warranties, and confers no rights.
>
>
>

 
Reply With Quote
 
Steven Cheng[MSFT]
Guest
Posts: n/a
 
      04-17-2007
Hi Morris,

For your further questions:


1) why do I have to assign the control to a local variable as a label and
then get the text value of the local label? How can I get the string value
directly of the formview control?
=====================
After databinding, the column/field value from datasource record has been
bound to the inner sub control(such as TextBox or Label) in the Formview,
therefore, you need to extract the sub inner control instance from
FormView's Control colleciton and pickup the value from those sub control
instances. FormView does not have properties to get them directly.



2) what is the difference between null, "" and " "
=========================
For string value, "null" means that this string variable doesn't point to
any managed string object, it is a null reference. While for "", it means
the string variable does point to a valid string object, but the string is
any empty string. I suggest you always use the

string.IsNullOrEmpty( string )or if(str != null || str != string.Empty)

to test empty string value. The following statement is dangerous

>>>>>>>>>>

SalesPhone3.Text.ToString() == ""
<<<<<<<<<<<

because if SalesPh one3.Text is "null", then call any method on a null
refernce will raise NullReference exception.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no rights.



 
Reply With Quote
 
Steven Cheng[MSFT]
Guest
Posts: n/a
 
      04-19-2007
Hi Morris,

Still any questions on this? If there is anything else you wonder, please
feel free to post here.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no rights.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Its a bird, its a plane, its.. um, an Attribute based System? thunk Ruby 14 04-03-2010 10:08 AM
Its a bird, its a plane, its.. um, an Attribute based System? thunk Ruby 0 04-01-2010 10:25 PM
Its a bird, its a plane, no ummm, its a Ruide thunk Ruby 1 03-30-2010 11:10 AM
print struct fields and its member structs' fields recursively, generically call_me_anything C++ 4 09-30-2007 10:12 PM
How to represent the data in a not bound control via bound control Mario Krsnic ASP .Net 0 06-23-2006 07:38 AM



Advertisments