Wizard Control - Issue 2

M

Morris Neuman

4/21/09 - sent to MSGroup - Wizard Control - #2

Hi,

I have a Wizard with several steps.

In Step 2 I have a FormView whose InsertItemTemplate contains a table that
has labels and textboxes that the user can update.
<asp:FormView ID="FormView1" runat="server"
OnPreRender="FV1_PreRender"
DefaultMode="Insert">
<InsertItemTemplate>
<span style="font-family: Verdana; font-size: 10pt;
color: navy;">
<table cellpadding="5" cellspacing="0" frame="border">
<tr>
<td>
<span style="font-family: Verdana;
font-size: 10pt; color: navy;">
<asp:Label ID="Label15" runat="server"
style="font-size: 8pt" Text="Status:"></asp:Label>
</span>
</td>
<td>
<span style="font-family: Verdana;
font-size: 10pt; color: navy;">
<asp:TextBox ID="TBStatus"
runat="server" Width="27px" Wrap="False"></asp:TextBox>
</span>
</td>
<td>
<asp:Label ID="Label14" runat="server"
Text="Label"></asp:Label>
</td>
<td>
</td>
</tr>
<tr>
<td>
<span style="font-family: Verdana;
font-size: 10pt; color: navy;">
<asp:Label ID="Label10" runat="server"
style="font-size: 8pt" Text="Line#:"></asp:Label>
</span>
</td>
<td>
<span style="font-family: Verdana;
font-size: 10pt; color: navy;">
<asp:TextBox ID="TBLine" runat="server"
Width="27px"></asp:TextBox>
</span>
</td>
<td style="width: 71px">
</td>
<td>
</td>
</tr>
<tr>
<td>
<span style="font-family: Verdana;
font-size: 10pt; color: navy;">
<asp:Label ID="Label11" runat="server"
style="font-size: 8pt" Text="Rings:"></asp:Label>
</span>
</td>
<td>
<span style="font-family: Verdana;
font-size: 10pt; color: navy;">
<asp:TextBox ID="TBRings" runat="server"
Width="52px"></asp:TextBox>
</span>
</td>
<td style="width: 71px">
</td>
<td>
</td>
</tr>
</table>
</span>
</InsertItemTemplate>
</asp:FormView>



In Step 4 I have a button whose click event attempts to access the info in
these textboxes.
The button click has the following:
string txtStatus = "";
string txtLine = "";
string txtRings = "";

FormView FV1 = FindControl("FormView1") as FormView;
TextBox tbStatus = FV1.FindControl("TBStatus") as TextBox;
txtStatus = tbStatus.Text;
TextBox tbLine = FV1.FindControl("TBLine") as TextBox;
txtLine = tbLine.Text;
TextBox tbRings = FV1.FindControl("TBRings") as TextBox;
txtRings = tbRings.Text;

When I run it there is no data found in FormView1 and I get
Error - Object reference not set to an instance of an object.
Referring to Line - TextBox tbStatus = FV1.FindControl("TBStatus") as
TextBox;


How can I access the data in Wizard Step4 from the textboxes in table in
FormView1 created in Wizard Step2 ?
 
T

Thomas Sun [MSFT]

Hi Morris,

From your description, I understand that you want to refer to the input
control that exists in InsertItemTemplate of FormView, and this FormView is
within Wizard control. But when you tried to use your code above to access
input control, you got the error message. If I have misunderstood you,
please feel free to let me know.

The error message indicates that FV1 is not an instance of FormView
control. In this case, we can access this FormView control directly from
codebehind class because the Web controls defined within the wizard's steps
can be accessed programmatically just like other control on the page.

For example:
===========================
<%@ Page Language="VB" %>

<script runat="server">

Protected Sub btnGetValue_Click(ByVal sender As Object, ByVal e As
System.EventArgs)

