User91 User91 - 10 months ago 51 Question

Button visibility before and after session is called in VB

I have two web forms, say a target form and source form. I have two buttons Save and Update on the target form. What i want these button to do is simply a work with there visibility. I want Save button to be visible when this form is opened but Update button to be invisible. But when this form acts as a target form, i.e., when this form is redirected from another form, I want the save button to be invisible and Update button to be visible. Here is the code that I wrote in target form

Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load

update_btn.Visible = False
Dim str As String
Dim i As Integer
Dim checkData As New DataTable
str = "select AreaID, Years, StateForest, PrivateForest, TotalForest, PercentageSF, PercentageTotal from forestry_area where AreaID = '" & Session("AreaID").ToString() & "'"
checkData = c.PopulateData(str, i)
If i > 0 Then
ddl_year.SelectedItem.Text = checkData.Rows(0).Item("Years").ToString
SFtx.Text = checkData.Rows(0).Item("StateForest").ToString
PFtx.Text = checkData.Rows(0).Item("PrivateForest").ToString
End If
txtAreaID.Text = Session("AreaID").ToString()
update_btn.Visible = True
save_btn.Visible = False
End Sub

And this is the code that I wrote in source form:

Protected Sub Grid_SelectedIndexChanged(sender As Object, e As EventArgs) Handles Grid.SelectedIndexChanged

Dim id As String
Dim lbl As Label = Grid.SelectedRow.FindControl("Lbl_ID")
id = lbl.Text
Session.Add("AreaID", id)
Response.Redirect("target form.aspx")
End Sub

This code seems to work initially but when I click the update button all the button disappears and Update doesn't even work.

Answer Source

If I understand correctly your form act as target if Session("AreaID") is defined. So you can check this value during Page_Load in order to show/hide your buttons.

Please remember that you need to remove session variabile when they became unuseful.

You can also use a parameter in your Response.Redirect and verify this parameter instead of a session variable:

Response.Redirect("targetform.aspx?areaID=" + id)