items of listbox losing styles after postback - asp.net

I have a ListBox. When I bind data I need to distinguish some items by color. There is how I do it
Protected Sub ListBox_DataBound(ByVal sender As Object, ByVal e As EventArgs)
Dim lb As ListBox = CType(sender, ListBox)
For Each li As ListItem In lb.Items
If someFlag = True Then
li.Attributes("style") += "color: red;"
End If
Next
End Sub
But when I do postback these styles is lost. How I can save style between postback?
EDIT: Inside Page_Load I don't do anything with this listbox. I fill listbox when occur event selectIndexChange for ddl. but when I click on linkbutton
Private Sub lnkbtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lnkBtn.Click
Dim selectedItems As ArrayList = New ArrayList
For Each itm As ListItem In listbox1.Items
If itm.Selected = True Then
selectedItems.Add(itm)
End If
Next
For i As Int32 = 0 To selectedItems.Count - 1
listbox1.Items.Remove(selectedItems(i))
listbox2.Items.Add(selectedItems(i))
Next
End Sub
after this event items of listbox1 lost styles

Related

Delete selected gridview row

trying to delete selected gridview row. I read few sites and used code however I am getting error. I think because most of the codes I've seen they using DataGridView, This code is within delete button event
For i = 0 To myGridView.Rows.Count - 1 Step i + 1
Dim delrow As GridViewRow = myGridView.Rows(i)
If delrow.Selected = True Then
myGridView.Rows.RemoveAt(i)
End If
Next
the two errors I am getting are:
1) RemoveAt is not a member of GridViewRowCollection
2) Selected is not a member of GridViewRow
Your help will be highly appreciated!
I usually delete rows on GridViews putting an ImageButton (with a trash bin) on every row with a Template Field, then on RowDataBound event I associate "DeleteRow" event and rowIndex to the CommandName and CommandArgument
Protected Sub myGridView_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
Dim ib As ImageButton = e.Row.FindControl("ibDeleteRow")
ib.CommandName = "DeleteRow"
ib.CommandArgument = e.Row.RowIndex
End if
end sub
Private Sub myGridView_RowCommand(sender As Object, e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand
If e.CommandName = "DeleteRow" Then
Dim rowToDeleteIndex as integer = e.CommandArgument
myGridView.DeleteRow(rowToDeleteIndex)
end if
end sub
Private Sub myGridView_RowDeleting(sender As Object, e As GridViewDeleteEventArgs) Handles GridView1.RowDeleting
End Sub

How do i give a value a TextBox create dynamically when i click and doubleClick? vb.net

I have text box create like that:
Dim Result1 As New TextBox
Result1.ID = "BOX_Result" & a & "_" & i
I want when i click on that textbox to write "OK" and when i double click in cell to put NOT/OK
Important! The TextBox is created Dynamically if i try Result.Click doesn't work, get ne that error: "Result1.Click display error: "Click is not an event of 'System.Web.UI.WebControls.TextBox' "
I try like that but doesn't work:
AddHandler Result1.Click, AddressOf Me.Result1_Click
Private Sub Result1_Click(ByVal sender As Object, _
ByVal e As System.EventArgs)
Result1.Text = "OK"
End Sub
I want when a person click on that textbox created dynamically but doesn't work click. Thanks for help
You can add these lines to your TextBox definition:
Result1.Attributes.Add("onclick", "this.value = 'OK';")
Result1.Attributes.Add("ondblclick", "this.value = 'NOT/OK';")
In this code, the text "NOT/OK" is displayed when the user double-clicks in the TextBox. In your question, you talk about a double-click "in cell". If that "cell" is not the TextBox, please give some indication of what kind of control it is.
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim tb As New TextBox 'Create the new TextBox
AddHandler tb.DoubleClick, AddressOf TB_DoubleClick 'Add a handler to the textbox`s DoubleClick event
AddHandler tb.Click, AddressOf TB_Click
'Set any other properties of textbox you want here....
Me.Controls.Add(tb) 'Add the textbox to the forms controls
End Sub
'This is the textbox Click event handler sub
Private Sub TB_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim tb As TextBox = DirectCast(sender, TextBox) 'Cast the (sender) into a textbox to get access to the textbox`s properties
Result1.Text = "OK"
End Sub
'This is the textbox DoubleClick event handler sub
Private Sub TB_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs)
Dim tb As TextBox = DirectCast(sender, TextBox) 'Cast the (sender) into a textbox to get access to the textbox`s properties
Result1.Text = "NOT OK"
End Sub
I have create a simple exemple for you :
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim Result1 As New TextBox
Result1.Text = "BOX_Result"
Dim loc As New Point With {.Y = 117, .X = 111}
Result1.Location = loc
Me.Controls.Add(Result1)
AddHandler Result1.Click, AddressOf Me.Result1_Click
End Sub
Private Sub Result1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim txt As TextBox = sender
sender.Text = "OK"
End Sub
End Class
Hope that you want

Access Individual Row Template in GridView

