Name not exists when it obviously does.

T

tshad

I am getting an error on a object name that doesn't exist (according to asp.net), but if you look at the trace, it does.

Here is the error:

*******************************************************************************************
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: BC30451: Name 'txtQuestion' is not declared.

Source Error:

Line 102: Dim positionID as Integer = Session("PositionID")
Line 103:
Line 104:trace.warn("txtQuestion = " & txtQuestion.text)
*******************************************************************************************

Here is the asp code showing it as part of the EditItemTemplate - so of course it doesn't exist when the page is first displayed(last line shows it):
******************************************************************************************
<EditItemTemplate>
<table border="1" width="100%" CellPadding="0" CellSpacing="0" style="margin:0">
<tr>
<td>
<asp:ImageButton ID="ExpandButton" runat="server" Font-Size="2" ImageUrl="../images/expand.gif" Width="16" Height="16" CommandName="Select" AlternateText="Click here to see details"></asp:ImageButton>
</td>
<td>
<asp:ImageButton ID="CollapseButton" visible="false" runat="server" Font-Size="2" ImageUrl="../images/collapse.gif" Width="16" Height="16" CommandName="Select" AlternateText="Click here to see details"></asp:ImageButton>
<asp:label id="PositionID" Visible="false" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "PositionID") %>' />
<asp:label id="QuestionUnique" Visible="false" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "QuestionUnique") %>' />
</td>
<td width="100%">

<asp:TextBox id="txtQuestion" columns="60" MaxLength="50" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Question") %>' />
*******************************************************************************************

Here is the trace.route showing it (I had to comment out the trace.warn statement to get it):

***************************************************************************************
DataList1System.Web.UI.WebControls.DataList5283100 DataList1:_ctl0System.Web.UI.WebControls.DataListItem9860 DataList1:_ctl0:_ctl2System.Web.UI.LiteralControl1340 DataList1:_ctl0:ExpandButtonSystem.Web.UI.WebControls.ImageButton1620 DataList1:_ctl0:_ctl3System.Web.UI.LiteralControl460 DataList1:_ctl0:CollapseButtonSystem.Web.UI.WebControls.ImageButton00 DataList1:_ctl0:_ctl4System.Web.UI.LiteralControl130 DataList1:_ctl0:positionIDSystem.Web.UI.WebControls.Label032 DataList1:_ctl0:_ctl5System.Web.UI.LiteralControl130 DataList1:_ctl0:QuestionUniqueSystem.Web.UI.WebControls.Label028 DataList1:_ctl0:_ctl6System.Web.UI.LiteralControl590
---> DataList1:_ctl0:txtQuestion System.Web.UI.WebControls.TextBox1650 DataList1:_ctl0:_ctl7System.Web.UI.LiteralControl650 DataList1:_ctl0:_ctl0System.Web.UI.WebControls.Button670 DataList1:_ctl0:_ctl8System.Web.UI.LiteralControl130 DataList1:_ctl0:_ctl1System.Web.UI.WebControls.Button670 DataList1:_ctl0:_ctl9System.Web.UI.LiteralControl1220 DataList1:_ctl0:DataGrid1System.Web.UI.WebControls.DataGrid0136 DataList1:_ctl0:_ctl10System.Web.UI.LiteralControl600 DataList1:_ctl1System.Web.UI.WebControls.DataListItem400 *****************************************************************************************

This does not show up on the first page (as we are not in edit mode yet), but I am getting the error before the first page is executed. We do not go into this subroutine except during editing mode - when the object would exist.

Why would I get the error at this time and how do I get around this?

Thanks

Tom
 
K

Karl Seguin

Tom,
It's hard to be sure without more context, but it seems pretty clear that
infact txtQuestion DOES NOT exist. What does exist is
DataList1:_ctl0:txtQuestion which is quite different. If you want to get a
reference to txtQuestion, you need to do a FindControl("txtQuestion")
within the edit template.

In other words, what you see as txtQuestion is a control embedded within
other controls (from what you've shown, that's a datalist containing an
editTemplate which contains a textbox). It therefore isnt simply available
as "txtQuestion". It only exists as "txtQuestion" as far as the edit
template is concerned. So to get it, you need to do:

private Sub Edit_Command(sender As Object, e As DataListCommandEventArgs)
...
dim txt as TextBox = ctype(e.Item.FindControl("txtQuestion"), TextBox)
...
end sub

Karl

P.S. - Option Strict is your friend in VB.Net


--
MY ASP.Net tutorials
http://www.openmymind.net/


I am getting an error on a object name that doesn't exist (according to
asp.net), but if you look at the trace, it does.

Here is the error:

