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.
Related
i have I have an ASP.NET / VB application
i need check on selected item in check box list but the checkbox is dynamically from code behind
<asp:Panel ID="panel1" runat="server">
<asp:Button ID="Button1" runat="server" Text="Save" />
<asp:Label ID="lbl_Selected_Items" runat="server" Text=""></asp:Label>
</asp:Panel>
code behind
page load
Dim CheckBoxList As New CheckBoxList
pagerview.Controls.Add(CheckBoxList)
...est
Button1 Click event
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim CheckBoxList1 As CheckBoxList = CType(Me.FindControl("CheckBoxList"), CheckBoxList)
Dim sCheckedValue As String = ""
For Each oItem As ListItem In CheckBoxList1.Items
If oItem.Selected Then
If sCheckedValue = "" Then
sCheckedValue = ("Selected Value : " + oItem.Value & " Selected Text: ") + oItem.Text
Else
sCheckedValue += ("<br/>Selected Value : " + oItem.Value & " Selected Text: ") + oItem.Text
End If
End If
Next
lbl_Selected_Items.Text = sCheckedValue
error text : null reference exception object reference not set to an instance of an object –
Error
So what is happening? Error? no result?
At this point the only thing I can recommend is loading dynamic controls in the page init instead of the page load.
EDIT
You will find when you debug that your checkbox on the first line of your button click is Nothing. There are two mistakes in your code.
When you add the control to the form, you do not provide an id value. and you do not lookup this value
When you lookup the control in the button click FindControl only looks in a single container. It is not recursive. You should be calling pagerView.FindControl
The Page load (I still recommend init):
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
Handles Me.Load
Dim CheckBoxList As New CheckBoxList
CheckBoxList.ID = "cbl1"
pagerview.Controls.Add(CheckBoxList)
End Sub
And the button click:
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim CheckBoxList1 As CheckBoxList = CType(pagerview.FindControl("cbl1"), CheckBoxList)
Dim sCheckedValue As String = ""
For Each oItem As ListItem In CheckBoxList1.Items
If oItem.Selected Then
If sCheckedValue = "" Then
sCheckedValue = ("Selected Value : " + oItem.Value & " Selected Text: ") + oItem.Text
Else
sCheckedValue += ("<br/>Selected Value : " + oItem.Value & " Selected Text: ") + oItem.Text
End If
End If
Next
lbl_Selected_Items.Text = sCheckedValue
End Sub
MS Reference
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 an HTML table to display a set of records from database after user entered a patient code or during Page Load when the patient code is passed through Querystring. Each data row has a Delete link button which is created dynamically.
I'd like to have the table refreshed after deletion. However, whenever the link button is clicked, it will postback and all the records will be wiped out unless I reload the data. If I reload the data again after deletion, there will be 2 times data fetching and the table rows will be doubled.
The first 2 rows of the tables are defined on the ASPX for easier styling purpose. If I cleared the table when fetching the data, the first rows will be wiped out as well. I have another 8 tables on different pages created in the same manner, hence I'd rather find other solution rather than defining and styling the rows from code behind.
Any help will be greatly appreciated.
The codes I use are as follow:
ASPX
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="lblPatientCode" runat="server" Text="Patient Code : "></asp:Label>
<asp:TextBox ID="txtPatientCode" runat="server"></asp:TextBox>
<asp:Button ID="btnFind" runat="server" name="Date" Text="Find" Width="90" CssClass="ButtonNormal" />
<br /><br />
<table id="tblDoctorInformation" class="PatientDetailsTable" runat="server">
<tr><td colspan="2">DOCTOR INFORMATION</td></tr>
<tr>
<td>Doctor In Charge</td>
<td> </td>
</tr>
</table>
</div>
</form>
</body>
Code Behind:
Protected PM As New PatientManagement.Common
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
GetPatient()
Else
If txtPatientCode.Text <> "" Then
GetPatient()
End If
End If
End Sub
Private Sub GetPatient()
Dim sPatientCode As String = ""
If IsPostBack Then
sPatientCode = Trim(txtPatientCode.Text)
Else
sPatientCode = Trim(Page.Request.QueryString("PatientCode"))
End If
If sPatientCode <> "" Then
Dim dr As SqlDataReader
dr = PM.ExecuteReader("SELECT LOGID,DOCTORNAME FROM PMV_DOCTORINCHARGE WHERE PATIENTCODE='" & sPatientCode & "'")
If dr.HasRows Then
Do While dr.Read
Dim tRow As New HtmlTableRow
Dim tCellDoctorName As New HtmlTableCell
Dim tCellModifyLink As New HtmlTableCell
Dim lb As New LinkButton
'Doctor Name
tCellDoctorName.InnerHtml = PM.GetString_TableCell(dr("DoctorName"))
tRow.Cells.Add(tCellDoctorName)
'Delete links
lb.Text = "Delete"
lb.Attributes.Add("AutoPostBack", False)
lb.CommandArgument = dr("LogID").ToString()
AddHandler lb.Click, AddressOf DeleteRecord
tCellModifyLink.Controls.Add(lb)
tCellModifyLink.Align = "Center"
tRow.Cells.Add(tCellModifyLink)
tblDoctorInformation.Rows.Add(tRow)
Loop
End If
End If
End Sub
Private Sub btnFind_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnFind.Click
GetPatient()
End Sub
Protected Sub DeleteRecord(ByVal sender As Object, ByVal e As System.EventArgs)
Dim lbNew As New LinkButton
Dim sResult As String = ""
Dim bResult As Boolean
lbNew = sender
bResult = PM.DeleteMultiRecord(lbNew.CommandArgument, lbNew.CommandName, Session.Item("UserID"), sResult)
If Not bResult Then
MsgBox(sResult, MsgBoxStyle.OkOnly, "Deletion was not successful")
Else
GetPatient()
End If
End Sub
Before Deleting a record, try to place a PatientID in Textbox and Delete the record, then Table will Display the records matching with the TextBox input.
The Problem is with PageLoad() method, if the TextBox is Empty And It is a Postback, then GetPatient() method will not be called. So try to modify the PageLoad() Method.
Better to remove IsPostBack condition in PageLoad().
I have searched around and it seems dynamic handler is best to be declared during PageInit. For my case, as the link button control needs to carry some values on the respective data row, it seems I can't avoid calling GetPatient to create controls.
To move on with the issue, I did a workaround by using Javascript. I created a hidLogID hidden field on ASPX page. I changed the link button to an <a> control and call a javascript function passing the dr("LogID"). The javascript function will assign the value to hidLogID and submit the page. PageLoad will call DeleteRecord()when hidLogID value is not blank. The value for hidLogID will be cleared on DeleteRecord().
Thank you for all the help!
I have a form which contains a checkbox field. On page load I want to create a separate checkbox for each customer in my Database. The code I have to create the checkboxes for each customer works fine. However, I also want to check in the database if the customer is set to unauthorized if they are then I want to check there box. I also have code for the case where the user checks a box. If a box is checked I update the database setting the unauthorized attribute to true. My problem is when I check a box it works fine and the box is checked, however if I reload the page all the boxes are unchecked. So either my database update is not updating the database or the way I check on page load for checked boxes is incorrect. Any ideas?
The code for the asp checkbox field:
<asp:CheckBoxList id="check1" AutoPostBack="True" TextAlign="Right" OnSelectedIndexChanged="Check" runat="server">
</asp:CheckBoxList>
The Code for the page load:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim sql As String = "SELECT Name, unauthorized, ID FROM Customer ORDER BY Name"
Dim dt As DataTable = db.execDataTableQuery(sql, "Customer")
Dim i As Integer
For i = 0 To dt.Rows.Count - 1
check1.Items.Add(New ListItem(CStr(dt.Rows(i).Item("Name"))))
check1.Items(i).Value = CInt(dt.Rows(i).Item("ID"))
check1.Items(i).Text = CStr(dt.Rows(i).Item("Name"))
If CInt(dt.Rows(i).Item("unauthorized")) = 1 Then
check1.Items(i).Selected = 1
Else
check1.Items(i).Selected = 0
End If
Next
End Sub
The code to update the database:
Sub Check(ByVal sender As Object, ByVal e As EventArgs)
Dim sql As String
If check1.SelectedItem.Selected = 1 Then
sql = "UPDATE Customer SET unauthorized = 1 WHERE ID = #ID"
db.execUpdateQuery(sql, New SqlClient.SqlParameter("#ID", check1.SelectedItem.Value))
Else
sql = "UPDATE Customer SET unauthorized = 0 WHERE ID = #ID"
db.execUpdateQuery(sql, New SqlClient.SqlParameter("#ID", check1.SelectedItem.Value))
End If
End Sub
End Class
You need to wrap you code in Page_Load in a Not Page.IsPostback, otherwise on postback no events are triggered and the selection is lost since you're overwriting it from database.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostback Then
' databind your CheckBoxList '
End If
End Sub
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