简体   繁体   中英

VB.Net How to check if a value has been set for a property

I've spent a few hours trying to figure this out and have come up empty. I have a .ascx usercontrol that I pass some values so it will fill some dropdownlists. If no value has been "Set" for the dropdownlist, I'd like to hide the dropdownlist. This is what I have in my codebehind (I cut out irrelevant parts) databaselinks.ascx.vb:

Public Class databaselinks
Inherits System.Web.UI.UserControl

Public Property Drawing As String
    Get
        '
    End Get
    Set(ByVal value As String)
        If Not (value Is Nothing) Then
            ' Create a new ListItemCollection
            Dim liDrawingLink As New ListItemCollection()

            ' Add items to the collection
            liDrawingLink.Add(New ListItem("For this drawing...", ""))
            liDrawingLink.Add(New ListItem("...view drawing details", "engdrawingdetail.aspx?dwg=" & value))
            liDrawingLink.Add(New ListItem("...view Bill of Materials", "engdrawingbom.aspx?dwg=" & value))
            liDrawingLink.Add(New ListItem("...list Work Orders", "wolist.aspx?dwg=" & value))
            liDrawingLink.Add(New ListItem("...list transmittals", "engtransmittaldrawing.aspx?dwg=" & value))

            ' Databind our DDL to the ListItemCollection we just filled
            ddDrawingLink.DataSource = liDrawingLink
            ddDrawingLink.DataTextField = "Text"
            ddDrawingLink.DataValueField = "Value"
            ddDrawingLink.DataBind()
            ddDrawingLink.Visible = True
        Else
            ddDrawingLink.Visible = False
        End If

    End Set
End Property

Sub btnGo3_Click(ByVal sender As Object, ByVal e As EventArgs)
    Response.Redirect(ddDrawingLink.SelectedItem.Value)
End Sub
End Class

And my usercontrol databaselinks.ascx:

<%@ Control Language="vb" AutoEventWireup="false" CodeBehind="databaselinks.ascx.vb" Inherits="Rubb_Intranet_Databases.databaselinks" %>

<asp:Table Id="tblA" RunAt="server" CssClass="printhide">
    <asp:TableRow Id="trA1" RunAt="server" >
        <asp:TableCell Id="tdA1" RunAt="server">
            <asp:Table Id="tblB" RunAt="server">
                <asp:TableRow Id="trB1" RunAt="server" >
                    <asp:TableCell Id="tdB1" RunAt="server" BackColor="#FFFF99" Style="font-size: 9px; border: 1px solid Black; padding: 2px;">
                        Choose a page to visit from the dropdown
                    </asp:TableCell>
                </asp:TableRow>
            </asp:Table>
        </asp:TableCell>
    </asp:TableRow>
    <asp:TableRow Id="TableRow1" RunAt="server" >
        <asp:TableCell Id="TableCell1" RunAt="server">
            <asp:DropDownList Id="ddDrawingLink" RunAt="server" Width="190" OnSelectedIndexChanged="btnGo3_Click" AutoPostBack="True" Visible="False" />
        </asp:TableCell>
    </asp:TableRow>
</asp:Table>

In my main page I set the value for the usercontrol with:

dblinks.Drawing = "12345"

which works great, but on pages where I haven't set the Drawing property, I still get a blank dropdownlist. I am checking if the value Is Nothing, but that doesn't do it, I've also tried checking if it is NullOrEmpty, IsEmpty, to no avail. I've also tried setting the default value to Nothing and that hasn't worked either. What am I missing? I know I can force the value to be Nothing by setting it on my pages, but rather than go through dozens of pages to set the value to be Nothing, it seems simpler to have the control figure out whether the value has been set.

Thank you for your input, I have learned a lot from StackOverflow, but this is the first time I've had to reach out for help.

-=Glen

This logic:

If Not (value Is Nothing) Then

is in the property setter . So if nothing ever sets a value to the property, that logic will never be invoked. Perhaps you want to put it in Page_Load instead? (Or if there's an equivalent for user controls, it's been a while since I've had to use Web Forms thankfully.) Something like this:

Sub Page_Load(ByVal Sender As System.Object, ByVal e As System.EventArgs)
    If Drawing Is Nothing Then
        ddDrawingLink.Visible = False
    End If
End Sub

This would conditionally hide that one control when the user control is being loaded in the context of a page, rather than when setting the property value. (Since logically you want this to happen when you don't set the property value.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM