Wrox Home  
Search P2P Archive for: Go

  Return to Index  

aspx_beginners thread: Dropdownlist and my selected item from my database


Message #1 by miss ruby <unix_box@y...> on Mon, 13 May 2002 18:59:37 -0700 (PDT)
hello..

I'm using dropdownlist control that the items is from
my database. Now i'm in edit mode. So how can i make
my
value from my database is selected in the
dropdownlist???



thanks...




=====
miss ruby
malaysia

__________________________________________________
Do You Yahoo!?
LAUNCH - Your Yahoo! Music Experience
http://launch.yahoo.com
Message #2 by "Dogers" <dogers@t...> on Tue, 14 May 2002 18:14:45 +0100
> I'm using dropdownlist control that the items is from
> my database. Now i'm in edit mode. So how can i make
> my
> value from my database is selected in the
> dropdownlist???

I take it you mean youre storing the value the listbox contains in the
database, and you want to pull it out and have that one selected
again..?

If so, you need this crazy bit of code-
List1.SelectedItem 
List1.Items.IndexOf(List1.Items.FindByValue(objDataReader("youritem")))

(assuming youre using a datareader.. Replace it with whatever datastore
you want!) there must be a faster way of doing this, which id certainly
like to know, but the above works fine for me!

-- 
Dogers (dogers@t...)
http://www.the-marines.com

Message #3 by miss ruby <unix_box@y...> on Tue, 14 May 2002 23:30:58 -0700 (PDT)

thanks Dogers... i got it..

emm.. it's about dropdownlist.. how about others
control like checkboxlist and optionlistbox???

it's i will use the same coding??
anyway, i will test it...!!!


thankss...




--- Dogers <dogers@t...> wrote:
> > I'm using dropdownlist control that the items is
> from
> > my database. Now i'm in edit mode. So how can i
> make
> > my
> > value from my database is selected in the
> > dropdownlist???
> 
> I take it you mean youre storing the value the
> listbox contains in the
> database, and you want to pull it out and have that
> one selected
> again..?
> 
> If so, you need this crazy bit of code-
> List1.SelectedItem 
>
List1.Items.IndexOf(List1.Items.FindByValue(objDataReader("youritem")))
> 
> (assuming youre using a datareader.. Replace it with
> whatever datastore
> you want!) there must be a faster way of doing this,
> which id certainly
> like to know, but the above works fine for me!
> 
> -- 
> Dogers (dogers@t...)
> http://www.the-marines.com
> 
> 


=====
miss ruby
malaysia

__________________________________________________
Do You Yahoo!?
LAUNCH - Your Yahoo! Music Experience
http://launch.yahoo.com
Message #4 by "Dogers" <dogers@t...> on Wed, 15 May 2002 12:51:41 +0100
> emm.. it's about dropdownlist.. how about others
> control like checkboxlist and optionlistbox???

Ive not used those, however if provide direct access to the checkboxes
and option controls inside them, then its just "checkbox.checked 
true".. If not, then youll need to have a look through the class browser
to find out what options it actually has :)

-- 
Dogers (dogers@t...)
http://www.the-marines.com

Message #5 by "Rich Lipscomb" <rlipscomb@h...> on Wed, 15 May 2002 17:32:11
I have the exact same situation - The grid renders fine, but when I go 
into edit mode, the dropdown displays the first item in the list.  Where 
would I place your code exmaple: List1.SelectedItem ...?  Here's my column 
containing the dropdown:

<EditItemTemplate>
  <asp:DropDownList runat="server" id="ddlCategory" 
   DataSource='<%# GetCategory() %>'
   DataTextField = 'Cname'	
   DatavalueField = 'Cname'				
   SelectedItem = '<%#GetSelectedCategory(DataBinder.Eval 
(Container.DataItem, "Category")) %>'/>
</EditItemTemplate>
</asp:TemplateColumn>


> > I'm using dropdownlist control that the items is from
> my database. Now i'm in edit mode. So how can i make
> my
> value from my database is selected in the
> dropdownlist???

I take it you mean youre storing the value the listbox contains in the
database, and you want to pull it out and have that one selected
again..?

