Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to get value of user control in code-behind file?

Status
Not open for further replies.

BG12424

Programmer
Jun 4, 2002
717
US
I am using VS.NET and Visual Basic as the source language.

In my code-behind(.vb file), how am I supposed to get the value from a user control that is a DropDownList on my aspx page.

I have it registered in my aspx file, but how am I supposed to reference that control in my code-behind file? Thank you.

regards,
Brian
 
someone just helped me out on this... here was my situation, it may help you...

thread855-426697
 
Thanks, that code help does give me some ideas, but I still need to know how to reference a user control within a code-behind file before I get to your step.
regards,
Brian
 
Well you are dragging that control onto your form right? If so then you have a variable that is referencing that control already.

If you are adding the control at run time then you obviously already have that variable set up. That'l do donkey, that'l do
[bravo] Mark
If you are unsure of forum etiquette check here faq796-2540
 
Dragging the user control to my design pad in VS.NET does bring the control into the HTML portion and puts the Register tag at the top. It doesn't put anything in the code-behind file though. Does there need to be a reference to that user control from within that class where all my logic is for the page? What is the syntax for it if it is? Thanks regards,
Brian
 
Ok I am a little confused here.
When you say user control you do mean one that you created right?
If so did you create it in another project?
If so then you need to add a reference to that project in your current project to be able to use that control.
If Not then.... O S#@$ I just went and checked some of my code and I don't have a reference to it either. You know what. I am going to take a look at this and get back to you.

In the meantime anyone who knows the answer don't feel shy about sharing it. That'l do donkey, that'l do
[bravo] Mark
If you are unsure of forum etiquette check here faq796-2540
 
This control is in the same project and yes, it is one that I created. Here is the control below.


Imports NewLeafTech

Public MustInherit Class Clients
Inherits System.Web.UI.UserControl
Protected WithEvents ddlClient As System.Web.UI.WebControls.DropDownList
Protected WithEvents lblClient As System.Web.UI.WebControls.Label

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

End Sub

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub

#End Region

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim o_Client As New NewLeafTech.ClientAdmin()
With ddlClient
.DataSource = o_Client.GetClientsAll
.DataTextField = &quot;ClientName&quot;
.DataValueField = &quot;ClientID&quot;
.DataBind()
End With
End Sub

End Class


<%@ Control Language=&quot;vb&quot; AutoEventWireup=&quot;false&quot; Codebehind=&quot;Clients.ascx.vb&quot; Inherits=&quot;NewLeafTech.Clients&quot; TargetSchema=&quot; %>
<asp:DropDownList id=&quot;ddlClient&quot; Font-Names=&quot;Verdana&quot; Font-Size=&quot;Smaller&quot; runat=&quot;server&quot; Width=&quot;199px&quot;></asp:DropDownList>


regards,
Brian
 
Haven't heard from anyone in awhile. Just wondering if someone can help out with this? Thanks regards,
Brian
 
Ok, let's say that my usercontrol is called 'myUC.ascx', then my class name (and therefore the usercontrol's name in the app's .dll) is myUC

So, put your user control on the .aspx page. Give it an ID. Let's get real creative here and call it myUC1.

Then, you need to set up a property in your user control, and have it access the value of the dropdown. So, call it dropdownvalue (or whatever), and have it do something like this for the get portion of the property:

dim output as object
output = myDropDownList.SelectedItem.value
return object

