473,320 Members | 2,177 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

set usercontrol value

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?

Dec 4 '06 #1
4 1611
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.
--
Nick Wegner

"igotyourdotnet" wrote:
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?
Dec 5 '06 #2
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.

--
Nick Wegner

"Nick Wegner" wrote:
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.
--
Nick Wegner

"igotyourdotnet" wrote:
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?

Dec 5 '06 #3
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 (wa****@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.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/subscripti...t/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.

Dec 5 '06 #4
Walter, thanks for the improvement of my code. You are correct.
SelectedIndex is the best choice for this senerio.
--
Nick Wegner

"Walter Wang [MSFT]" wrote:
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 (wa****@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.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/subscripti...t/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.

Dec 5 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Will Gillen | last post by:
I know this has probably been asked before, but I can't seem to find a solid answer in any of the archives. First, before my question, please forgive my limited knowledge of the event lifecycle...
12
by: Joe | last post by:
Hello All: Do I have to use the LoadControl method of the Page to load a UserControl? I have a class which contains three methods (one public and two private). The class acts as a control...
2
by: lotus | last post by:
HI All.. I'm realtively new to C#. I have MainForm which includes Parent usercontol, and this parent usercontrol also contains child usercontrol. MainForm --> Parent usercontrol --> child...
3
by: Jeff | last post by:
Hey ASP.NET 2.0 This the ObjectDataSource in my UserControl, <asp:ObjectDataSource ID="odsMessage" runat="server" SelectMethod="ExecuteMessage" TypeName="AH.MyNetwork.BLL.Network.Message">...
6
by: MeowCow | last post by:
I have created a UserControl that encapsulates a third party data grid. My goal was to create my own DataSource and DataMember properties that forward the binding to the third party grid, then use...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.