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

M

Morris Neuman

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.
 
S

Steven Cheng[MSFT]

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.webcontrols

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/subscriptions/managednewsgroups/default.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/subscriptions/support/default.aspx.

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



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

Morris Neuman

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 " "
 
S

Steven Cheng[MSFT]

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.
 
S

Steven Cheng[MSFT]

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.
 

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,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top