I have a dropdown box with the value titleid. I also have a checkboxlist with the value of classificationid. How do I populate a checkboxlist when I change the selection in the dropdown?
I have this on pageload
Checkboxlist1.datasource="mystoredprocedure"
checkboxlist1.datatextfield = "description"
checkboxlist1.datavaluefield = "classificationid"
Checkboxlist.databind()
If checkboxlist1.items[i].value = "1' then
checkboxlist1.items[i].selected = true
else
checkboxlist1.items[i].selected = false
Do I need to put something in here to connect/bind to the dropdown box?
You need to handle the "SelectedIndexChanged" event of the dropdown, This event is raised when the selectedItem changes. Please also remember to set the "AutoPostBack" property of the dropdwn to true.
<asp:DropDownListID="DropDownList1"AutoPostBack="true"runat="server"OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"/>
protectedvoid DropDownList1_SelectedIndexChanged(object sender,EventArgs e)
{
//Bind the check box list here again and add any necessary logic you want might want
}s
If this helps please remember to mark post as answer.
Help I'm confused...
I have this as my dropdown:<asp:DropDownListID="DropDownList1"runat="server"DataSourceID="SqlDataSource1"
DataTextField="Title"DataValueField="TitleID"AutoPostBack="True"OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
</asp:DropDownList><br/><asp:CheckBoxListID="CheckBoxList1"runat="server"DataSourceID="SqlDataSource2"
DataTextField="description"DataValueField="classificationid"RepeatColumns="5"RepeatDirection="Horizontal"BorderStyle="Dashed"CellPadding="2"CellSpacing="2"Height="247px"Width="762px"BorderColor="#CFB37D">
</asp:CheckBoxList><br/>
What do I put on the code behind page for the SelectedIndexChange to bring back the items in the database? If the titleID is selected I want check marks in the checkboxes (classificationid) to show what the user has selected already. Please help!
ProtectedSub CheckBoxList1_SelectedIndexChanged(ByVal senderAsObject,ByVal eAs System.EventArgs)Handles CheckBoxList1.SelectedIndexChangedEndSub
Okay I've figured some things out... Here's my stored procedure:
CREATE Procedure GetClassID
@.titleid int
AS
SELECT
classifications.[description], titleclassification.classificationid
FROM
classifications inner join titleclassification on titleclassification.classificationid = classifications.classificationid
wheretitleid=@.titleid
GO
I'm trying to code the checkboxlist and having problems
ProtectedSub CheckBoxList1_SelectedIndexChanged(ByVal senderAsObject,ByVal eAs System.EventArgs)Handles CheckBoxList1.SelectedIndexChangedDim iAsInteger
Dim chkbxAs CheckBoxListFor i = 0To chkbx.Items.Count - 1
Next
if chkbx.Items[i].SelectedValuethen
CheckBoxList1.Items([i].selected =True)Else
CheckBoxList1.Items([i].selected =False)EndIf
CheckBoxList1.DataBind()EndSub
what am I doing wrong?
Ok, i want to understand one thing. Based on which field do you want the items to be checked? You have 2 fields in your select SP, you need to have a third one and based on it, you set the the items to be checked or not.
I want the classificationid field to be checked. What's the other field to add and how do I set the items to be checked or not? Do I need a Boolean?
if your sql database classificationid field is set to 'bit' you wont need to do anything except checkboxlist1.databind()
it knows 1's are checks and vice versa.
edit: ok so you are using stored procedures. ive never done it that way. what i would do, which is pretty simple imo, is create a fresh checkbox list. let it create a sqldatasource for it. i use the nice little sql generator thing when i create it. select the 2 columns from your database. click the where button and use the control selected index thing. i hope im not being to vague. then lastly you will have to set the dropdownlist to autopostback.
Okay I tried it your way and got the same thing as my stored procedure but how do I get the items to be checked? What do I put for the codebehind
ProtectedSub CheckBoxList1_SelectedIndexChanged(ByVal senderAsObject,ByVal eAs System.EventArgs)Handles CheckBoxList1.SelectedIndexChanged
That's where I'm lost.
its not checkbox1 seletedindexchanged, its dropdownlist1 selectedindex changed. in that code snippet just put checkboxlist1.databind(). when a checkbox list is bound to a bit field, it will check the boxes that are true
please post your most recent aspx page code so i can look at your sqldatasource.
I can't change my classificationid field to a bit field because it will change the entire structure of my database. Ugh. Do I write a function like so to convert to a bit. Is this one okay.
CREATE FUNCTION [Converttobool]
(@.value char(1))
RETURNS bit
BEGIN
declare @.returnvalue bit
if @.value = 'y'
set @.returnvalue = 1
else
set @.returnvalue= 0
return @.returnvalue
END
i think you can just do it in your select statement
cast(columnname, bit)
This is what I have thus far it brings back my items from the database but it's not checked, any ideas?
ProtectedSub DropDownList1_SelectedIndexChanged(ByVal senderAsObject,ByVal eAs System.EventArgs)Handles DropDownList1.SelectedIndexChangedDim iAsInteger
For i = 0To CheckBoxList1.Items.Count - 1If (CheckBoxList1.Items(i).Selected)Then
Response.Write(CheckBoxList1.Items(i).Value)
EndIf
Next
EndSub
you dont need to manually check anything if youve databound it to the sqldatasource... its just checkboxlist1.databind()
Added it cast to my select statement
SELECT
classifications.[description], cast(titleclassification.classificationid as bit)
FROM
classifications inner join titleclassification on titleclassification.classificationid = classifications.classificationid
wheretitleid=@.titleid
Now I'm getting ...Databinding: System.Data.DataRowView does not contain a property with the name classificationid. Any suggestions?
Okay I got rid of the databinding error message, page comes up but still no items are checked? I'm going. I feel that I'm almost there any suggestions?
Here's my select statement:
SELECT
classifications.[description], cast(titleclassification.classificationid as bit)
FROM
classifications inner join titleclassification on titleclassification.classificationid = classifications.classificationid
wheretitleid=@.titleid
What do I put here?
ProtectedSub DropDownList1_SelectedIndexChanged(ByVal senderAsObject,ByVal eAs System.EventArgs)Handles DropDownList1.SelectedIndexChangedCheckBoxList1.DataBind()
EndSubThanks if anyone can assist me!
0 comments:
Post a Comment