Get value of listitem on postback - asp.net

I have two listitems and the postback and run a function.
<asp:RadioButtonList runat="server" CssClass="ccsw" ID="ccsw" AutoPostBack="true" RepeatDirection="Horizontal" OnSelectedIndexChanged="UpdateCharges">
<asp:ListItem Text="Credit Card"></asp:ListItem>
<asp:ListItem Text="Debit Card"></asp:ListItem>
</asp:RadioButtonList>
And it runs the function UpdateCharges
Sub UpdateCharges(ByVal sender As Object, ByVal e As System.EventArgs)
If ccsw_1.Checked Then
lblPayText.Text = "Payment Amount = £" & Session("strTotal_DebtCharge")
Else
lblPayText.Text = "Payment Amount = £" & Session("strTotal_Debt")
End If
End Sub
I need to find out which one is checked and then change the text of a label depending on that.
The code I have doesn't work i don't think .Checked works in this instance.
Any idea of how to get the value or text of the listitem?
Thanks

If Me.ccsw.SelectedIndex = 0 Then
lblPayText.Text = "Payment Amount = £" & Session("strTotal_DebtCharge")
Else
lblPayText.Text = "Payment Amount = £" & Session("strTotal_Debt")
End If

Related

Button Stops Functioning when Trying to Assign Session Variable

I am trying to retain a list of strings after a button press and have been using session variables. This is a snippet of the code:
.aspx page
<asp:Panel runat="server" ID="pnlAdd" >
<asp:DropDownList runat="server" ID="ddlOrder" >
<asp:ListItem Value="-1">Select an Order Type</asp:ListItem>
<asp:ListItem Value="1">hey there</asp:ListItem>
</asp:DropDownList>
<asp:DropDownList runat="server" ID="ddlOrderStatus" >
<asp:ListItem Value="-1">Select an Order Status</asp:ListItem>
<asp:ListItem Value="1">another option</asp:ListItem>
</asp:DropDownList>
</asp:Panel>
<br />
<asp:Button runat="server" ID="btnAdd" Text="Add" />
.vb page
Protected Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
Dim list As New List(Of String)
list = Session("flist")
list.Add(ddlOrder.SelectedItem.Text + " " + ddlOrderStatus.SelectedItem.Text)
Session("flist") = list
For i As Integer = 0 To 10
MsgBox(i.ToString + " " + list(i))
Next
MsgBox("Here")
End Sub
At the point that I assign the Session variable to list with list = Session("flist"), nothing will happen on the button click. If I remove that line, the rest of the button fires. Am I using the Session variable incorrectly? How can I achieve this correctly?
The list (of String) cannot be nothing before you are going to add elements into this list, So, change your code as follows:
Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
Dim list As New List(Of String)
Dim objList As Object = Session.Item("flist")
If objList IsNot Nothing AndAlso TypeOf (objList) Is List(Of String) Then
list = CType(objList, List(Of String))
End If
list.Add(ddlOrder.SelectedItem.Text + " " + ddlOrderStatus.SelectedItem.Text)
Session("flist") = list
For i As Integer = 0 To list.Count - 1
MsgBox(i.ToString + " " + list(i))
Next
MsgBox("Here on end")
End Sub

how to get all values of selected checkbox in vb.net and set it on string list