Dim tbStatus As TextBox =
DirectCast(FormView1.FindControl("TBStatus"), TextBox)
If tbStatus IsNot Nothing Then
Response.Write(tbStatus.Text)
End If
End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:wizard ID="Wizard1" runat="server" ActiveStepIndex="1">
<wizardsteps>
<asp:wizardstep ID="Wizardstep1" runat="server"
Title="Step 1">
<asp:FormView ID="FormView1" runat="server"
DefaultMode="Insert">
<InsertItemTemplate>
<span style="font-family: Verdana;
font-size: 10pt; color: navy;">
<table cellpadding="5" cellspacing="0"
frame="border">
<tr>
<td><span style="font-family:
Verdana; font-size: 10pt; color: navy;">
<asp:Label ID="Label15"
runat="server" Style="font-size: 8pt" Text="Status:"></asp:Label>
</span></td>
<td><span style="font-family:
Verdana; font-size: 10pt; color: navy;">
<asp:TextBox ID="TBStatus"
runat="server" Width="27px" Wrap="False"></asp:TextBox>
</span></td>
<td>
<asp:Label ID="Label14"
runat="server" Text="Label"></asp:Label>
</td>
<td></td>
</tr>
<tr>
<td><span style="font-family:
Verdana; font-size: 10pt; color: navy;">
<asp:Label ID="Label10"
runat="server" Style="font-size: 8pt" Text="Line#:"></asp:Label>
</span></td>
<td><span style="font-family:
Verdana; font-size: 10pt; color: navy;">
<asp:TextBox ID="TBLine"
runat="server" Width="27px"></asp:TextBox>
</span></td>
<td style="width: 71px"></td>
<td></td>
</tr>
<tr>
<td><span style="font-family:
Verdana; font-size: 10pt; color: navy;">
<asp:Label ID="Label11"
runat="server" Style="font-size: 8pt" Text="Rings:"></asp:Label>
</span></td>
<td><span style="font-family:
Verdana; font-size: 10pt; color: navy;">
<asp:TextBox ID="TBRings"
runat="server" Width="52px"></asp:TextBox>
</span></td>
<td style="width: 71px"></td>
<td></td>
</tr>
</table>
</span>
</InsertItemTemplate>
</asp:FormView>
</asp:wizardstep>
<asp:wizardstep ID="Wizardstep2" runat="server"
Title="Step2">
<asp:button ID="btnGetValue" runat="server"
OnClick="btnGetValue_Click" Text="Get Data From FormView" />
</asp:wizardstep>
</wizardsteps>
</asp:wizard>
&nbsp;
</div>
</form>
</body>
</html>
==================

Note: It would be better check whether object IsNot Nothing before
attempting to use it. The Page.FindControl method searches only the page's
immediate, or top-level, container; it does not recursively search for
controls in naming containers contained on the page. So it returns Nothing
for FormView control. For more information about Page.FindControl Method
(String), see http://msdn.microsoft.com/en-us/library/31hxzsdw.aspx.


I look forward to receiving your test results.


--
Best Regards,
Thomas Sun

Microsoft Online Partner Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

With newsgroups, MSDN subscribers enjoy unlimited, free support as opposed
to the limited number of phone-based technical support incidents. Complex
issues or server-down situations are not recommended for the newsgroups.
Issues of this nature are best handled working with a Microsoft Support
Engineer using one of your phone-based incidents.
==================================================

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

--------------------
 
M

Morris Neuman

Hi,

My page code is in C#. I tried the code you suggested (see below) in the
Step 4 button click event but it did not work.
TextBox tbStatus = DirectCast(FormView1.FindControl("TBStatus")) as TextBox;
I get error "The name "DirectCast" does not exist in the current context.

I am trying to do the following:

I have a 5 step wizard. Step 2 contains a formview whose insertitemtemplate
contains a table that has several textboxes. The data in these textboxes can
be updated by the user. In step 4, I have a button whose click event needs
to access this data in the formview/insertitemtemplate/table/textboxes.

If in step2 I have textboxes then I can access this info in step 4 but not
if the textboxes are in a table in a formview.

Any help would be appreciated.

--
Thanks
Morris


Thomas Sun said:
Hi Morris,

From your description, I understand that you want to refer to the input
control that exists in InsertItemTemplate of FormView, and this FormView is
within Wizard control. But when you tried to use your code above to access
input control, you got the error message. If I have misunderstood you,
please feel free to let me know.

The error message indicates that FV1 is not an instance of FormView
control. In this case, we can access this FormView control directly from
codebehind class because the Web controls defined within the wizard's steps
can be accessed programmatically just like other control on the page.

For example:
===========================
<%@ Page Language="VB" %>

<script runat="server">

Protected Sub btnGetValue_Click(ByVal sender As Object, ByVal e As
System.EventArgs)

