set usercontrol value

G

Guest

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?
 
G

Guest

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.
 
G

Guest

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.
 
W

Walter Wang [MSFT]

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.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 ([email protected], 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.
 
G

Guest

Walter, thanks for the improvement of my code. You are correct.
SelectedIndex is the best choice for this senerio.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top