• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 584
  • Last Modified:

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

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.

1 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

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now