I'm trying to get a drop down list control to work in FormView. I need to have the list be a filtered view of a certain table and still be bound to a field in the data I'm editing. I've tried setting the item data programatically, ant that works but then the data binding
doesn't work, It tries to insert null into the database.
This is the code I've tried. I've also tried doing the same thing in several other events, it still tries to insert null into the database.
<asp:DropDownList ID="lstManagers" runat="server"
OnDataBound ="ManagersLoad"
SelectedValue='<%# Bind("UserName") %>' Width="100%"
DataSourceID="TimeOff" DataTextField="UserName" DataValueField="UserName">
</asp:DropDownList>
Protected Sub ManagersLoad(ByVal sender As Object, ByVal e As System.EventArgs)
Dim lst As DropDownList = FormView1.FindControl("lstManagers")
'get list of managers
Using ef As New TimeOffData.TimeOffEntities
For Each item As ListItem In lst.Items
Dim li As ListItem = item
item.Text = (From x In ef.TimeOffUsers Where x.UserName = li.Value Select x.FirstName & " " & x.LastName).FirstOrDefault
Next
End Using
End Sub
I took all the data binding stuff of the control and just decide it would be easier to do it manually. I've changed the code to this,
Protected Sub FormView1_ItemInserting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.FormViewInsertEventArgs) Handles FormView1.ItemInserting
Dim lst As DropDownList = FormView1.FindControl("lstManagers")
e.Values.Item("ManagerName") = lst.SelectedValue
End Sub
Protected Sub ManagersLoad(ByVal sender As Object, ByVal e As System.EventArgs)
Dim lst As DropDownList = FormView1.FindControl("lstManagers")
'get list of managers
Using ef As New TimeOffData.TimeOffEntities
Dim mng = From x In ef.TimeOffUsers Where x.IsManager = True
For Each item In mng
lst.Items.Add(New ListItem(item.FirstName & " " & item.LastName, item.UserName))
Next
End Using
End Sub
Related
I'm trying to create a quiz webpage in Visual Studio 2012 and have a little trouble getting the selected value from a dynamically created radiobuttonlist inside a table.
This is how I create the radiobuttonlist (and add it to the table)
Dim NewRow As New TableRow
Dim NewCell As New TableCell
Dim rblOptions As New RadioButtonList
rblOptions.ID = "Option" & intQuestion.ToString
NewCell.Controls.Add(rblOptions)
NewRow.Cells.Add(NewCell)
'Questions is a table
Questions.Rows.Add(NewRow)
This is how I am trying to get the selectedvalue from the radiobuttonlist after the users hits a button.
Dim rbl As RadioButtonList = DirectCast(Page.FindControl("Option" & intQuestion.ToString), RadioButtonList)
If rbl.SelectedValue.ToString = sdrGetQuestions.Item(0).ToString Then
intScore += 1
End If
In both cases the variable intQuestion = 0.
When running the program I come across the error:
Object reference not set to an instance of an object.
On this line:
If rbl.SelectedValue.ToString = sdrGetQuestions.Item(0).ToString Then
Which I think obviously means the program couldn't find the control it had to place in rbl. I'm couldn't find any answers online...
Could anyone point me in the right direction?
This is my .aspx code.
<asp:Table ID="Questions" runat="server">
</asp:Table>
<asp:Button ID="SaveButton" runat="server" Text="Save" />
This is my code behind file code.
I have added dynamic drop-down list.
On save button click, I am retrieving selected value.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim NewRow As New TableRow
Dim NewCell As New TableCell
Dim rblOptions As New RadioButtonList
rblOptions.ID = "Option1"
rblOptions.Items.Add(New System.Web.UI.WebControls.ListItem("1", "1"))
rblOptions.Items.Add(New System.Web.UI.WebControls.ListItem("2", "2"))
rblOptions.Items.Add(New System.Web.UI.WebControls.ListItem("3", "3"))
NewCell.Controls.Add(rblOptions)
NewRow.Cells.Add(NewCell)
'Questions is a table
Questions.Rows.Add(NewRow)
End Sub
Protected Sub SaveButton_Click(sender As Object, e As EventArgs) Handles SaveButton.Click
If Page.IsValid Then
Dim rbl As RadioButtonList = DirectCast(Questions.FindControl("Option1"), RadioButtonList)
If rbl.SelectedValue.ToString = "ExpectedValue" Then
End If
End If
End Sub
Not picking up that something is selected, and not displaying the value.
When I debug the code and step-in:
VBCODE:
When Debugging the "li" after Each holds a value(ex286)
when I go to Item and open the box I get this:
Item = Argument not specified for parameter 'index' of 'Public ReadOnly Default Property Item(index As Integer) As System.Web.UI.WebControls.ListItem'.
Item = In order to evaluate an indexed property, the property must be qualified and the arguments must be explicitly supplied by the user.
the "li" after If holds a value(ex286), but the Selected is "FALSE" Do not know why.
After the = li is the text and the Value(286)
Another thing it only gives me the value for the first box value not the rest if I click them.
Protected Sub LinkButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles LinkButton.Click
For Each li As ListItem In CheckBoxList.Items
If li.Selected Then
Texttext.Text = li.Value
Else
Texttext.Text = "Give Up Loser!"
End If
NextEnd Sub
ASCX FILE
<asp:CheckBox ID="CheckBoxSelectAll" runat="server" Text="Select All" AutoPostBack="True" />
<asp:CheckBoxList ID="CheckBoxList" runat="server"
DataSourceID="ObjectDataSource1" DataTextField="Name" DataValueField="Id"
RepeatColumns="3" ></asp:CheckBoxList>
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="GetStuff"
DataObjectTypeName ="DataTransfer.TheData"
TypeName="BusinessDelegate.DataBusinessDelegate">
</asp:ObjectDataSource>
<asp:LinkButton ID="LinkButton" runat="server" Text="Here"></asp:LinkButton>
<asp:Label ID ="Texttext" runat="server" Text=""></asp:Label>
I have tried a few items from online but nothing worked correctly.
Get all selected values of CheckBoxList in VB.NET
ASP.NET, VB: checking which items of a CheckBoxList are selected
I am not sure what you are trying to achieve with this.
The below code might work for you.
Protected Sub LinkButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles LinkButton.Click
Dim cbChecked As Boolean
For lItem = 0 To CheckBoxList.Items.Count - 1
cbChecked = CheckBoxList.GetItemChecked(lItem)
If cbChecked Then
Texttext.Text = CheckBoxList.GetItemText(lItem)
Else
Texttext.Text = "Give Up Loser!"
End If
Next
End Sub
When you run this above code, if the last check box is not checked then you will end up with
'Give Up Loser!' in the 'Texttext' text box.
You can get the number of checked boxes in the list by using the below code
Protected Sub LinkButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles LinkButton.Click
Dim checkedBoxes = CheckBoxList.CheckedItems
Dim checkedBoxesCount = checkedBoxes.Count
For Each lItems In checkedBoxes
Dim chkdCheckBoxName = lItems.ToString
Next
End Sub
Try this below code to write the values of checked boxes in the text box.
Protected Sub LinkButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles LinkButton.Click
Texttext.Text = "" 'clearing the text box
Dim checkedBoxes = CheckBoxList.CheckedItems
Dim checkedBoxesCount = checkedBoxes.Count
For Each lItems In checkedBoxes
Dim chkdCheckBoxName = lItems.ToString
Texttext.Text = Texttext.Text & " | " & chkdCheckBoxName
Next
If checkedBoxesCount = 0 Then
Texttext.Text = "Give Up Loser!"
End If
End Sub
Above code is based on System.Windows.Forms.CheckedListBox
I have a table with employee info: email, name , address etc. On one page a have a drop down with only the emails (Primary key) and on the second page I'd like to display a detail view for each employee. So far I've assigned the selected email to a session value.
Dim SelectedEmail As String
SelectedEmail = DropDownList1.SelectedValue
Session("selection") = SelectedEmail
I created a button in the first page that takes me to the detail page and viceversa. I'm having issues retrieving the email, so far i only have this:
Dim selectedemail As String = Session("selection")
I'll take a stab.
It appears that all you've done is put the selected value in a local variable
Dim SelectedEmail As String
SelectedEmail = DropDownList1.SelectedValue
when I think your intention is to put it in a session variable
Session("selection") = DropDownList1.SelectedValue
So that you can pick it up again on the other page
Dim selectedEmail As String = Session("selection")
I tried to reproduce the issue but it is working fine in my case.
I have added a button and a dropdownlist in a page called default.aspx
<li class="three">
<asp:Button ID="Button1" runat="server" Text="Button" />
<asp:DropDownList ID="DropDownList1" runat="server">
</asp:DropDownList>
</li>
In the Page.Load event handler I put three ListItem in the dropDownlist and in the Button.Click event handler I added a session attribute with the selectedValue from the DropDownList
Here is the code
Public Class _Default
Inherits Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
Dim item1 As ListItem = New ListItem("Test A", "test A", True)
Dim item2 As ListItem = New ListItem("Test AB", "test AB", True)
Dim item3 As ListItem = New ListItem("Test C", "test C", True)
DropDownList1.Items.Add(item1)
DropDownList1.Items.Add(item2)
DropDownList1.Items.Add(item3)
End Sub
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Session("variable") = "Test value"
Session("thevalue") = DropDownList1.SelectedValue
Response.Redirect("default2.aspx")
End Sub
End Class
When the click on the button occurs I provide the SelectedValue to Session("theValue") and redirect to default2.aspx. In default2.aspx I just use the Page.Load event to write on the Response stream after retrieving the Session attribute:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim myvariable As String = Session("variable")
Response.Write("the found value is" + myvariable)
Dim theValue As String = Session("thevalue")
Response.Write("<Br/> from drop down =" + theValue)
End Sub
It's working just fine.
For example when I select the 2nd item from the DropDownlist I have the following result
the found value isTest value
from drop down =test AB
So the issue lies somewhere else. You may check and see if you are handling the IsPostBack correctly in the Page.Load event. If you could share more of your code, that would help.
I am using a checked combobox for selecting multiple items. Following is the code am using
ASP.Net
<telerik:RadAjaxPanel runat="server" ID="RadAjaxPanel1">
<telerik:RadComboBox ID="cbo_tagtofilelist" runat="server" CheckBoxes="true"
Style="width: 157px; height: 15px" ExpandDirection="Down" EnableCheckAllItemsCheckBox="true"
value="date" Skin="WebBlue">
</telerik:RadComboBox>
</telerik:RadAjaxPanel>
VB.Net
Private Sub lnkfiletag_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkfiletag.Click
ShowCheckedItems(cbo_tagtofilelist)
End Sub
Private Shared Sub ShowCheckedItems(ByVal comboBox As RadComboBox)
Dim msSQL As String = " insert into selecteditems( fileid, name ) values"
For Each chkeitems As RadComboBoxItem In comboBox.CheckedItems
If chkeitems.Checked = True Then
msSQL &= " ('" & fid & "', '" & chkeitems.Value & "'),"
End If
Next
msSQL = msSQL.Substring(0, Len(msSQL) - 1)
con.OpenConn()
con.ExecuteNonQuerySQL(msSQL)
con.CloseConn()
End Sub
But using this code I am unable to find the selected items and insert into the database. Item count always be 0 on button click, what am I doing wrong with this code
Bind your RadComboBox in Page_Init also.
Here's Code :
Protected Sub Page_init(sender As Object, e As EventArgs)
RadComboBox1.DataSource = list
'Your DataSource Here
RadComboBox1.DataTextField = "TextField"
RadComboBox1.DataValueField = "ValueField"
RadComboBox1.DataBind()
End Sub
Please find more information here.
I am trying to set the selected value of a radiobuttonlist that is a template column within a gridview. I am having to do this using the code behind as the database field containing the data (Status) contains null values hence I cant used SelectedValue='<%# Bind("Status") %>' on the asp side.
It has been suggested that I use onRowDataBound to do this and use DataItem to retrieve the value from the datasource and used it to set the radiobuttonlist selected value but I'm not sure how to do this in vb code.
I've tried the following:
Protected Sub gv_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
Dim radioList = TryCast(e.Row.FindControl("rblActions"), RadioButtonList)
' this will be the object that you are binding to the grid
Dim myObject = TryCast(e.Row.DataItem, DataRowView)
Dim sStatus As String = Convert.ToString(myObject("Status"))
If sStatus <> Nothing Then
radioList.SelectedValue = sStatus
End If
End If
End Sub
but it hasnt work. Any help would be appreciated. Thanks
Found my own solution as follows:
Sub gv_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
Dim rbl As RadioButtonList = CType(e.Row.FindControl("rblActions"), RadioButtonList)
Dim selected As String = e.Row.DataItem("Status").ToString
If Not IsNothing(selected) Then
rbl.SelectedValue = selected
End If
End If
End Sub