IF SelectedItem.Equals ??? Then redirect Statement - asp.net

I have a gridview in my website in am building in Visiual studio 2010. IM new to programming and have a query in reference to redirecting the user to another page based on the results of the gridview.
My gridview works perfectly fine and pulls back items via a selection made in a dropdown list. These items have and id assigned (1 for Weekly & 2 for Monthly).
My users selects the Weekly or Montly option from the dropdown and the grid view is populated with this data. (This part works perfectly fine).
Once the results are displayed i want the user to then press the 'Create' button and for them to be directed to the correct Weekly.aspx or Monthly.aspx page based on the selected item from the dropdown list.
So far i have the following code which seems to redirect the user to the Monthly.aspx page for either selection from the dropdown list.
Protected Sub btnCreate_Agenda_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCreate_Agenda.Click
If DropDownList1.SelectedItem.Equals("1") Then
Response.Redirect("Weekly.aspx")
Else
Response.Redirect("Monthly.aspx")
End If
End Sub
Can anyone point me in the right direction?
In my page load event i would also like to make sure that for the webpage item 1 (Weekly) is the default selection. I have tried doing this by adding the following code to the page_load event but i have no just with the results.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
DropDownList1.SelectedItem.Equals("1")
End Sub

You need to assign the value to the dropdown or choose the index of the item, not compare the current value to something.
DropDownList1.SelectedValue = "1"
Or
DropDownList1.SelectedIndex = 1

The SelectedItem property will return a ListItem object, so that will never be equal to the string "1". Use the SelectedValue property instead:
If DropDownList1.SelectedValue = "1" Then
Response.Redirect("Weekly.aspx")
Else
Response.Redirect("Monthly.aspx")
End If
Regarding setting the default, the SelectedItem property is a read-only property. Use the SelectedIndex or SelectedValue properties to select an item:
DropDownList1.SelectedValue = "1"

Related

Toggle column display in ASP.NET gridview

