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
Related
I have a Repeater of checkboxes bound in code-behind:
<asp:Repeater runat="server" ID="rptOpenJobs" OnItemDataBound="rptOpenJobs_ItemDataBound">
<ItemTemplate>
<asp:CheckBoxList ID="lstOpen" runat="server"></asp:CheckBoxList>
</ContentTemplate>
</asp:Repeater>
Code-behind (VB.Net):
Protected Sub rptOpenJobs_ItemDataBound(sender As Object, e As RepeaterItemEventArgs)
Dim jobsTable As New DataTable
Dim conn As String = "[hidden]" 'Connection String to retrieve data table values
If e.Item.ItemType.Equals(ListItemType.AlternatingItem) Or e.Item.ItemType.Equals(ListItemType.Item) Then
Dim cboxlist As CheckBoxList = e.Item.FindControl("lstOpen")
If cboxlist.Items.Count = 0 Then
cboxlist.DataSource = jobsTable
cboxlist.DataTextField = "job_title"
cboxlist.DataValueField = "job_title"
cboxlist.DataBind()
End If
End If
End Sub
On form submission, the checked boxes do not stay checked. I've looked for solutions but haven't found any where the list is databound in code-behind.
I've tried adding an UpdatePanel around the repeater and around the CheckBoxList but the checked state still will not persist on form submission.
What does you page load event look like?
You ONLY load things up the first time in the If IsPostBack = False code stub.
99% if not 100% of your pages will ahve that code block. For me, comming from say Access, VB6, vb.net? Well a form loads, and we have the form load event.
but, any old button click on that page ALSO triggers a post back, and the on load event will run EVERY time (and then the button click or whatever event code will run).
So, all web pages look like this:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
' the REAL first time load event!!!
LoadGrid()
End If
End Sub
Sub LoadGrid()
Using cmdSQL As New SqlCommand("SELECT ID, FirstName, LastName, HotelName, City from tblHotels",
New SqlConnection(My.Settings.TEST3))
cmdSQL.Connection.Open()
GridView1.DataSource = cmdSQL.ExecuteReader
GridView1.DataBind()
End Using
End Sub
So note how we have that first page load event - we load up a grid. But if we do NOT put that code inside of of the IsPostBack = False part, then our setup code and our load code will run every time on a full page post back.
Your check boxes as you have should persist for you, and should survive a page post back.
I don't see where (how) you persisted the jobs table, but ONCE the repeater is loaded, they should survive a post back. I mean, the data bound event only triggers if you are re-binding the repeater. but, if you are say due to adding new rows or some such, and you do trigger (need/want) a databind event again, then that jobsTable does need to exist and be persisted if you do re-bind the repeater.
So, in your code, you have on page load, we load up the Repeater. Above code would be the same, eg this:
And since the table is used OVER and OVER for each item data bound event, then we should NOT re-pull the table each time in the item data bound event. We should setup (load) that table one time in our LoadGrid routine.
So, at the page level class, we will declarer a table variable scoped to the form/page level. This will persist long enough during each item bind event for the repeater to fill out the check box list.
We assume that you have a data source for hte repeter - it will repeat many times. And for EACH repeater, we have a checkbox list that ALSO has a set of values that we want to fill from the check box list of choices.
It is NOT 100% clear if each new check box list is to be driven from ONE table, and each repeated row of course does have a value from the table for each row of the repeating list that represents the chosen check box value.
Or, is the list of check box values for each repeated item different? This issue has to be cleared up.
Clear this last issue up for me, and I post more code as to how this can work.
Sub LoadGrid()
Using cmdSQL As New SqlCommand("SELECT ID, FirstName, LastName, HotelName, City from tblHotels",
New SqlConnection(My.Settings.TEST3))
cmdSQL.Connection.Open()
rptOpenJobs.DataSource = cmdSQL.ExecuteReader
rptOpenJobs.DataBind()
End Using
End Sub
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
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"
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim lab As Label = Me.GridView1.FindControl("Label1")
If TextBox2.Text = "7" Then
GridView1.SelectedRow.Cells(2).Text = "500"
Else
GridView1.SelectedRow.Cells(2).Text = "950"
End If
End Sub
The following error occurs : Object reference not set to an instance of an object.
You've got this code in your Page Load event, so it will run when the page is first loaded, and on every single postback. This probably isn't what you want.
I imagine that on the very first load, there isn't a selected row in your GridView, so GridView1.SelectedRow is going to be null. If this isn't null, then Cells or Cells(2) definitely will be. Trying to access a property on null is going to throw a NullReferenceException - "Object reference not set to an instance of an object".
As this MSDN example shows, you're probably better off accessing the SelectedRow property in an event handler for the SelectedIndexChanged event of the GridView.
Dim lab As Label = Me.GridView1.FindControl("Label1")
It doesn't look like you're doing anything with this label. Put a breakpoint on that line and see if it finds it. If it doesn't and you don't even use it, take the line out.
Also, check if textbox2 is valid whilst debugging.
I have a custom control that inherits from .NET's CompositeControl class. This control overrides the CreateChildControls in order to build its child controls dynamically. I need the page to post back after a couple different javascript events occur on the client side.
In order to accomplish this, I create two hidden controls on the page so I can set their values with javascript, submit the page, and read the values out on server side. Here's is the code I use to create these two hiddens:
Protected Overrides Sub CreateChildControls()
hdEventName = New HiddenField()
Controls.Add(hdEventName)
hdEventName.ID = "hdEventName"
hdEventArgs = New HiddenField()
Controls.Add(hdEventArgs)
hdEventArgs.ID = "hdEventValue"
' other controls
' ...
End Sub
When a javascript event occurs I set the value attribute of the two hiddens and submit the page, like so:
hdEventName.value = 'EventName';
hdEventArgs.value = 'arg1,arg2';
document.forms[0].submit();
In the OnLoad method of my control, I attempt to check the Value property of the hdEventName and hdEventArgs controls, but it is always empty. However, Page.Request.Form(hdEventName.UniqueID) and Page.Request.Form(hdEventArgs.UniqueID) return correct values. The actual HTML in the markup also shows correct values after the page posts back.
Why is the Value property of the HtmlInputHiddens disconnected from the actual value that appears on the client?
Update
It appears that a control's properties get loaded from the form sometime after OnLoad occurs. Thus I was able to solve my problem by either moving the code that checks the two hidden fields into the OnPreRender method, or adding the following method to my code -
Private Sub Event_Handler(ByVal sender As Object, ByVal e As EventArgs)
Handles hdEventName.ValueChanged
' do stuff with hiddens
' ...
' reset the values back
hdEventName.Value = String.Empty
hdEventArgs.Value = String.Empty
End Sub
when the page posts back there's nothing to link the variable hdEventName to the control you previously created. what you're doing is akin to having an integer declared at the class level and setting it to 5 when you're creating child controls. there's nothing to maintain that value in that variable across postbacks.
if you want to get a reference to the control you created previously, you'd have to use
hdEventName = CType(Page.FindControl("hdEventName") , HiddenField)
(i'm guessing at this) or Request if you're only concerned with the value.
It appears that a control's properties get loaded from the form sometime after OnLoad occurs. Thus I was able to solve my problem by either moving the code that checks the two hidden fields into the OnPreRender method, or adding the following method to my code -
Private Sub Event_Handler(ByVal sender As Object, ByVal e As EventArgs)
Handles hdEventName.ValueChanged
' do stuff with hiddens
' ...
' reset the values back
hdEventName.Value = String.Empty
hdEventArgs.Value = String.Empty
End Sub