Using Hidden Fields in an ItemCreated handler - asp.net

I'm making a large data entry web form using a DataList.
I have three sets of questions that I store some info about in ASP HiddenFields. I want to color the back of these data list items based on the values of those HiddenFields, but in the ItemCreated event my DataListItemEventArgs are all Nothing or Empty.
I had this working using ItemDataBound, but my rows would lose color upon postback.
This is what I was using in ItemDataBound:
Protected Sub CategoryColors(ByVal sender As Object, ByVal e As DataListItemEventArgs) Handles TheTable.ItemDataBound
Dim v = CType(e.Item.FindControl("CtType"), HiddenField).Value
Try
If v = 1 Then
e.Item.BackColor = Drawing.Color.LightBlue
ElseIf v = 2 Then
e.Item.BackColor = Drawing.Color.Khaki
ElseIf v = 3 Then
e.Item.BackColor = Drawing.Color.LightGreen
Else
e.Item.BackColor = Drawing.Color.Red
End If
Catch ex As Exception
End Try
End Sub
Copying the body of that sub to the ItemCreated handler doesn't do anything. Can I do this same thing a different way? Thanks much for your help.

Related

Maintain Tab index after post back with textchanged

I have 3 textboxes that have AutoPostBack = true. On postback it's losing it's focus. I've been searching and tried many things but nothing is working.
The code below is a snippet of what i'm trying to do.
basically this first snippet isn't grabbing anything. Does anyone know what i'm doing wrong here
**Side note I'm not using Update Panel i'm using LayoutItemNestedControlContainer
Dim ctrl = From control In wcICausedPostBack.Parent.Controls.OfType(Of WebControl)()
Where control.TabIndex > indx
Select control
Protected Sub txtDEPTH_TextChanged(sender As Object, e As EventArgs) Handles txtDEPTH.TextChanged
UpdateProductTemp()
txtDEPTH.Focus()
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Page.IsPostBack Then
Dim wcICausedPostBack As WebControl = CType(GetControlThatCausedPostBack(TryCast(sender, Page)), WebControl)
Dim indx As Integer = wcICausedPostBack.TabIndex
Dim ctrl = From control In wcICausedPostBack.Parent.Controls.OfType(Of WebControl)()
Where control.TabIndex > indx
Select control
ctrl.DefaultIfEmpty(wcICausedPostBack).First().Focus()
End If
End Sub
Protected Function GetControlThatCausedPostBack(ByVal page As Page) As Control
Dim control As WebControl = Nothing
Dim ctrlname As String = page.Request.Params.[Get]("__EVENTTARGET")
If ctrlname IsNot Nothing AndAlso ctrlname <> String.Empty Then
control = page.FindControl(ctrlname)
Else
For Each ctl As String In page.Request.Form
Dim c As Control = page.FindControl(ctl)
If TypeOf c Is TextBox OrElse TypeOf c Is DropDownList Then
control = c
Exit For
End If
Next
End If
Return control
End Function
Which version are you using? Because, when I try to set focus() of a Textbox, the compiler gives me an error, because there is no such method. However, HTML provides an attribute, which auto focuses an focusable element.
Solution:
TextBoxToSetFocus.Attributes.Add("autofocus", "")
Regards,
Maheshvara

How can do postback using response.redirect?

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
' hdncurrpayperiod.Value = test1.ToString
hdnPayperiod.Value = test1.ToString
lblPayPeriodStartDt.Text = test1
Else
lblPayPeriodStartDt.Text = hdnPayperiod.Value
End If
End Sub
Private Sub btnnext_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnnext.Click
' hdnPayPeriodStartDt.Value = ""
Dim datenext As Date
Dim datenextpayperiod As String
datenext = DateTime.Parse(lblPayPeriodStartDt.Text)
datenextpayperiod = datenext.Add(New TimeSpan(14, 0, 0, 0)).ToString("MM/dd/yyyy")
hdnPayperiod.Value = datenextpayperiod
lblPayPeriodStartDt.Text = hdnPayperiod.Value
'hdnPayperiod.Value = lblPayPeriodStartDt.Text.ToString
Response.Redirect("TimeSystem.aspx?PayPeriodStartDate=" + HttpUtility.UrlEncode(hdnPayperiod.Value), False)
End Sub
This is a user control which has calender with next and previous buttons and a label. I get the initial date from the a user control property on initial load(not postback) and on next button click it adds 14 days and displays on label. Everything is fine until I use response.redirect which is hitting not postback and getting the property value again. How can I avoid this. I have to pass the value of date with 14 days added to it in the url but this happens only once as it is refreshing with property value every time It hits response.redirect. Please let me know if I am not clear
In Page_Load in Not IsPostBack block add another 'if' statement that checks if 'PayPeriodStartDate' is present in query string (Request.QueryString). If it is - use that value to set hidden field and label if it isn't use 'test1' value as in your code sample.