I have a ListBox (named ListBoxProperty) and button (btnDeselectAll) in a TemplateField on a gridview control that shows on every row of the gridview. The code shown below is the code behind the button control in the TemplateField. This code works on every ListBox on every row in the gridview. I would like it to only affect the ListBox in the gridview row from which it was clicked. I've tried a few things with failure so I didn't include my failings - just the portion that almost gives me what I want. Couldn't include a pic alas my reputation is weak.
Protected Sub btnDeselectAll_Click(sender As Object, e As System.EventArgs)
For Each Row As GridViewRow In GridView1.Rows
Dim myListBox As ListBox = _
CType(Row.FindControl("ListBoxProperty"), ListBox)
For Each selectedItem As ListItem In myListBox.Items
selectedItem.Selected = False
Next
Next
End Sub
The NamingContainer of the button is the GridViewRow, there you are.
Protected Sub btnDeselectAll_Click(sender As Object, e As System.EventArgs)
Dim btn = DirectCast(sender, Button)
Dim row = DirectCast(btn.NamingContainer, GridViewRow)
Dim myListBox = DirectCast(row.FindControl("ListBoxProperty"), ListBox)
For Each selectedItem As ListItem In myListBox.Items
selectedItem.Selected = False
Next
End Sub

Dynamic link button not firing in update panel

I'm dynamically adding a link button in the footer of a grid view. The grid view is wrapped in an update panel. I can get an async post back (I can tell by seeing an update progress flash), but I can't get the debug point in my click function to fire.
Private Sub gvParts_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvParts.RowDataBound
ElseIf e.Row.RowType = DataControlRowType.Footer Then
If _showPrice Then
Dim clearbutton As New LinkButton
clearbutton.ID = "btnClearCart"
clearbutton.Text = "Remove All"
ScriptManager1.RegisterAsyncPostBackControl(clearbutton)
e.Row.Cells(7).Controls.Add(clearbutton)
AddHandler clearbutton.Command, AddressOf clearButton_click
End If
End If
Private Sub clearButton_click(ByVal sender As Object, ByVal e As System.EventArgs)
ClearCart()
End Sub
try this
<dl>
Private Sub gvParts_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvParts.RowDataBound
ElseIf e.Row.RowType = DataControlRowType.Footer Then
If _showPrice Then
Dim clearbutton As New LinkButton
clearbutton.ID = "btnClearCart"
clearbutton.Text = "Remove All"
ScriptManager1.RegisterAsyncPostBackControl(clearbutton)
e.Row.Cells(7).Controls.Add(clearbutton)
AddHandler clearbutton.Command, AddressOf clearButton_click
ScriptManager.GetCurrent(Me).RegisterAsyncPostBackControl(clearbutton)
End If
End If
Sorry,It's my mistake I have posted wrong code.Place the above code on OnRowCreated event of gridview
try this
Private Sub gvParts_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvParts.RowDataBound
ElseIf e.Row.RowType = DataControlRowType.Footer Then
If _showPrice Then
Dim clearbutton As New LinkButton
clearbutton.ID = "btnClearCart"
clearbutton.Text = "Remove All"
ScriptManager1.RegisterAsyncPostBackControl(clearbutton)
e.Row.Cells(7).Controls.Add(clearbutton)
AddHandler clearbutton.Command, AddressOf clearButton_click
ScriptManager.GetCurrent(Me).RegisterAsyncPostBackControl(clearbutton)
End If
End If
The controls have to be added to your Controls collection prior to the page_load event. The default databinding (which fires the OnRowCreated, OnRowDataBound events) happens during the OnLoad event. Try moving your databinding code to the Page_Init function. Depending on what your databinding code looks like, this might mean you will have to implement the databinding "manually" (ie. Set the datasource and call .DataBind() in code)

listbox selected items into textbox not working

my vb.net will not use listbox1.selecteditems it always comes up with a blue line underneath even though when i search online everyone is using this.
my goal is to get the selected items and list them in a textbox
Protected Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim li As ListItem
For Each li In ListBox1.Items
If li.Selected Then
TextBox1.Text &= li.Text & vbCrLf
End If
UpdatePanel2.Update()
Next
End Sub
End Class
To determine the selection in a multi-selection list control
Loop through the control's Items collection and test the Selected property of every individual item.
For Each li In ListBox1.Items
If li.Selected Then
TextBox1.Text &= li.Text & vbCrLf
End If
Next
MSDN: To determine the selection in a multi-selection list control
SelectedItems is not available in ASP.Net, this property exists only for Winforms-Listbox Controls.
I think the problem here is that you're binding to the control immediately prior to trying to retrieve a selected value from it. When the control is initially bound to the datasource it won't have any selected items.
You need to split it out so that you bind the listbox when the page is loaded, then the user selects some things in that box, clicks your Button2, and the value of the textbox is updated on postback.
First, on page load:
Protected Sub Page_Load(object sender, EventArgs, e)
listcmd.Connection = conn1
conn1.Open()
listcmd.CommandText = "SELECT distinct B603SalesAsOFMASTER.SDESCR FROM B603SalesAsOFMASTER"
listda.Fill(saolist, "listboxtext")
Dim dt As DataTable = saolist.Tables("listboxtext")
ListBox1.DataSource = dt
ListBox1.DataValueField = "SDESCR"
ListBox1.DataMember = "SDESCR"
ListBox1.DataBind()
conn1.Close()
End Sub
Then, this code is run when the user clicks Button2
Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
For i As Integer = 0 To ListBox1.SelectedItems.Count - 1
TextBox1.Text &= DirectCast(ListBox1.SelectedItems(i), DataRowView)(1).ToString & vbCrLf
Next
CheckBox1.Visible = True
TextBox1.Visible = True
End Sub
To view (Text Item in textbox)
textbox3.Text = listBox1.GetItemText(listBox1.SelectedItem);

Resources