I have two email fields, one a text field (index 15), the other a mailto: hyperlink (index 16), both in a gridview. (And yes, I know identifying via index isn't the best way to go -- just trying to make it work at this point).
When not editing, I need to show only the hyperlink field (making it available for the user to click on). When editing, I need to show only the text field, so they can modify the value.
I've got everything working as needed except that both fields display when the grid is initially shown. If I try to hide the text field in any of the normal ways (hiding cells on RowDataBound or hiding the column upon declaration), then it doesn't show up when editing.
Here's what I'm doing so far. The RowEditing event has the following code:
GridView1.Columns(16).Visible = False
GridView1.Columns(15).Visible = True
The RowCancelingEdit event has the opposite logic, toggling visibility on both fields. And finally the RowUpdating event has the following, which turns the hyperlink display back on:
GridView1.Columns(16).Visible = True
I'm relatively new to ASP.NET, so I definitely don't know all of the constructs available.
How can I hide the text field upon normal grid display, but still have the field available to show when in edit mode?
Try to RowCommand Event and set Edit button CommandName="name"
If e.CommandName = "name" Then
Dim row As GridViewRow = DirectCast(DirectCast(e.CommandSource, LinkButton).NamingContainer, GridViewRow)
Dim lblwwwhid = CType(row.FindControl("txtwwwhid"), Label)
lblwwwhid .visible =false
End if
It dawned on me that I could simply show/hide columns upon the initial databind (which works), as such:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
DataBind()
GridView1.Columns(16).Visible = True
GridView1.Columns(15).Visible = False
End If
End Sub

vb asp.net session variable not working?

I just want to control which button user clicked, so I use Session Variable to store that data because I have to create all dynamic control in the Page_Load (to allow event handler work properly). The problem is this Session Variable is not work at the first time I clicked but only the second time.
Here is my code:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
'For the first time I want this session variable to be false because Image Button is not yet click
Me.Session("ImageClick") = False
End If
If Me.Session("ImageClick") Then
'Then after Button Image is clicked I try to add Dynamic control to a div
AddPreviewTable(0)
CreateButtomButton(Me.Session("totalPage"))
Debug.WriteLine(Me.Session("totalPage"))
Me.Session("ImageClick") = False
End If
End sub
The problem is I have to click on ButtonImage two times to turn Me.Session("ImageClick") to True.
Your if block is incorrect. You need to cast your session object into a bool:
If (Session("ImageClick") Is Not Nothing And CBool(Me.Session("ImageClick"))) Then
'Do your stuff.
End If

How to hide a button in .net 2010?

I have a simple web for that has a dropdown list and a button on the form.
The dropdown list is bound to a table in my database and holds three values with idents between 1 and 3 (Weekly, Monthly, Please Select).
I have set my datasource to pull back item 3 (Please Select) as the first item in the dropdown list to prompt the user to select an option. Items 1 & 2 have data assigned to them and this pulls back the relevent data for these options.
Here is my problem. I need to hide my button when the page loads until the user selects item 1 or 2 and would like the button to be hidden of option 3 is selected. I have tried to complete this in my page load event and the code for the dropdown list but i cannot seem to get this to work.
If IsNumeric(DropDownList1.SelectedValue) = 3 Then
btnAddAgendaTemplate.Visible = False
End If
Change the compare to:
If DropDownList1.SelectedValue = "3" Then
btnAddAgendaTemplate.Visible = False
End If
The function IsNumberic just confirm if this is number or not and not convert it to number, and not compare it with the 3. Now the SelectedValue is a string, so direct compare it with the "3", no reason to convert it to number as it is.
Edit: (based on the comment)
In Selected Index changed event for the drop down check for the selectedvalue to be 3 and process accordingly
Suppose your selectedIndexChanged event is like this in your code behind:
Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged
If DropDownList1.SelectedValue.Equals("3") Then
btnAddAgendaTemplate.Visible = False
End If
End Sub
In page load event write following code
If Not Page.IsPostBack() Then
//Set default value in drop down list as 3
End
and
Write selection index changed event of drop down and write following code inside it.
If DropDownList1.SelectedValue = "3" Then
btnAddAgendaTemplate.Visible = False
ELSE
btnAddAgendaTemplate.Visible = True
END

session variable set by checkbox not being stored/shared from page to page

I have a listview that's showing a long list. Each item has the ability to be 'hidden' or not. I want a checkbox to either show all the list, or not to show the hidden ones. The idea is that users will hide the older items they don't want to see any more, but may want to see at some point. I want to store the value of this decision in a session variable so if the user navigates to another page, then comes back, the ShowAllCheckbox will pre-populate to what the user has previously decided. Everything is working good, except i can't get the session variable to keep. It keeps going back to False. This is what I have:
aspx page:
Show Hidden: <asp:Checkbox ID="ShowHiddenCheckbox" runat="server" AutoPostBack="True" OnCheckedChanged="ShowHiddenCheckboxChange" />
...
<asp:ListView ...>
<!-- this list works fine, and pulls the correct records -->
aspx.vb page:
Protected Sub ShowHiddenCheckBoxChange(ByVal sender As Object, ByVal e As EventArgs)
' toggle the values
Dim CheckBoxField As CheckBox = TryCast(sender, CheckBox)
If CheckBoxField.Checked Then
Session("ShowHiddenRotations") = False
Else
Session("ShowHiddenRotations") = True
End If
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'if i navigate to another page, and come back to this one, this comes back as "False". I don't understand how it could be reset to False.
Response.Write( "Session: " & Session("ShowHiddenRotations") )
'when page loads check if session variable has been set to show/hide the hidden rotations
If Session("ShowHiddenRotations") Then
If Session("ShowHiddenRotations") = True Then
'update sql query on select statement to show the hidden rotations
'update checkbox to show it as checked
ShowHiddenCheckBox.Checked = True
Else
ShowHiddenCheckBox.Checked = False
End If
Else
'not checked by default (ie don't show hidden)
ShowHiddenCheckBox.Checked = False
End If
End Sub
The Session variable always reverts back to False when i navigate to another page and come back to this one. My understanding of session variables was that they would pass their values from one page to another until the user closes the browser. Maybe there's another way of doing this, or something simple I'm missing. Any help is much appreciated! Thanks!
Is session-state enabled on your site? It can be disabled in a couple of different way, on a page level or even in web.config.
You should also be aware the Page_Load event fires for every request, before the check-box auto-postback happens.
I'm also a little confused as to what you're trying to store: I assume every row has a check-box, but it seems you're trying to store the set/not-set value in a single session variable. How do you differentiate which have been selected, and which ones hasn't? :)
Update:
Okay, let's try a clean the code up a little bit. First create a property to access the session value:
Private Property ShowHiddenRotations As Boolean
Get
If Not Session("ShowHiddenRotations") Is Nothing Then
Return CType(Session("ShowHiddenRotations"), Boolean)
Else
Return False
End If
End Get
Set(value As Boolean)
Session("ShowHiddenRotations") = value
End Set
End Property
If you're using that value on other pages, I would recommend moving it to a seperate class.
Then we can reduce your other code to something closer to this:
Protected Sub ShowHiddenCheckBoxChange(ByVal sender As Object, ByVal e As EventArgs)
ShowHiddenRotations = ShowHiddenCheckbox.Checked
End Sub
And ...
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If not Page.IsPostBack
' Load your data, better stick it in a seperate sub...
ShowHiddenCheckBox.Checked = ShowHiddenRotations
else
' This section is executed BEFORE any control methods are run, i.e. ShowHiddenCheckBoxChange
end if
End Sub
I'm guessing your problem is really just the order of how things are called in your page. What happens when you debug through it?

Reset values inside a repeater

I have a repeater which displays products. Users can select a Size - dropdownlist and an Amount - textbox. Then they press Order or Cancel. When they press Cancel I would like the values of Size and Amount to return to their default values.
Protected Sub lbtnCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lbtnCancel.Click
rpt.ProductList is the repeater name
lblFeedback.Text = ("")
lblFeedback.ForeColor = Drawing.Color.Black
End Sub
Any help is welcome!
I am using VB.NET.
What if you rebind your repeater on the Cancel Click, so it will again be populated with your Default Values ?
Use the rptProductList_ItemCommand event to catch the repeateritem line and play with the controls as you please ...

Resources