Visual Studio .NET - How to bind ListBox? - Asked By ben beethe on 25-Jul-05 04:26 PM
I have a listbox in my ItemTemplate of a FormView.
How do I bind the listbox? Here is my Source.
<asp:GridView ID="GridView1" AllowSorting="True" AllowPaging="True" runat="server"
DataSourceID="SqlDataSource1" DataKeyNames="class_name"
AutoGenerateColumns="False" Width="500px" SelectedIndex="0" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" OnPageIndexChanged="GridView1_PageIndexChanged" OnRowDeleted="GridView1_RowDeleted" OnSorted="GridView1_Sorted">
<Columns>
<asp:CommandField ShowSelectButton="true" ShowDeleteButton="true" />
<asp:BoundField DataField="class_name" HeaderText="class_name" ReadOnly="True" SortExpression="class_name" />
<asp:BoundField DataField="professor" HeaderText="professor" SortExpression="professor" />
<asp:BoundField DataField="room" HeaderText="room" SortExpression="room" />
<asp:BoundField DataField="days" HeaderText="days" SortExpression="days" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="Data Source=LAPTOP;Initial Catalog=classes;Persist Security Info=True;User ID=jettman26;Password=jettmanou812"
SelectCommand="SELECT * FROM [classes]"
DeleteCommand="DELETE FROM [classes] WHERE [class_name] = @original_class_name" ProviderName="System.Data.SqlClient">
</asp:SqlDataSource>
</td>
<td valign="top">
<asp:FormView ID="FormView1" runat="server" DataKeyNames="class_name" DataSourceID="SqlDataSource3" OnItemUpdated="FormView1_ItemUpdated" OnItemInserted="FormView1_ItemInserted" GridLines="Both">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("class_name") %>'></asp:Label><br />
<asp:Label ID="Label2" runat="server" Text='<%# Bind("professor") %>'></asp:Label><br />
<asp:ListBox ID="ListBox1" runat="server" SelectionMode="Multiple" >
<asp:ListItem>Monday</asp:ListItem>
<asp:ListItem>Tuesday</asp:ListItem>
<asp:ListItem>Wednesday</asp:ListItem>
<asp:ListItem>Thursday</asp:ListItem>
<asp:ListItem>Friday</asp:ListItem>
<asp:ListItem>Saturday</asp:ListItem>
<asp:ListItem>Sunday</asp:ListItem>
</asp:ListBox><br>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("time_start") %>'></asp:Label>
<asp:Label ID="Label4" runat="server" Text='<%# Bind("time_end") %>'></asp:Label><br />
<asp:Label ID="Label5" runat="server" Text='<%# Bind("room") %>'></asp:Label><br />
</ItemTemplate>
</asp:FormView>
<asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="Data Source=LAPTOP;Initial Catalog=classes;Persist Security Info=True;User ID=jettman26;Password=jettmanou812"
SelectCommand="SELECT [class_name], [professor], [time_start], [time_end], [room], [days] FROM [classes] WHERE ([class_name] = @class_name)"
UpdateCommand="UPDATE [classes] SET [professor] = @professor, [time_start] = @time_start, [time_end] = @time_end, [room] = @room, [days] = @days WHERE [class_name] = @original_class_name"
InsertCommand="INSERT INTO [classes] ([class_name], [professor], [time_start], [time_end], [room], [days]) VALUES (@class_name, @professor, @time_start, @time_end, @room, @days)" ProviderName="System.Data.SqlClient">
<SelectParameters>
<asp:ControlParameter ControlID="GridView1" Name="class_name" PropertyName="SelectedValue"
Type="String" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="professor" Type="String" />
<asp:Parameter Name="time_start" Type="String" />
<asp:Parameter Name="time_end" Type="String" />
<asp:Parameter Name="room" Type="String" />
<asp:Parameter Name="days" Type="String" />
<asp:Parameter Name="original_class_name" Type="String" />
</UpdateParameters>
<InsertParameters>
<asp:Parameter Name="class_name" Type="String" />
<asp:Parameter Name="professor" Type="String" />
<asp:Parameter Name="time_start" Type="String" />
<asp:Parameter Name="time_end" Type="String" />
<asp:Parameter Name="room" Type="String" />
<asp:Parameter Name="days" Type="String" />
</InsertParameters>
</asp:SqlDataSource>
</td>
</tr>
</table>
<br />
<asp:Label ID="ErrorMessageLabel" EnableViewState="false" runat="server" />
</asp:Content>
Which ListBox are you trying to bind? - Asked By Jon Wojtowicz on 25-Jul-05 05:42 PM
The only ListBox in your code sample has static list items and does not need to be databound. If you are referring to binding to your insert/update parameters you'll have to do that in code since you have your list box is set to allow multiple selections.
Trying to bind to ListBox1 - Asked By ben beethe on 25-Jul-05 05:56 PM
Since my post I have added a EditItemTemplate and InsertItemTemplate, but for now that doesn't matter.
I want to bind to ListBox1.
Lets say in a specific record in my DB is Monday. In my ListBox1 I want it to show Monday being selected. That is what I don't know how to do. Right now when I pull up that record it shows nothing being selected.
After the ListBox is dataBound, - Asked By Peter Bromberg on 25-Jul-05 07:10 PM
you can set the SelectedIndex property programmatically to choose which item is pre-selected.
e.g., (untested pseudocode)
ListBox.DataBind();
ListBox.SelectedIndex =4;
If your day of the week - Asked By Jon Wojtowicz on 25-Jul-05 07:29 PM
selected is stored as an integer you can bind to the SelectedIndex property using the databinding expression
SelectedIndex='<%# Eval("dayOfWeek") %>'
If not, then you'll have to write code. You can use the ListBox.Items.FindByText to find the matching item and then set it as the SelectedItem.
I did notice that you have the mode set to multiselect. If you have multiple values then you'll need to perform this ina loop and the data binding expression won't work.
I tried it - Asked By ben beethe on 25-Jul-05 11:03 PM
I tried SelectedIndex='<%# Eval("days") %>' and it didn't work.
I get the message:
Conversion from string "Monday " to type 'Integer' is not valid
I also tried SelectedValue='<%# Eval("days") %>' and got the message:
'ListBox1' has a SelectedValue which is invalid because it does not exist in the list of items.
Parameter name: value
This doesn't make any sense because I only have one record in my Class table and the days field contains "Monday". I have "Monday" as a ListItem. So why doesn't this work?
Pre-selected - Asked By ben beethe on 25-Jul-05 11:04 PM
I don't really want any pre-selected. I want it to read the db and if Monday is in the db for that record, then Monday will be Selected in the ListBox for that record.
This seems to be way harder than it should be.
The issue is - Asked By Jon Wojtowicz on 26-Jul-05 05:24 AM
what you can do using the declaritive syntax. Only the SelectedIndex is available. If you are storing the day as a string there is little you can do other than writing code and finding the value in the list.
Another approach is to store the enumerated value for the day of the week as an integer, Monday = 0, Tuesday = 1, etc. This would allow you to set the SelectedIndex using the databinding expression.
I changed to SelectedIndex - Asked By ben beethe on 26-Jul-05 10:09 AM
I am now using
SelectedIndex='<%# Bind("days") %>'
I changed the Value for the listItems to use 0-6 for the days of the week. Now if I have 0 in my days field in my DB, it will have Monday selected in my ListBox!
Thanks a lot for that.
Now comes the next part. I want to be able to select multiple days.
My thought was that I could seperate each value in my days field by seperating each value with a ';'.
Here is what I though could work, but I can't get the syntax right.
Also, if there is something better I can do, let me know.
Protected Sub FormView1_ItemInserting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.FormViewInsertEventArgs) Handles FormView1.ItemInserting
Dim lbl As ListBox = CType(FormView1.FindControl("ListBox1"), ListBox)
Dim var As Integer = lbl.SelectedIndex
Dim strDay As String = ""
For Each _li As ListItem In var
If _li.Selected Then
strDay += _li.Value + ";"
End If
Next
SqlDataSource3.InsertParameters("days").DefaultValue = strDay
End Sub
You're close - Asked By Jon Wojtowicz on 26-Jul-05 04:58 PM
Protected Sub FormView1_ItemInserting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.FormViewInsertEventArgs) Handles FormView1.ItemInserting
Dim lbl As ListBox = CType(FormView1.FindControl("ListBox1"), ListBox)
Dim var As Integer = lbl.SelectedIndex
Dim strDay As String = ""
For Each _li As ListItem In lbl.Items
If _li.Selected Then
strDay += _li.Value + ";"
End If
Next
SqlDataSource3.InsertParameters("days").DefaultValue = strDay
End Sub
I would suggest using a StringBuilder for your concatenation and also removing the trailing ; before you save the value. I'm assuming you're going to use String.Split(';') to extract the values back out.
I might also suggest that you modify your db schema and place the week day values in a separate table so they can be searchable.
still getting nullreference - Asked By ben beethe on 26-Jul-05 05:21 PM
Thanks for the code.
When it gets to
For Each _li As ListItem In lbl.Items
I get that same "NullReferenceException was unhandled by user code"
"Object reference not set to an instance of an object."
by the way - Asked By ben beethe on 26-Jul-05 05:24 PM
The var variable is not used in your code?
got it to go past that - Asked By ben beethe on 26-Jul-05 05:53 PM
I was using the wrong listbox :(
Now it goes thru the code and when it is done with this sub, it goes to the following sub.
Protected Sub FormView1_ItemInserted(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.FormViewInsertedEventArgs)
If (Not e.Exception Is Nothing) Then
ErrorMessageLabel.Text = "An error occured while entering this record. Please verify you have entered data in the correct format."
e.ExceptionHandled = True
End If
GridView1.DataBind()
End Sub
Then it asks if I want to see the dissasembler because of some error that I can't see.
This error might be caused since I don't have the code to read these values back from the db.
This is where I am really confused. Where do I put the code to read from the db to show which listItems should be selected in my ListBox?
Actually it's in the code you posted - Asked By Jon Wojtowicz on 26-Jul-05 05:58 PM
but you are correct I did not use it and forgot to remove it. The NullReferencException means that
Dim lbl As ListBox = CType(FormView1.FindControl("ListBox1"), ListBox) is failing. If you step through the code you can verify that.
What are you getting in the e.Values dictionary when you run your code? I'm wondering if the values are being placed in there.
You should place the code - Asked By Jon Wojtowicz on 26-Jul-05 06:04 PM
in the DataBound event of the FormView. Then you should be able to get a reference to your listbox, get the data from the database and set the selected items. You can get a reference to the item that's databound by using the DataItem property of the FormView.
do you have an example? - Asked By ben beethe on 26-Jul-05 07:55 PM
Thanks for the info.
Do you have an example on using the DataItem for the reference and get the data from the db?
don't think I am doing this right - Asked By ben beethe on 27-Jul-05 11:25 AM
To read out of the db into the ListBox, I am not sure how to do this. Don't know where/how to use DataItem like you said. This is all I have and don't think it is right.
Dim lbl As ListBox = CType(FormView1.FindControl("ListBox1"), ListBox)
SqlDataSource3.SelectParameters("days").DefaultValue = lbl
SqlDataSource3.SelectCommand = "SELECT [days] FROM [Classes] WHERE ([class_name] = @class_name)"
this is what I came up with - Asked By ben beethe on 27-Jul-05 03:28 PM
When I have this code in here, My page won't even load. I am extremely lost.
Any ideas?
Protected Sub FormView1_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles FormView1.DataBound
Dim lbl As ListBox = CType(FormView1.DataItem("ListBox1"), ListBox)
Dim strDay As String()
strDay = SqlDataSource3.SelectParameters("days").ToString.Split(";"c)
SqlDataSource3.SelectCommand = "SELECT [days] FROM [Classes] WHERE ([class_name] = @class_name)"
Dim i As Integer = 0
While i < strDay.Length - 1
lbl.Items.FindByText(strDay(i)).Selected = True
i = i + 1
End While
End Sub
I was looking for an example - Asked By Jon Wojtowicz on 27-Jul-05 07:01 PM
of something I had that was similar. Unfortunately I think I deleted my VM since it was with Beta 1.
I think you'd be better off without SqlDataSource3 in this case. Your code could look something like this:
Dim lbl As ListBox = CType(FormView1.FindControl("ListBox1"), ListBox)
Dim strDays As String()
strDays = CType(FormView1.DataItem, DataRowView)("days").ToString.Split(";"c)
For Each strDay As String in strDays
ListBox.Items.FindByText(strDay).Selected = True <== This could be FindByValue
Next
thanks for the example - Asked By ben beethe on 27-Jul-05 08:18 PM
Thanks. I think I have the reading from the database working. Now I still have a problem w/Inserting into the DB.
After I click the Insert link, it runs through the following code fine. When it hits End Sub, the value of strDay is "5;6;" (I selected Saturday and Sunday) which I believe it should be. But,
Protected Sub FormView1_ItemInserting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.FormViewInsertEventArgs) Handles FormView1.ItemInserting
Dim lbl As ListBox = CType(FormView1.FindControl("ListBox3"), ListBox)
Dim strDay As String = ""
For Each _li As ListItem In lbl.Items
If _li.Selected Then
strDay += _li.Value + ";"
End If
Next
End Sub
the problem is when it goes thru the following code. It doesn't go thru the error handling and when it gets to End Sub, I get a dissassembler error. Got any ideas?
Protected Sub FormView1_ItemInserted(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.FormViewInsertedEventArgs)
If (Not e.Exception Is Nothing) Then
ErrorMessageLabel.Text = "An error occured while entering this record. Please verify you have entered data in the correct format."
e.ExceptionHandled = True
End If
GridView1.DataBind()
End Sub
Here is my updated Source code.
<asp:GridView ID="GridView1" AllowSorting="True" AllowPaging="True" runat="server"
DataSourceID="SqlDataSource1" DataKeyNames="class_name"
AutoGenerateColumns="False" Width="500px" SelectedIndex="0" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" OnPageIndexChanged="GridView1_PageIndexChanged" OnRowDeleted="GridView1_RowDeleted" OnSorted="GridView1_Sorted">
<Columns>
<asp:CommandField ShowSelectButton="true" ShowDeleteButton="true" />
<asp:BoundField DataField="class_name" HeaderText="class_name" ReadOnly="True" SortExpression="class_name" />
<asp:BoundField DataField="professor" HeaderText="professor" SortExpression="professor" />
<asp:BoundField DataField="room" HeaderText="room" SortExpression="room" />
<asp:BoundField DataField="days" HeaderText="days" SortExpression="days" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="Data Source=LAPTOP;Initial Catalog=classes;Persist Security Info=True;User ID=jettman26;Password=jettmanou812"
SelectCommand="SELECT * FROM [classes]"
DeleteCommand="DELETE FROM [classes] WHERE [class_name] = @original_class_name" ProviderName="System.Data.SqlClient">
</asp:SqlDataSource>
</td>
<td valign="top">
<asp:FormView ID="FormView1" runat="server" DataKeyNames="class_name" DataSourceID="SqlDataSource3" OnItemUpdated="FormView1_ItemUpdated" OnItemInserted="FormView1_ItemInserted" GridLines="Both">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("class_name") %>'></asp:Label><br />
<asp:Label ID="Label2" runat="server" Text='<%# Bind("professor") %>'></asp:Label><br />
<asp:ListBox ID="ListBox1" runat="server" SelectionMode="Multiple" Height="121px">
<asp:ListItem Value="0">Monday</asp:ListItem>
<asp:ListItem Value="1">Tuesday</asp:ListItem>
<asp:ListItem Value="2">Wednesday</asp:ListItem>
<asp:ListItem Value="3">Thursday</asp:ListItem>
<asp:ListItem Value="4">Friday</asp:ListItem>
<asp:ListItem Value="5">Saturday</asp:ListItem>
<asp:ListItem Value="6">Sunday</asp:ListItem>
</asp:ListBox><br>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("time_start") %>'></asp:Label>
<asp:Label ID="Label4" runat="server" Text='<%# Bind("time_end") %>'></asp:Label><br />
<asp:Label ID="Label5" runat="server" Text='<%# Bind("room") %>'></asp:Label><br />
<asp:LinkButton ID="Edit" runat="server" CommandName="Edit">Edit</asp:LinkButton>
<asp:LinkButton ID="New" runat="server" CommandName="New">New</asp:LinkButton>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("class_name") %>'></asp:TextBox><br>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("professor") %>'></asp:TextBox><br>
<asp:ListBox ID="ListBox2" runat="server" SelectionMode="Multiple" >
<asp:ListItem Value="0">Monday</asp:ListItem>
<asp:ListItem Value="1">Tuesday</asp:ListItem>
<asp:ListItem Value="2">Wednesday</asp:ListItem>
<asp:ListItem Value="3">Thursday</asp:ListItem>
<asp:ListItem Value="4">Friday</asp:ListItem>
<asp:ListItem Value="5">Saturday</asp:ListItem>
<asp:ListItem Value="6">Sunday</asp:ListItem>
</asp:ListBox><br>
<asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("time_start") %>'></asp:TextBox>
<asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("time_end") %>'></asp:TextBox><br>
<asp:TextBox ID="TextBox5" runat="server" Text='<%# Bind("room") %>'></asp:TextBox><br>
<asp:LinkButton ID="Update" runat="server" CommandName="Update">Update</asp:LinkButton>
<asp:LinkButton ID="Cancel" runat="server" CommandName="Cancel">Cancel</asp:LinkButton>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("class_name") %>'></asp:TextBox><br>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("professor") %>'></asp:TextBox><br>
<asp:ListBox ID="ListBox3" runat="server" SelectionMode="Multiple" SelectedIndex='<%# Bind("days") %>'>
<asp:ListItem Value="0">Monday</asp:ListItem>
<asp:ListItem Value="1">Tuesday</asp:ListItem>
<asp:ListItem Value="2">Wednesday</asp:ListItem>
<asp:ListItem Value="3">Thursday</asp:ListItem>
<asp:ListItem Value="4">Friday</asp:ListItem>
<asp:ListItem Value="5">Saturday</asp:ListItem>
<asp:ListItem Value="6">Sunday</asp:ListItem>
</asp:ListBox><br>
<asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("time_start") %>'></asp:TextBox>
<asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("time_end") %>'></asp:TextBox><br>
<asp:TextBox ID="TextBox5" runat="server" Text='<%# Bind("room") %>'></asp:TextBox><br>
<asp:LinkButton ID="Insert" runat="server" CommandName="Insert">Insert</asp:LinkButton>
<asp:LinkButton ID="Cancel" runat="server" CommandName="Cancel">Cancel</asp:LinkButton>
</InsertItemTemplate>
</asp:FormView>
<asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="Data Source=LAPTOP;Initial Catalog=classes;Persist Security Info=True;User ID=jettman26;Password=jettmanou812"
SelectCommand="SELECT [class_name], [professor], [time_start], [time_end], [room], [days] FROM [classes] WHERE ([class_name] = @class_name)"
UpdateCommand="UPDATE [classes] SET [professor] = @professor, [time_start] = @time_start, [time_end] = @time_end, [room] = @room, [days] = @days WHERE [class_name] = @original_class_name"
InsertCommand="INSERT INTO [classes] ([class_name], [professor], [time_start], [time_end], [room], [days]) VALUES (@class_name, @professor, @time_start, @time_end, @room, @days)" ProviderName="System.Data.SqlClient">
<SelectParameters>
<asp:ControlParameter ControlID="GridView1" Name="class_name" PropertyName="SelectedValue"
Type="String" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="professor" Type="String" />
<asp:Parameter Name="time_start" Type="String" />
<asp:Parameter Name="time_end" Type="String" />
<asp:Parameter Name="room" Type="String" />
<asp:Parameter Name="days" Type="String" />
<asp:Parameter Name="original_class_name" Type="String" />
</UpdateParameters>
<InsertParameters>
<asp:Parameter Name="class_name" Type="String" />
<asp:Parameter Name="professor" Type="String" />
<asp:Parameter Name="time_start" Type="String" />
<asp:Parameter Name="time_end" Type="String" />
<asp:Parameter Name="room" Type="String" />
<asp:Parameter Name="days" Type="String" />
</InsertParameters>
</asp:SqlDataSource>
I think you need - Asked By Jon Wojtowicz on 27-Jul-05 08:32 PM
to set the insert parameter "days" value equal to the days string, strDay before you leave the ItemInserting method.
You should also have the following line so it doesn't get an error on loading
strDay = strDay.Substring(0, strDay.Length-1)
that will remove the trailing ;
wouldn't that - Asked By ben beethe on 27-Jul-05 09:06 PM
cause a problem with the following sub because I am looking for ";"?
Also, If I manually enter 5;6; in my days column in my db, it will load up just fine when I hit select.
Protected Sub FormView1_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles FormView1.DataBound
If Not FormView1.CurrentMode = FormViewMode.Insert Then
Dim lbl As ListBox = CType(FormView1.FindControl("ListBox1"), ListBox)
Dim strDay As String()
strDay = FormView1.DataItem("days").ToString.Split(";"c)
Dim i As Integer = 0
While i < strDay.Length - 1
lbl.Items.FindByValue(strDay(i)).Selected = True
i = i + 1
End While
End If
End Sub
Ok but did you notice - Asked By Jon Wojtowicz on 27-Jul-05 09:19 PM
you are skipping over the last element in your array? That's because the split char at the end of your string is giving you a null string as the last element. If you strip the last ; you wouldn't have to worry about it. That's also why the For Each was probably giving you an exception.
Whatever works for you though. Glad you got it to work.
Also, - Asked By ben beethe on 27-Jul-05 09:20 PM
do I need
SelectedIndex='<%# Bind("days") %>'
in my InsertItemTemplate for the listbox when I do an Insert?
And, if I have SelectedIndex='<%# Bind("days") %>'
in my listbox for my InsertItemTemplat, it will still write to the db but it will only write the value of the first day selected, i.e. if I select Monday it will write 0 with no';'.
If I take SelectedIndex='<%# Bind("days") %>' out, it won't write the days field at all.
Since I don't have SelectedIndex='<%# Bind("days") %>' in my ItemTemplate and in my EditItemTemplate, I don't think I need it for my InsertItemTemplate for listbox. Would this be correct?
Since you have a multi select listbox - Asked By Jon Wojtowicz on 27-Jul-05 09:34 PM
you do not need to set a selectedindex for it. The user should be able to see the selected items in the list.
If you want to get really fancy, try and use a checkbox list in a panel. That way the user doesn't need to hold down the ctrl key when making multiple selections.
i added it - Asked By ben beethe on 27-Jul-05 09:37 PM
Now the sub is the following.
Protected Sub FormView1_ItemInserting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.FormViewInsertEventArgs) Handles FormView1.ItemInserting
Dim lbl As ListBox = CType(FormView1.FindControl("ListBox3"), ListBox)
Dim strDay As String = ""
For Each _li As ListItem In lbl.Items
If _li.Selected Then
strDay += _li.Value + ";"
End If
Next
strDay = strDay.Substring(0, strDay.Length - 1)
End Sub
But I am still getting the dissasembler error when It goes thru the
Protected Sub FormView1_ItemInserted
would that be easier - Asked By ben beethe on 27-Jul-05 09:39 PM
than what I am trying to do?
Also, - Asked By ben beethe on 27-Jul-05 09:44 PM
If I get rid of the trailing ";", it will not have my last date selected.
i.e., If I have 0;1 in the db, only Monday will be selected in my Listbox. If I don't remove the trailing ";", It will have Monday and Tuesday selected in my ListBox.
Could any of my problems be related to the fact that my ListBoxes are named different.
ItemTemplate - ListBox1
EditItemTemplate -ListBox2
InsertItemTemplate -ListBox3
You need to set the - Asked By Jon Wojtowicz on 28-Jul-05 05:40 AM
Insert Parameter value on your SqlDataSource to strDays. I still don't see that in your code.
Something similar to the following
SsqlDataSource1.InsertParameters("days").Value = strDays
I don't think it would be - Asked By Jon Wojtowicz on 28-Jul-05 05:43 AM
easier or harder, pretty much about the same when it comes right down to it. Users tend to like it better since they don't have to remember to hold the ctrl key down.
Sent the reply to myself - Asked By Jon Wojtowicz on 28-Jul-05 06:00 AM
doh!
I don't think it easier or harder, pretty much about the same when it comes right down to it. Users tend to like it better since they don't have to remember to hold the ctrl key down.
Would I - Asked By ben beethe on 28-Jul-05 07:44 AM
have to create 7 new field in my db? Monday thru Sunday? If so, what would I make my datatype, Integer?
I added - Asked By ben beethe on 28-Jul-05 07:51 AM
SsqlDataSource3.InsertParameters("days").Value = strDay
to the
Sub FormView1_ItemInserting
and now it is writing to the db with the correct value. But, I am still getting the error when it runs thru the
Sub FormView1_ItemInserted.
I don't understand why I am getting this error at all.
I am now using a checkboxlist but - Asked By ben beethe on 28-Jul-05 01:31 PM
having the same problem as with the listbox. I keep getting the same disassember problem. When I show the Disassembler, a yellow arrow is pointing to the 'nop'
00000046 nop
00000047 mov esi,dword ptr [ebp-10h]
<asp:FormView ID="FormView1" runat="server" DataKeyNames="class_name" DataSourceID="SqlDataSource3" OnItemUpdated="FormView1_ItemUpdated" OnItemInserted="FormView1_ItemInserted" GridLines="Both">