Dim tbStatus As TextBox =
DirectCast(FormView1.FindControl("TBStatus"), TextBox)
If tbStatus IsNot Nothing Then
Response.Write(tbStatus.Text)
End If
End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:wizard ID="Wizard1" runat="server" ActiveStepIndex="1">
<wizardsteps>
<asp:wizardstep ID="Wizardstep1" runat="server"
Title="Step 1">
<asp:FormView ID="FormView1" runat="server"
DefaultMode="Insert">
<InsertItemTemplate>
<span style="font-family: Verdana;
font-size: 10pt; color: navy;">
<table cellpadding="5" cellspacing="0"
frame="border">
<tr>
<td><span style="font-family:
Verdana; font-size: 10pt; color: navy;">
<asp:Label ID="Label15"
runat="server" Style="font-size: 8pt" Text="Status:"></asp:Label>
</span></td>
<td><span style="font-family:
Verdana; font-size: 10pt; color: navy;">
<asp:TextBox ID="TBStatus"
runat="server" Width="27px" Wrap="False"></asp:TextBox>
</span></td>
<td>
<asp:Label ID="Label14"
runat="server" Text="Label"></asp:Label>
</td>
<td></td>
</tr>
<tr>
<td><span style="font-family:
Verdana; font-size: 10pt; color: navy;">
<asp:Label ID="Label10"
runat="server" Style="font-size: 8pt" Text="Line#:"></asp:Label>
</span></td>
<td><span style="font-family:
Verdana; font-size: 10pt; color: navy;">
<asp:TextBox ID="TBLine"
runat="server" Width="27px"></asp:TextBox>
</span></td>
<td style="width: 71px"></td>
<td></td>
</tr>
<tr>
<td><span style="font-family:
Verdana; font-size: 10pt; color: navy;">
<asp:Label ID="Label11"
runat="server" Style="font-size: 8pt" Text="Rings:"></asp:Label>
</span></td>
<td><span style="font-family:
Verdana; font-size: 10pt; color: navy;">
<asp:TextBox ID="TBRings"
runat="server" Width="52px"></asp:TextBox>
</span></td>
<td style="width: 71px"></td>
<td></td>
</tr>
</table>
</span>
</InsertItemTemplate>
</asp:FormView>
</asp:wizardstep>
<asp:wizardstep ID="Wizardstep2" runat="server"
Title="Step2">
<asp:button ID="btnGetValue" runat="server"
OnClick="btnGetValue_Click" Text="Get Data From FormView" />
</asp:wizardstep>
</wizardsteps>
</asp:wizard>

</div>
</form>
</body>
</html>
==================

Note: It would be better check whether object IsNot Nothing before
attempting to use it. The Page.FindControl method searches only the page's
immediate, or top-level, container; it does not recursively search for
controls in naming containers contained on the page. So it returns Nothing
for FormView control. For more information about Page.FindControl Method
(String), see http://msdn.microsoft.com/en-us/library/31hxzsdw.aspx.


I look forward to receiving your test results.


--
Best Regards,
Thomas Sun

Microsoft Online Partner Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

With newsgroups, MSDN subscribers enjoy unlimited, free support as opposed
to the limited number of phone-based technical support incidents. Complex
issues or server-down situations are not recommended for the newsgroups.
Issues of this nature are best handled working with a Microsoft Support
Engineer using one of your phone-based incidents.
==================================================

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

--------------------
 
T

Thomas Sun [MSFT]

Hi Morris,

Thanks for your response.

The version of demo above is in VB.NET. So you got the error in C# code.
The following code is in C#:

==========================
<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