I need to get selected items on checkbox and set as string format like (value1,value2,value3) from the checkbox i selected
For Each row As GridViewRow In GridView1.Rows
If row.RowType = DataControlRowType.DataRow Then
Dim CheckRow As CheckBox = (TryCast(row.Cells(1).FindControl("chckSelector"), CheckBox))
If CheckRow.Checked Then
Dim scode As String = TryCast(row.Cells(2).FindControl("lblsstorecode"), Label).Text
lbltest.Text = 'this i want to get the value like this (value1,value2,value3) from checkbox that i selected
End If
End If
Next
Depending on why you are using the GridView control, you might me able to accomplish this easier by using a CheckBoxList instead. In Asp.net what you describe is accomplished very easily with a CheckBoxList as follows:
In .aspx:
<asp:CheckBoxList ID="Itemlst" runat="server" RepeatColumns="1">
<asp:ListItem Value="">Item 1</asp:ListItem>
<asp:ListItem Value="">Item 2</asp:ListItem>
<asp:ListItem Value="">Item 3</asp:ListItem>
</asp:CheckBoxList>
</br>
<asp:Button ID="Button1" runat="server" Text="Button" />
</br>
<asp:TextBox ID="TextBox1" runat="server" Width="400"></asp:TextBox>
Then in the code behind:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim selected = New List(Of ListItem)
Dim itemcount As Integer
Dim csvlist As String = ""
For Each item As ListItem In Itemlst.Items
If item.Selected Then selected.Add(item)
Next
itemcount = selected.Count
For i = 0 To itemcount - 1
csvlist = csvlist & selected(i).ToString & ","
Next
csvlist = Mid(csvlist, 1, Len(csvlist) - 1)
TextBox1.Text = csvlist
End Sub
The Windows Forms CheckedListBox would probably work similar if you are developing a desktop application.

Select multiple value in DropDownList

I have a dropdownlist (SingleSelection) which retrieve the data from sql database, I want to change it to MultiSelection (select multi value), following is my code.
ASP.NET
<asp:DropDownList ID="DrpGroup" runat="server" Width="250px" AutoPostBack="True">
</asp:DropDownList>
VB
Protected Sub DrpGroup_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DrpGroup.SelectedIndexChanged
If (DrpGroup.SelectedValue.ToString.Trim.ToUpper <> "PLEASE SELECT...") Then
Dim cnt_value As Integer = TDES.FindKey("Select Count(*) from dbo.VU_CUSTOMERBYGROUP WHERE upper(ltrim(rtrim(GROUP_NAME)))='" & DrpGroup.SelectedValue.ToString.Trim.ToUpper & "'")
lblNumberCount.Visible = True
lblNumberCount.Text = DrpGroup.SelectedValue.ToString.Trim.ToUpper & " has " & CStr(cnt_value) & " member(s). <br /> The cost for this SMS broadcast will be xxx" & CStr(cnt_value * 0.5)
End If
If (DrpGroup.SelectedValue.ToString.Trim.ToUpper = "PLEASE SELECT...") Then
lblNumberCount.Visible = False
End If
End Sub
Your efforts would be appreciated.
Use ListBox instead of DropDownList
<asp:ListBox runat="server" ID="multiSelect" SelectionMode="multiple" >
<asp:ListItem Text="option1" Value="option1"></asp:ListItem>
<asp:ListItem Text="option2" Value="option2"></asp:ListItem>
<asp:ListItem Text="option3" Value="option3"></asp:ListItem>
</asp:ListBox>

VB.net dropdown list value populated in variable rather than text

I have a dropdown list populated as follows:
<asp:DropDownList ID="ddlSubgroup" runat="server" cssclass="dropdown" style="font-size: 11px" AutoPostBack="True">
<asp:ListItem Selected="True">Select All</asp:ListItem>
<asp:ListItem Value="GenF">Girls</asp:ListItem>
<asp:ListItem Value="GenM">Boys</asp:ListItem>
-----snip
In my page load procedure I have declared a variable and then use that variable to update the contents of a label:
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
Dim strSubgroup As String = ddlSubgroup.Text
lblHeaderSubroup.Text = " " & strSubgroup
However, this results in the dropdown listitem value eg "GenF" displaying in my label and not the text eg Girls as expected.
Use ddlSubgroup.SelectedItem.Text property.
lblHeaderSubroup.Text = ddlSubgroup.SelectedItem.Text
Try referring to the lblHeaderSubroup.SelectedItem.Text property instead of the object itself.
This could be where you're experiencing the issue.

passing checkboxlist selected items as a concatenated string