Programmatically databind listview while using datapager

I have a listview and two data pagers. My listview is hooked up to a data source whose data is ordered randomly. ORDER BY NEWID()
As you can imagine, each time I select a page or click next/prev page all the data is randomized making the datapager quite useless.
I figured I could set the datasource when the page is not posted back and then programmatically set the datasource to the listview but now when selecting a page or clicking the next/prev buttons, the page just simply doesn't change..
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
Dim dv As DataView = sdsMembres.Select(DataSourceSelectArguments.Empty)
lvListMembres.DataSource = dv
lvListMembres.DataBind()
DataPager2.PagedControlID = "lvListMembres"
DataPager3.PagedControlID = "lvListMembres"
DataPager2.DataBind()
DataPager3.DataBind()
End If
End Sub
What am I missing / is there a better way to do this. The order by MUST be random and I MUST have the data pagers separate from the listview.
Thank you!
Take the code between the If statement and place it in a separate function.
Add a method of storing the data, in this particular case I'll store it as a DataTable in a session.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
' Clear datatable from session
Session("CourtierList_dt") = Nothing
' Set datasource to listview and save in a session variable
SetCourtierList()
End If
End Sub
Protected Sub SetCourtierList()
Dim dt As DataTable
If Session("CourtierList_dt") Is Nothing Then
Dim dv As DataView = sdsMembres.Select(DataSourceSelectArguments.Empty)
Session("CourtierList_dt") = dv.ToTable
End If
dt = Session("CourtierList_dt")
lvListMembres.DataSource = dt
lvListMembres.DataBind()
DataPager2.PagedControlID = "lvListMembres"
DataPager3.PagedControlID = "lvListMembres"
DataPager2.DataBind()
DataPager3.DataBind()
End Sub
Also, make sure to add the PagePropertiesChanging event to the listview
Protected Sub lvListMembres_PagePropertiesChanging(sender As Object, e As System.Web.UI.WebControls.PagePropertiesChangingEventArgs) Handles lvListMembres.PagePropertiesChanging
DataPager2.SetPageProperties(e.StartRowIndex, e.MaximumRows, False)
DataPager3.SetPageProperties(e.StartRowIndex, e.MaximumRows, False)
SetCourtierList()
End Sub
And there you have it, hope this is helpful for someone.

Items.Count returning 0 for a list box

I am trying to use the code below to store the items from the list into a session. For some reason when I debug the code the count is returning 0 even though there are multiple items in the list box? Any ideas what I am doing wrong here?
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
NameTextBox_AutoCompleteExtender.OnClientItemSelected = "getSelected"
End Sub
Protected Sub cmdNext_Click(sender As Object, e As System.Web.UI.ImageClickEventArgs) Handles cmdNext.Click
Dim n As Integer = NPListbox.Items.Count
Dim arr As String() = New String(n - 1) {}
For i As Integer = 0 To arr.Length - 1
arr(i) = NPListbox.Items(i).ToString()
Next
Session("arr") = arr
Response.Redirect("~/frmDescription.aspx")
End Sub
<script language="javascript" type="text/javascript">
function getSelected(source, eventArgs) {
var s = $get("<%=NameTextBox.ClientID %>").value;
var opt = document.createElement("option");
opt.text = s.substring(s.length - 10);
opt.value = s.substring(s.length - 10);
document.getElementById('<%= NPListbox.ClientID %>').options.add(opt);
}
I am going to guess that you do not have any logic in your Page_Load to populate the listbox, based upon what it had when you finished the autocomplete extender logic. Since, you do not, then when the click event fires after the Page_Load your values are gone.
Put the logic that executes on selection of the autocomplete extender in a method and have your Page_Load call that, like this:
Protected Sub Page_Load(sender As Object, e As EventArgs)
' Put call here to populate the listbox results from autocomplete extender selection
PopulateListBox()
End Sub
Private Sub PopulateListBox()
' Go to whatever resource you are using to get the values for the list box
End Sub
UPDATE:
Since you are depending upon using a client-side function to grab the values from the autocomplete extender and populating the listbox that way, you need to mimic that logic in your Page_Load on the server-side, because it will be too late if you try to use the client-side one, since you need the data server-side and all of the server-side events happen before the client-side logic in a server post back.
You need to do something like this:
Protected Sub Page_Load(sender As Object, e As EventArgs)
' Only do this when page has posted back to the server, not the first load of the page
If IsPostBack Then
' Put call here to populate the listbox results from autocomplete extender selection
PopulateListBox()
End If
End Sub
Private Sub PopulateListBox()
' Get value from text box
Dim textBoxValue As String = Me.NameTextBox.Text
' Create new item to add to list box
Dim newItem As New ListItem(textBoxValue)
' Add item to list box and set selected index
NPListbox.Items.Add(newItem)
NPListbox.SelectedIndex = NPListbox.Items.Count - 1
End Sub

Creating buttons dynamically in ListView

