Using a boolean for conditional updates, why isn't this working?

Posted on 2007-08-06
Last Modified: 2012-05-05
I've got a bit of an issue and I can't figure it out. I'm doing some updates on a grid and due to the way the update works from the RadGrid I'm trying to implement the following.

I've declared a boolean Dim bRelCode As Boolean and I've got  a selectedindexchanged event firing on a dropdown as follows:

    Public Sub dropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
        'Load the label using the selected value of first dropdownlist.

        'first reference the edited grid item through the NamingContainer attribute
        Dim editedItem As GridEditableItem = CType(CType(sender, DropDownList).NamingContainer, GridEditableItem)

        Dim lblRelCode As Label = CType(editedItem("TemplateColumnRelCode").FindControl("RelCode"), Label)

        Dim table As DataTable = DataSourceHelperLK.GetDataTable(("SELECT code_ID FROM contrid WHERE ci_title = '" _
        + CType(editedItem("TemplateColumnRelCodeDesc").FindControl("DropDownList1"), DropDownList).SelectedItem.Text + "'"))

        'Dim lstRelCode As DataList = CType(editedItem("TemplateColumnRelCode").FindControl("lstRelCode"), DataList)
        Dim grid As GridView = CType(editedItem("TemplateColumnRelCode").FindControl("gridview1"), GridView)
        Dim lbl As Label = CType(editedItem("TemplateColumnRelCode").FindControl("RelCode"), Label)

        grid.DataSource = table
        lbl.Visible = False
        bRelCode = True

    End Sub

Now that sets my boolean to true and everything is fine at this point, but when I go to do the update portion it seems my boolean is losing the value assigned to it in the dropdown changed event. And when I get to this bit of code in the update sub;

            If bRelCode = True Then
                grid = TryCast(eeditedItem.FindControl("Gridview1"), GridView)
                lblRelCode = CType(grid.Rows(Convert.ToInt32(0)).Cells(0).FindControl("lblRelCode"), Label)
                lblRelCode = CType(eeditedItem.FindControl("RelCode"), Label)
            End If

My bRelCode = false and the wrong piece of that if..then is firing.

Question by:ApexCo
    1 Comment
    LVL 25

    Accepted Solution

    Is this all happening on the same postback?  If so, the update code may be exceuting before the selectedindexchanged event.  If it's another postback, then the value will be lost because each post-back is atomic; server-side variables (including properties of the form) will not persist between post-backs.  You can either:

    1) Use values from the form controls rather than a variable
    2) Save the variable in the ViewStatate:      ViewState(bRelCode) = True)
    3) Add the variable as a hidden input on the form

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
    Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now