I am trying to pass the selected items from a checkboxlist in asp.net (vs 2005/.net 2.0) as a concatenated string.
Currently my .aspx is
<asp:CheckBoxList id="checkbox1" AutoPostBack="False" AppendDataBoundItems="true" CellPadding="5" CellSpacing="5" RepeatColumns="1" RepeatDirection="Vertical" RepeatLayout="Flow" TextAlign="Right" runat="server">
<asp:ListItem Value="1">Carrots</asp:ListItem>
<asp:ListItem Value="2">Lettuce</asp:ListItem>
<asp:ListItem Value="3">Olives</asp:ListItem>
<asp:ListItem Value="4">Onions</asp:ListItem>
<asp:ListItem Value="5">Tomato</asp:ListItem>
<asp:ListItem Value="6">Pickles</asp:ListItem>
</asp:CheckBoxList>
And the .aspx.vb is (inside the Protected Sub for submit)
For Each li As ListItem In checkbox1.Items
If li.Selected = True Then
checkbox1.Text = checkbox1.Text + "," + li.Text
End If
Next
Which is written to the db via
checkbox1.Text = dv(0)("Salad").ToString()
When I select and save, I am currently getting an error
Server Error in '/' Application.
'checkbox1' has a SelectedValue which is invalid because it does not exist in the list of items.
Parameter name: value
Any thoughts on how to concatenate the selected checkbox items
For example, if some selects Carrots, Lettuce, and Tomato;
checkbox1 = 1,2,5
I don't think you are assigning to a variable like you describe you are returning.
string list = "";
For Each li As ListItem In chkQ4.Items
If li.Selected = True Then
list = list + "," + li.Text
End If
Next
is how you should write the line above.
In C# using linq, I would write
var list = checkbox1.Items
.Cast<ListItem>()
.Where(item => item.Selected == true)
.Select(item => item.Value);
var result = string.Join(",",list);
which I believe is the following in VB
Dim list = checkbox1.Items.Cast(Of ListItem)().Where(Function(item) item.Selected = True).[Select](Function(item) item.Value)
Dim result = String.Join(",", list.ToArray())
This is a little example
Markup:
<asp:CheckBoxList ID="CheckBoxList1" runat="server">
<asp:ListItem Value="1">Carrots</asp:ListItem>
<asp:ListItem Value="2">Apples</asp:ListItem>
<asp:ListItem Value="3">Lettuce</asp:ListItem>
</asp:CheckBoxList>
<br />
<asp:Literal ID="Literal1" runat="server"></asp:Literal><br />
<br />
<asp:Button ID="Button1" runat="server" Text="Concatenate" /><br />
<asp:Button ID="Button2" runat="server" Text="Save" />
Codebehind:
Private Sub SaveItems(ByVal strItems As String)
Dim cn As New SqlConnection("user id=sa;password=abcd;database=BD_Test;server=SERVNAME")
Dim cmd As New SqlCommand("Insert into CheckItems values (#ItemId)", cn)
Dim cmdPar As New SqlParameter("#ItemId", SqlDbType.Char, 1)
If strItems.Split(",").Length > 0 Then
cmd.Parameters.Add(cmdPar)
Using cn
cn.Open()
Using cmd
''Split the existing selected values, store it in an array
''and iterate to get each element of it to save it in the DB
For Each strItem As String In strItems.Split(",")
cmd.Parameters("#ItemId").Value = strItem
cmd.ExecuteNonQuery()
Next
End Using
Me.Literal1.Text = "Items saved"
End Using
End If
End Sub
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Me.Literal1.Text = ""
For Each l As ListItem In Me.CheckBoxList1.Items
''Concatenate keeping the order of the items in your CheckboxList
If l.Selected Then
Me.Literal1.Text = Me.Literal1.Text & l.Value & ","
End If
Next
''Remove the final "," in case its at the end of the string
''to avoid db issues and selected items issues
If Right(Me.Literal1.Text, 1) = "," Then
Me.Literal1.Text = Left(Me.Literal1.Text, Me.Literal1.Text.Length - 1)
End If
''You can also save the items directly after concatenating
''Me.SaveItems(Me.Literal1.Text)
End Sub
Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
Me.SaveItems(Me.Literal1.Text)
End Sub
The expected result is to have the selected values of the items on the page literal:
1,3
Hope this helps.

Resources