I have a following problem. I have a ListView which returns data from SQL table. One of its columns looks like "Ambient/Trance/Goa Trance/House".
All i want to do is parse this column and create buttons for each value, for example a button for "Ambient", a button for "Trance", etc.
I tried to create buttons in ItemDataBound event in the following way:
Dim ListView_Albums_PlaceHolder_Artists As PlaceHolder = e.Item.FindControl("ListView_Albums_PlaceHolder_Artists")
Dim Artists As String() = e.Item.DataItem("album_artists").ToString.Split("/")
Dim ArtistsN As String() = e.Item.DataItem("album_artists_n").ToString.Split("/")
Dim ListView_Albums_Literal_Artists As New Literal
If Artists.Length = 1 Then
ListView_Albums_Literal_Artists.Text = "Artist: "
Else
ListView_Albums_Literal_Artists.Text = "Artists: "
End If
ListView_Albums_PlaceHolder_Artists.Controls.Add(ListView_Albums_Literal_Artists)
For Integer1 As Integer = 0 To Artists.Length - 1
Dim ListView_Albums_LinkButton_Artist As New LinkButton
ListView_Albums_LinkButton_Artist.Text = ArtistsN(Integer1)
ListView_Albums_LinkButton_Artist.CommandName = "Artist"
ListView_Albums_LinkButton_Artist.CommandArgument = Artists(Integer1)
ListView_Albums_LinkButton_Artist.CssClass = "a-03"
ListView_Albums_PlaceHolder_Artists.Controls.Add(ListView_Albums_LinkButton_Artist)
Dim ListView_Albums_Literal As New Literal
ListView_Albums_Literal.Text = ", "
If Not Integer1 = Artists.Length - 1 Then
ListView_Albums_PlaceHolder_Artists.Controls.Add(ListView_Albums_Literal)
End If
Next
They created fine but they didn't work at all. I tried to Add Handler for Click or Command event but it also didn't help.
Please help me to solve my problem!
Edit:
As VinayC suggested I changed ItemDataBound to ItemCreated. That helped, but I faced another problem: as far as I understand e.Item.DataItem or, maybe, e.Item becomes Nothing on PostBacks so the buttons do not work.
How to solve that problem? Thanks once again!
I believe that buttons must be getting created late in page life cycle and hence not responding to events.
You may want to try moving your code in ItemCreated event and use ListView's ItemCommand event to trap these. Yet another suggestion is to assign (different) ID to your link buttons - for example
ListView_Albums_LinkButton_Artist.ID = "A" & Artists(Integer1)
In case, you want to attach an click event handler directly to buttons then ID is must.
So, I solved my problem. The solution wasn't simple but here it is:
In ItemCreated event I firstly count the number of buttons, then save it to ViewState, and only then I create buttons. I had to save the number of buttons to ViewState because on every postback e.Item.DataItem becomes Nothing.
Maybe there is a simplier solution but I found only that one...
Sub OnItemCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ListViewItemEventArgs)
Dim ListView_Albums_PlaceHolder_Artists As PlaceHolder = e.Item.FindControl("ListView_Albums_PlaceHolder_Artists")
If Not ListView_Albums_PlaceHolder_Artists Is Nothing Then
If Not e.Item.DataItem Is Nothing Then
ViewState("Length") = e.Item.DataItem("album_artists").ToString.Split("/").Length
End If
If Not ViewState("Length") Is Nothing Then
Dim Length As Integer = ViewState("Length")
For Integer1 As Integer = 0 To Length - 1
Dim ListView_Albums_LinkButton_Artist As New LinkButton
ListView_Albums_LinkButton_Artist.ID = "ListView_Albums_LinkButton_Artist_" & Integer1
ListView_Albums_PlaceHolder_Artists.Controls.Add(ListView_Albums_LinkButton_Artist)
Next
End If
End If
End Sub
Sub OnItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ListViewItemEventArgs)
Dim ListView_Albums_PlaceHolder_Artists As PlaceHolder = e.Item.FindControl("ListView_Albums_PlaceHolder_Artists")
If Not ListView_Albums_PlaceHolder_Artists Is Nothing Then
If Not e.Item.DataItem Is Nothing Then
Dim Artists As String() = e.Item.DataItem("album_artists").ToString.Split("/")
Dim Artists_N As String() = e.Item.DataItem("album_artists_n").ToString.Split("/")
For Integer1 As Integer = 0 To Artists.Length - 1
Dim ListView_Albums_LinkButton_Artist As LinkButton = e.Item.FindControl("ListView_Albums_LinkButton_Artist_" & Integer1)
ListView_Albums_LinkButton_Artist.CommandArgument = Artists(Integer1)
ListView_Albums_LinkButton_Artist.Text = Artists_N(Integer1)
ListView_Albums_LinkButton_Artist.CssClass = "a-03"
Next
End If
End If
End Sub

Resources