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

Discussion in 'ASP .Net Web Controls' started by Morris Neuman, Apr 16, 2007.

  1. 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
     
    Morris Neuman, Apr 16, 2007
    #1
    1. Advertising

  2. 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.
     
    Steven Cheng[MSFT], Apr 16, 2007
    #2
    1. Advertising

  3. RE: Do not display a formview if one of its data bound fields is n

    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.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.
    >
    >
    >
     
    Morris Neuman, Apr 16, 2007
    #3
  4. RE: Do not display a formview if one of its data bound fields is n

    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.
     
    Steven Cheng[MSFT], Apr 17, 2007
    #4
  5. RE: Do not display a formview if one of its data bound fields is n

    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.
     
    Steven Cheng[MSFT], Apr 19, 2007
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mario Krsnic
    Replies:
    0
    Views:
    386
    Mario Krsnic
    Jun 23, 2006
  2. call_me_anything
    Replies:
    4
    Views:
    469
    Pete Becker
    Sep 30, 2007
  3. thunk
    Replies:
    1
    Views:
    318
    thunk
    Mar 30, 2010
  4. thunk
    Replies:
    0
    Views:
    491
    thunk
    Apr 1, 2010
  5. thunk
    Replies:
    14
    Views:
    629
    thunk
    Apr 3, 2010
Loading...

Share This Page