Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / December 2006

Tip: Looking for answers? Try searching our database.

set usercontrol value

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
igotyourdotnet - 04 Dec 2006 21:03 GMT
I have a dropdown on my page and I want to define what should be selected in
the drop down when the page loads. I have a session variable I'm setting to
do this.
I can get the selected value and selectedItem.Text from the user control
dropdown, but how can I set what I want selected in the user control?
Nick Wegner - 05 Dec 2006 01:01 GMT
Hello,

If the listbox is in the usercontrol then you need to make a public property
on the usercontrol that you use to set the selectedItem property of the
listbox in the user control. Like this:

In your usercontrol do this:

Public Class WebUserControl1
   Inherits System.Web.UI.UserControl
   Protected WithEvents listbox1 As ListBox
   Private m_selectedValue As String = String.Empty

   Public Property SelectedValue() As String
       Get
           Return m_selectedValue
       End Get
       Set(ByVal Value As String)
           m_selectedValue = Value
       End Set
   End Property
   Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
       'Load you listbox here

       For Each item As ListItem In listbox1.Items
           If item.Value = m_selectedValue Then
               item.Selected = True
               Exit For
           End If
       Next
   End Sub

End Class

Now in your web page you can set the seletedValue property like this:

Protected WithEvents myUserControl As WebUserControl1 'This is your
usercontrol
 'This code goes in the Page_Load event

 yourUserControl.SelectedValue = Session("MySessionKey")

I would stay way from using the session state for something like this.  You
find that a querystring would work better in this situation.

Let me know if you have any questions.
Signature

Nick Wegner

> I have a dropdown on my page and I want to define what should be selected in
> the drop down when the page loads. I have a session variable I'm setting to
> do this.
> I can get the selected value and selectedItem.Text from the user control
> dropdown, but how can I set what I want selected in the user control?
Nick Wegner - 05 Dec 2006 01:08 GMT
Sorry your web page code should look like this:

Protected WithEvents myUserControl As WebUserControl1

'This code goes in the Page_Load event

 myUserControl.SelectedValue = Session("MySessionKey")

Let me know if you have any questions.

Signature

Nick Wegner

> Hello,
>
[quoted text clipped - 49 lines]
> > I can get the selected value and selectedItem.Text from the user control
> > dropdown, but how can I set what I want selected in the user control?
Walter Wang [MSFT] - 05 Dec 2006 03:23 GMT
Hi,

You can use following example code in your user control's load event:

       for (int i = 0; i < ddl1.Items.Count; i++)
       {
           if (ddl1.Items[i].Text == "2")
           {
               ddl1.SelectedIndex = i;
               break;
           }
       }

Note you'd better use SelectedIndex to set the selected item rather than
set an item's Selected property, it will cause error when two items both
have Selected property set to true.

Sincerely,
Walter Wang (wawang@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications. If you are using Outlook Express, please make sure you clear the
check box "Tools/Options/Read: Get 300 headers at a time" to see your reply
promptly.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
Nick Wegner - 05 Dec 2006 03:50 GMT
Walter, thanks for the improvement of my code.  You are correct.  
SelectedIndex is the best choice for this senerio.  
Signature

Nick Wegner

> Hi,
>
[quoted text clipped - 38 lines]
>
> This posting is provided "AS IS" with no warranties, and confers no rights.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.