Something along those lines. Basically, you are providing an interface here so that a consumer (that's you) can get the value out.

Then, in your codebehind on the page where you wish to use it, you need to give it a declaration along with all the others that you'll see right under your class declaration:

Protected WithEvents myUC1 as myUC
'This is the ID that you gave it on the .aspx page
' with the type specified as the class name, myUC

Then, you can access the property of the usercontrol in your code:

myUC1.dropdownvalue

To get your value out.

It's all about the classes. Set up your classes, and share data between them via properties. It's how everything works in asp.net.

Just like your regular text boxes, right?

myTextBox.text

Just an object of type System.Web.UI.WebControls.TextBox with a property of .text that returns to the consumer (that's you again) the value that was entered by the user into the text box.

Hope that clears it up for you.

:)
paul
penny1.gif
penny1.gif

The answer to getting answered -- faq855-2992
 
Thanks Paul, I will try and follow your clear logic. regards,
Brian
 
Alright Paul, I know I'm close, but need a bit of help with some finishing touches. I am able to get the value displayed and passed to my stored procedure, but the drop down list does not retain the value that was selected. What am I missing here? Thanks

--------------------------------------------------------------------------
USER CONTROL SECTION - CONSOLIDATED THE CODE BREAKOUT
--------------------------------------------------------------------------
code behind part:

Imports NewLeafTech

Public MustInherit Class Clients
Inherits System.Web.UI.UserControl

Protected WithEvents ddlClient As System.Web.UI.WebControls.DropDownList
Public ClientValue As Integer
Public ClientValueIndex As Integer

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim o_Client As New NewLeafTech.Finance.ClientAdmin()

With ddlClient
.DataSource = o_Client.GetClientsAll
.DataTextField = &quot;ClientName&quot;
.DataValueField = &quot;ClientID&quot;

If Page.IsPostBack Then
ClientValue = .SelectedItem.Value
ClientValueIndex = .SelectedIndex
End If

.SelectedIndex = ClientValueIndex
.DataBind()

End With
End Sub

End Class

html part:
<%@ Control Language=&quot;vb&quot; AutoEventWireup=&quot;false&quot; Codebehind=&quot;Clients.ascx.vb&quot; Inherits=&quot;NewLeafTech.Clients&quot; TargetSchema=&quot; %>
<asp:DropDownList id=&quot;ddlClient&quot; Font-Names=&quot;Verdana&quot; Font-Size=&quot;Smaller&quot; runat=&quot;server&quot; Width=&quot;199px&quot;></asp:DropDownList>


--------------------------------------------------------------------------
WEB FORM SECTION - CONSOLIDATED THE CODE BREAKOUT
--------------------------------------------------------------------------

WebForm.aspx snippet:
code behind part:

Imports NewLeafTech

Public Class AddAccountsReceivable
Inherits System.Web.UI.Page
Protected WithEvents ucClients As Clients

Private Sub Page_Load(ByVal Sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

End Sub

Sub AddAR(ByVal Sender As Object, ByVal e As EventArgs)

Dim o_AR As New NewLeafTech.Finance.AccountsReceivable()
o_AR.Client = ucClients.ClientValue

End Sub

End Class


html part:
<%@ Page Language=&quot;vb&quot; AutoEventWireup=&quot;false&quot; Codebehind=&quot;AddAccountsReceivable.aspx.vb&quot; Inherits=&quot;NewLeafTech.AddAccountsReceivable&quot;%>
<%@ Register TagPrefix=&quot;NLT&quot; TagName=&quot;Clients&quot; Src=&quot;Clients.ascx&quot; %>
<HTML>
<body>
<form id=&quot;Form1&quot; method=&quot;post&quot; runat=&quot;server&quot;>
<NLT:Clients id=&quot;ucClients&quot; ClientValueIndex=&quot;2&quot; runat=&quot;server&quot; />
</form>
</body>
</HTML>
regards,
Brian
 
The problem is that your user control is getting loaded after viewState has been loaded. To avoid this problem, load the user control in the page_init event of the page...

This event comes before load_viewstate, and so after it loads, the viewstate comes into play and sets it to the previous value.

-paul
penny1.gif
penny1.gif

The answer to getting answered -- faq855-2992
 
Would I need to incorporate a placeholder control to do this? I am researching it now and it appears that I do. Something like:

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
ctlClient = DirectCast(LoadControl(&quot;Clients.ascx&quot;),Clients)
PlaceHolder1.Controls.Add(ctlClient)

End Sub

Would this be correct syntax to do this? Thanks regards,
Brian
 
You can shorten it a bit:

ctlClient = LoadControl(&quot;Clients.ascx&quot;)
PlaceHolder1.Controls.Add(ctlClient)

And yes, that's exactly the logic.
penny1.gif
penny1.gif

The answer to getting answered -- faq855-2992
 
I'm still unable to get the value to be selected after I submit my page. There must be something small in what I have code thus far. Could I please get a second, or third, fourth... glance at my code on how I have this written. Thank you

----------------------------------------------------------------------
CODE BEHIND FILE FOR ASPX file:
----------------------------------------------------------------------
Imports NewLeafTech

Public Class AddAccountsReceivable
Inherits System.Web.UI.Page

Protected WithEvents ucClients As Clients
Protected WithEvents PlaceHolder1 As System.Web.UI.WebControls.PlaceHolder

#Region &quot; Web Form Designer Generated Code &quot;

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

End Sub

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()

ucClients = LoadControl(&quot;Clients.ascx&quot;)
PlaceHolder1.Controls.Add(ucClients)
End Sub

#End Region

Private Sub Page_Load(ByVal Sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

End Sub

Sub AddAR(ByVal Sender As Object, ByVal e As EventArgs)

Dim o_AR As New NewLeafTech.Finance.AccountsReceivable()
o_AR.Client = ucClients.ClientValue

End Sub

End Class

----------------------------------------------------------------------
ASPX File:
----------------------------------------------------------------------
<form id=&quot;Form1&quot; method=&quot;post&quot; runat=&quot;server&quot;>
<asp:placeHolder id=&quot;PlaceHolder1&quot; runat=&quot;server&quot;></asp:placeHolder>
</form>

----------------------------------------------------------------------
Clients.ascx File
----------------------------------------------------------------------
Imports NewLeafTech

Public MustInherit Class Clients
Inherits System.Web.UI.UserControl

Protected WithEvents ddlClient As System.Web.UI.WebControls.DropDownList
Public ClientValue As Integer
Public ClientValueIndex As Integer

#Region &quot; Web Form Designer Generated Code &quot;

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

End Sub

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub

#End Region

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim o_Client As New NewLeafTech.Finance.ClientAdmin()

With ddlClient
.DataSource = o_Client.GetClientsAll
.DataTextField = &quot;ClientName&quot;
.DataValueField = &quot;ClientID&quot;

If Page.IsPostBack Then
ClientValue = .SelectedItem.Value
ClientValueIndex = .SelectedIndex
End If

.SelectedIndex = ClientValueIndex
.DataBind()

End With
End Sub

End Class
regards,
Brian
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top