Question How to pass value from control to variable

norwedsh

New member
Joined
Apr 22, 2013
Messages
3
Programming Experience
1-3
Hello,

Bit of a newbie here.

I have an .aspx page with FormView data control that uses a table to organize the label controls.

I want to pass the value from the current value the label displays to a variable on the code behind.

I have assigned the label an ID; as an example "lbl_TestLabel"

I am having trouble with the syntax. I assumed it would be something like:

strVariable = Me.lbl_TestLabel.value

Regardless, the label ID is not recognized. I get an alert that the ID I provided is not a member of the page.

I feel that this should be something simple, or am I completely off track?

Thanks in advance.
 
Hello All,

I have done some additional research and it appears that the solution may lie in using VB Script on the same .aspx page that contains the FormView control.

Here is the code for the bound label control:
VB.NET:
[INDENT][COLOR=#800080]<td class="ColumnHeader2_Value">
      <asp:Label ID="lbl_PhoneStatus" runat="server" Text='<%# Eval("AvailableStatus") %>' />
</td>[/COLOR][/INDENT]

I created a very simple VB Script just to see if I could assign the value displayed by the label to a variable:

VB.NET:
[COLOR=#800080]    <script language="vbscript" runat="server">[/COLOR]

[COLOR=#800080]        Sub fv_AssignedEmpEquipment_Load()[/COLOR]
[COLOR=#800080]            Dim strAvStat As String[/COLOR]

[COLOR=#800080]            strAvStat = lbl_PhoneStatus.Value[/COLOR]
[COLOR=#800080]        End Sub[/COLOR]

[COLOR=#800080]    </script>[/COLOR]

fv_AssignedEmpEquipment is the ID I've assigned the FormView control

However, I'm still getting a message that says "'lbl_PhoneStatus' not declared. It may be inaccessible due its protection level."
 
Got some help from a coworker; here is the solution:

Use OnDataBinding to call the Event:
<asp:Label ID="lbl_AvailableStatus" runat="server"
OnDataBinding="lbl_AvailableStatus_DataBinding" Text='<%# Bind("AvailableStatus") %>' />

Then in the code-behind:

Public Sub lbl_AvailableStatus_DataBinding(sender As Object, e As EventArgs) Handles fv_AssignedEmpEquipment.DataBinding


Dim strAvStat As String
Dim lbl_AvailableStatus As New Label


lbl_AvailableStatus = DirectCast(Me.fv_AssignedEmpEquipment.FindControl("lbl_AvailableStatus"), Label)


If lbl_AvailableStatus IsNot Nothing Then


Your code here....


End If


End Sub
 
Last edited:
Back
Top