How to hide a button in .net 2010? - asp.net

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

Related

Dynamically show/hide columns in Gridview in ASP.NET

I'm attempting to show/hide columns in gridview for better viewing.
What I need to do is:
hide columns 8, 9 and 10 on page load
show them upon button click
I successfully hidden them on pageload using RowCreated Event (code below). but as of now, I haven't yet found a way to show them again via button click.
Protected Sub GridView1_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowCreated
Dim row As GridViewRow = e.Row
' Intitialize TableCell list
Dim columns As New List(Of TableCell)()
For Each column As DataControlField In GridView1.Columns
'Get the first Cell /Column
Dim cell As TableCell = row.Cells(0)
' Then Remove it after
row.Cells.Remove(cell)
'And Add it to the List Collections
columns.Add(cell)
Next
' Add cells
row.Cells.AddRange(columns.ToArray())
e.Row.Cells(8).Visible = False
e.Row.Cells(9).Visible = False
e.Row.Cells(10).Visible = False
e.Row.Cells(11).Visible = False
End Sub
I tried the following methods with unfortunate results:
Set width to 0px then reset to auto upon click- since most of my columns are itemfields that contains either button or checkbox, this doesn't work at all
Use GridView1.Columns(8).Visible = False - same reason as above
Create RowDataBound Event with e.Row.Cells(8).Visible = True but i can't successfully call this event via button click yet.
Please advise. Thanks in advance.
DataGridView1.Columns(n).Visible = False ' working in vb.net
where n represent the column index

Set drop down list to start at something different than populated values

Been googling for a while now and cant see this answer anywhere.
Using ASP.NET (VB) in VS2013.
I have a drop down list with 4 preset values (see below)
<asp:DropDownList ID="TimeTextMins" runat="server" style="font-family:'Century Gothic'; font-size:12pt;">
<asp:ListItem Value="00"></asp:ListItem>
<asp:ListItem Value="15"></asp:ListItem>
<asp:ListItem Value="30"></asp:ListItem>
<asp:ListItem Value="45"></asp:ListItem>
</asp:DropDownList>
The values represent minutes in 15 minute splits.
When the page loads I want to put the current time in minutes in this drop down, the user can then change to a 15 minute segment if required. I know how to get the current minute in VB nut no idea how to set the DDL to it. Using selectedvalue doesn't work and neither does .text.
Cheers
Since a dropdown can only be set to values that are in the list, you will need to dynamically add the current minute to the dropdown values in the page Load event:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
Dim currentMinute = Date.Now.Minute
Dim currentMinuteString = CStr(currentMinute).PadLeft(2, "0"c)
If currentMinute Mod 15 = 0 Then
Me.TimeTextMins.SelectedValue = currentMinuteString
Else
Dim position = (currentMinute \ 15) + 1
Me.TimeTextMins.Items.Insert(position, currentMinuteString)
Me.TimeTextMins.SelectedValue = currentMinuteString
End If
End If
End Sub
Update
Based on your comment on the question that you only want the 15 minute intervals in the dropdown, I don't know that you can do that with the standard drop down list control, but there are 3rd party HTML controls that are more combobox like. What you could do is remove the added item when the selection is changed:
Private Sub TimeTextMins_SelectedIndexChanged(sender As Object, e As EventArgs) Handles TimeTextMins.SelectedIndexChanged
Dim itemsToRemove =
Me.TimeTextMins.Items.Cast(Of ListItem)() _
.Where(Function(x) x IsNot Me.TimeTextMins.SelectedItem AndAlso CInt(x.Value) Mod 15 <> 0) _
.ToList()
For Each item In itemsToRemove
Me.TimeTextMins.Items.Remove(item)
Next
End Sub
You will need to add AutoPostBack="true" to your asp:DropDownList tag.
You can do it using standard drop down and you can hide current time item when list is expanded, without postback. You have to write js function and assign it to onmousedown event and do a little trick.
First set your calculated current time as 'Text' property of first item on the list (leaving it's 'value' as '00') in code behind on Page_Load.
When page loads you will see current time as selected time in dropdown. Now in js function assigned to onmousedown you simply set innerText property of first option to '00':
You need flag variable to do it only once:
var flag = 0;
function dropDownList_MouseDown()
{
if (flag == 0)
{
var list = document.getElementById('<%= DropDownList1.ClientID %>');
list.options[0].innerText = '00';
flag =1;
}
}

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

IF SelectedItem.Equals ??? Then redirect Statement

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"

Update databound value on a label when MultiView Active View Changed?

I have a MultiView with several Views within it. When the ActiveView changes I want to selectively databind a label that is in one of the views. Ideally, I don't want to do it every time the ActiveView changes, rather only when it is actually the view containing the label that is active. I tried something like this:
Private Sub MultiView1_ActiveViewChanged(sender as Object, e as System.EventArgs) Handles MultiView1.ActiveViewChanged
Dim varView as String = MultiView1.GetActiveView.ToString
If varView = "vwExisting" Then
' Code to update label here with latest databind.
End If
End Sub
Now, MultiView1.GetActiveView.ToString doesn't return the value I'm looking for, does anyone know what will?
The GetActiveView Method returns a View Class, not a string. Try this...
If MultiView1.GetActiveView.ID = "vwExisting" Then
End If

Resources