****************************************************************************
***************
Description: An error occurred during the compilation of a resource
required to service this request. Please review the following specific error
details and modify your source code appropriately.

Compiler Error Message: BC30451: Name 'txtQuestion' is not declared.

Source Error:

Line 102: Dim positionID as Integer = Session("PositionID")
Line 103:
Line 104:trace.warn("txtQuestion = " & txtQuestion.text)
****************************************************************************
***************

Here is the asp code showing it as part of the EditItemTemplate - so of
course it doesn't exist when the page is first displayed(last line shows
it):
****************************************************************************
**************
<EditItemTemplate>
<table border="1" width="100%" CellPadding="0" CellSpacing="0"
style="margin:0">
<tr>
<td>
<asp:ImageButton ID="ExpandButton" runat="server" Font-Size="2"
ImageUrl="../images/expand.gif" Width="16" Height="16" CommandName="Select"
AlternateText="Click here to see details"></asp:ImageButton>
</td>
<td>
<asp:ImageButton ID="CollapseButton" visible="false"
runat="server" Font-Size="2" ImageUrl="../images/collapse.gif" Width="16"
Height="16" CommandName="Select" AlternateText="Click here to see
details"></asp:ImageButton>
<asp:label id="PositionID" Visible="false" runat="server"
Text='<%# DataBinder.Eval(Container.DataItem, "PositionID") %>' />
<asp:label id="QuestionUnique" Visible="false" runat="server"
Text='<%# DataBinder.Eval(Container.DataItem, "QuestionUnique") %>' />
</td>
<td width="100%">

<asp:TextBox id="txtQuestion" columns="60" MaxLength="50"
runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Question") %>'
/>
****************************************************************************
***************

Here is the trace.route showing it (I had to comment out the trace.warn
statement to get it):

****************************************************************************
***********
DataList1System.Web.UI.WebControls.DataList5283100
DataList1:_ctl0System.Web.UI.WebControls.DataListItem9860
DataList1:_ctl0:_ctl2System.Web.UI.LiteralControl1340
DataList1:_ctl0:ExpandButtonSystem.Web.UI.WebControls.ImageButton1620
DataList1:_ctl0:_ctl3System.Web.UI.LiteralControl460
DataList1:_ctl0:CollapseButtonSystem.Web.UI.WebControls.ImageButton00
DataList1:_ctl0:_ctl4System.Web.UI.LiteralControl130
DataList1:_ctl0:positionIDSystem.Web.UI.WebControls.Label032
DataList1:_ctl0:_ctl5System.Web.UI.LiteralControl130
DataList1:_ctl0:QuestionUniqueSystem.Web.UI.WebControls.Label028
DataList1:_ctl0:_ctl6System.Web.UI.LiteralControl590
---> DataList1:_ctl0:txtQuestion
System.Web.UI.WebControls.TextBox1650
DataList1:_ctl0:_ctl7System.Web.UI.LiteralControl650
DataList1:_ctl0:_ctl0System.Web.UI.WebControls.Button670
DataList1:_ctl0:_ctl8System.Web.UI.LiteralControl130
DataList1:_ctl0:_ctl1System.Web.UI.WebControls.Button670
DataList1:_ctl0:_ctl9System.Web.UI.LiteralControl1220
DataList1:_ctl0:DataGrid1System.Web.UI.WebControls.DataGrid0136
DataList1:_ctl0:_ctl10System.Web.UI.LiteralControl600
DataList1:_ctl1System.Web.UI.WebControls.DataListItem400
****************************************************************************
*************

This does not show up on the first page (as we are not in edit mode yet),
but I am getting the error before the first page is executed. We do not go
into this subroutine except during editing mode - when the object would
exist.

Why would I get the error at this time and how do I get around this?

Thanks

Tom
 
T

tshad

Karl Seguin said:
Tom,
It's hard to be sure without more context, but it seems pretty clear that
infact txtQuestion DOES NOT exist. What does exist is
DataList1:_ctl0:txtQuestion which is quite different. If you want to get
a
reference to txtQuestion, you need to do a FindControl("txtQuestion")
within the edit template.

In other words, what you see as txtQuestion is a control embedded within
other controls (from what you've shown, that's a datalist containing an
editTemplate which contains a textbox). It therefore isnt simply
available
as "txtQuestion". It only exists as "txtQuestion" as far as the edit
template is concerned. So to get it, you need to do:

private Sub Edit_Command(sender As Object, e As DataListCommandEventArgs)
...
dim txt as TextBox = ctype(e.Item.FindControl("txtQuestion"), TextBox)
...
end sub

That was it.

I keep forgetting that I need to get the control before I can use it.

Thanks,

Tom
 

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

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,160
Latest member
CollinStri
Top