If so, you need this crazy bit of code-
List1.SelectedItem 
List1.Items.IndexOf(List1.Items.FindByValue(objDataReader("youritem")))

(assuming youre using a datareader.. Replace it with whatever datastore
you want!) there must be a faster way of doing this, which id certainly
like to know, but the above works fine for me!

-- 
Dogers (dogers@t...)
http://www.the-marines.com

Message #6 by "Dogers" <dogers@t...> on Wed, 15 May 2002 23:39:06 +0100
>    SelectedItem = '<%#GetSelectedCategory(DataBinder.Eval 
> (Container.DataItem, "Category")) %>'/>

Assuming GetSelectedCategory is one of your functions, and returns the
index, you need to change it so-
"SelectedItem='<%
List1.Items.IndexOf(List1.Items.FindByValue(GetSelectedCategory(DataBind
er.Eval(Container.DataItem, "Category")))) %>'/>"

(not sure what that hash is infront of your function name, so you may or
may not need it ;) )

-- 
Dogers (dogers@t...)
http://www.the-marines.com

Message #7 by "Rich Lipscomb" <rlipscomb@h...> on Thu, 16 May 2002 02:19:52
Thanks for the help.  I'm still stuck on on the GetSelectedCategory 
function.  I'm trying to get it to return the actual string value so that 
when you go into edit mode, this value will be selected in the DDL.  Am I 
on the right track?  Or should should the function calculate the index 
value of the DDL list?  Either way, any type of code example would be 
greatly appreciated.

Rich
 
> >    SelectedItem = '<%#GetSelectedCategory(DataBinder.Eval 
> (Container.DataItem, "Category")) %>'/>

Assuming GetSelectedCategory is one of your functions, and returns the
index, you need to change it so-
"SelectedItem='<%
List1.Items.IndexOf(List1.Items.FindByValue(GetSelectedCategory(DataBind
er.Eval(Container.DataItem, "Category")))) %>'/>"

(not sure what that hash is infront of your function name, so you may or
may not need it ;) )

-- 
Dogers (dogers@t...)
http://www.the-marines.com

Message #8 by "Rich Lipscomb" <rlipscomb@h...> on Thu, 16 May 2002 02:59:44
I found an entirely different solution that I can almost understand:

Sub MyDataGrid_ItemDataBound(Sender As Object, E As DataGridItemEventArgs)

Dim itemType As ListItemType = e.Item.ItemType
If itemType = ListItemType.EditItem Then	
  Dim myList As DropDownList = CType(e.Item.FindControl("ddlCategory"),   
DropDownList)
  Dim myRow As DataRowView = CType(e.Item.DataItem, DataRowView)
  Dim myCat As String = myRow("Category").ToString()			
  
  myList.DataTextField = "Cname"
  myList.DataValueField = "Cname"
  myList.DataSource = GetCategory()
  myList.DataBind()
  myList.Items.FindByValue(myCat).Selected = True
End If
End Sub

<EditItemTemplate>
   <asp:dropdownlist id="ddlCategory" runat="server"/>
</EditItemTemplate>


I hope this helps someone.

> Thanks for the help.  I'm still stuck on on the GetSelectedCategory 
f> unction.  I'm trying to get it to return the actual string value so 
that 
w> hen you go into edit mode, this value will be selected in the DDL.  Am 
I 
o> n the right track?  Or should should the function calculate the index 
v> alue of the DDL list?  Either way, any type of code example would be 
g> reatly appreciated.

> Rich
 > 
>>  >    SelectedItem = '<%#GetSelectedCategory(DataBinder.Eval 
>>  (Container.DataItem, "Category")) %>'/>

> Assuming GetSelectedCategory is one of your functions, and returns the
i> ndex, you need to change it so-
"> SelectedItem='<%
L> ist1.Items.IndexOf(List1.Items.FindByValue(GetSelectedCategory(DataBind
e> r.Eval(Container.DataItem, "Category")))) %>'/>"

> (not sure what that hash is infront of your function name, so you may or
m> ay not need it ;) )

> -- 
D> ogers (dogers@t...)
h> ttp://www.the-marines.com


  Return to Index