protected void btnGetValue_Click(object sender, EventArgs e)
{
TextBox tbStatus = (TextBox)FormView1.FindControl("TBStatus");
if (tbStatus != null)
{
Response.Write(tbStatus.Text);
}
}
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:wizard ID="Wizard1" runat="server" ActiveStepIndex="0">
<wizardsteps>
<asp:wizardstep runat="server" Title="Step 1">
<asp:FormView ID="FormView1" runat="server"
DefaultMode="Insert">
<InsertItemTemplate>
<span style="font-family: Verdana;
font-size: 10pt; color: navy;">
<table cellpadding="5" cellspacing="0"
frame="border">
<tr>
<td><span style="font-family:
Verdana; font-size: 10pt; color: navy;">
<asp:Label ID="Label15"
runat="server" Style="font-size: 8pt" Text="Status:"></asp:Label>
</span></td>
<td><span style="font-family:
Verdana; font-size: 10pt; color: navy;">
<asp:TextBox ID="TBStatus"
runat="server" Width="27px" Wrap="False"></asp:TextBox>
</span></td>
<td>
<asp:Label ID="Label14"
runat="server" Text="Label"></asp:Label>
</td>
<td></td>
</tr>
<tr>
<td><span style="font-family:
Verdana; font-size: 10pt; color: navy;">
<asp:Label ID="Label10"
runat="server" Style="font-size: 8pt" Text="Line#:"></asp:Label>
</span></td>
<td><span style="font-family:
Verdana; font-size: 10pt; color: navy;">
<asp:TextBox ID="TBLine"
runat="server" Width="27px"></asp:TextBox>
</span></td>
<td style="width: 71px"></td>
<td></td>
</tr>
<tr>
<td><span style="font-family:
Verdana; font-size: 10pt; color: navy;">
<asp:Label ID="Label11"
runat="server" Style="font-size: 8pt" Text="Rings:"></asp:Label>
</span></td>
<td><span style="font-family:
Verdana; font-size: 10pt; color: navy;">
<asp:TextBox ID="TBRings"
runat="server" Width="52px"></asp:TextBox>
</span></td>
<td style="width: 71px"></td>
<td></td>
</tr>
</table>
</span>
</InsertItemTemplate>
</asp:FormView>
</asp:wizardstep>
<asp:wizardstep runat="server" Title="Step2">
<asp:button ID="btnGetValue" runat="server"
OnClick="btnGetValue_Click" Text="Get Data From FormView" />
</asp:wizardstep>
</wizardsteps>
</asp:wizard>
</div>
</form>
</body>
</html>
====================

I look forward to receiving your test results.
 
M

Morris Neuman

Thanks. That worked.

What is the difference between your code
TextBox tbStatus = (TextBox)FormView1.FindControl("TBStatus");
and the one I used below:
FormView FV1 = FindControl("FormView1") as FormView;
TextBox tbStatus = FV1.FindControl("TBStatus") as TextBox;

I have used this code before to first find the formview control then the
textbox within the formview and it has worked, so why did it not for the
wizard template?

Referencing data on different sections of a page is most confusing to me.
Any guidance would be much appreciated.
 
T

Thomas Sun [MSFT]

Hi Morris,

Thanks for your response and I am glad it works.
Thanks. That worked.

What is the difference between your code
TextBox tbStatus = (TextBox)FormView1.FindControl("TBStatus");
and the one I used below:
FormView FV1 = FindControl("FormView1") as FormView;
TextBox tbStatus = FV1.FindControl("TBStatus") as TextBox;

I have used this code before to first find the formview control then the
textbox within the formview and it has worked, so why did it not for the
wizard template?

Referencing data on different sections of a page is most confusing to me.
Any guidance would be much appreciated.

In this case, FormView1 is not in page's top-level container and when we
invoke FindControl method in codebehind class directly, it is using
Page.FindControl method. As I mentioned in my first post, the
Page.FindControl method searches only the page's immediate, or top-level,
container; it does not recursively search for controls in naming containers
contained on the page. So it returns Null for FormView control. There is
another method: Control.FindControl Method, which searches the control with
the specified ID in the current naming container.

Since FormView1 is in Wizard1's template, we also can use
Control.FindControl method to locate this FormView.

For example, we use Control.FindControl method:
=========
FormView FV1 = Wizard1.FindControl("FormView1") as FormView;
TextBox tbStatus = FV1.FindControl("TBStatus") as TextBox;
=========

For more information about using Page.FindControl, see
http://msdn.microsoft.com/en-us/library/31hxzsdw.aspx.
For more information about using Control.FindControl, see
http://msdn.microsoft.com/en-us/library/486wc64h.aspx.

I look forward to hearing from you.
 
M

Morris Neuman

Thanks for clarifiying. I will keep in mind as I continue with my website
development.
 
T

Thomas Sun [MSFT]

Hi Morris,
Thanks for clarifiying. I will keep in mind as I continue with my website
development.

I am glad that my reply can help you.
If you have any farther question, please feel free to post it here.
 

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

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top