FormView ItemTemplate Textbox value

I am trying to get the value of a textbox which is part of a FormView, ItemTemplate.

My button code is.

protected void btnNext_Click(object sender, EventArgs e)
{
TextBox ID = (TextBox)FormView1.FindControl("ID");
Response.Write((ID.Text.ToString());
}

I am getting an error

"Object reference not set to an instance of an object."

i am not sure how to fix this.
Mr_ShawAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

David RobitailleAnalyst ProgrammerCommented:
you got that because "FormView1.FindControl("ID");" return nothing.
is the "ID" textbox inside something else? (please send some fo the ASPX code)

0
Mr_ShawAuthor Commented:
yes it is inside the ItemTemplate.

When I try and use FormView1.ItemTempalte the FindControl() is not listed in the intellicense.
0
prairiedogCommented:
It seems that there is button on the page. Is it outside the FormView control?
0
Amazon Web Services

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

strickddCommented:
A couple notes:

1) FormView controls can have an object (i.e. TextBox) with the same ID in each of the three templates.
2) To get the object, you have to use MyFormView.FindControl(controlID). (as you already know)
3) If the object with the specified controlID does not exist in the current template, .FindControl will return null.

My guess is that the template the you have active is not the one you think is active (i.e. you think the ItemTemplate is displayed when really the InsertTemplate is). If you are doing any dynamic creation or setting of the active mode/template, make sure that is maintained on the postback.

If you set a breakpoint in the OnClick event and run in debug, then when you get to the breakpoint, you can open the Immediate window (View > Other Windows > Immediate i believe). Here you can type in the FormViewID.<Propery> and find out what the active mode is.

Make sure that the object you are doing the FindControl on exists in all three templates (Add, Edit, Item). Also verify that the ID you are using for FindControl is correct (case and spelling).

If this doesn't work, let me know.
0
David RobitailleAnalyst ProgrammerCommented:
you dont need to use specify the ItemTempalte if it s the active template.
Coul i see some of your ASPX code?
0
Mr_ShawAuthor Commented:
No. It is inside the FormView.

I am running this code off a button in the PagerTemplate.

0
Mr_ShawAuthor Commented:
Hi strickdd,

I think what you said applies to my case.

I am note sure what you mean by "make sure that is maintained on the postback".

How do i do this?
0
David RobitailleAnalyst ProgrammerCommented:
"dynamic creation" : do you Crate new textbox on do you put them in the aspx???
if you do this, you need to "recreate" them on each postback.
http://www.4guysfromrolla.com/articles/092904-1.aspx
 
0
Mr_ShawAuthor Commented:
I have created them using aspx
0
prairiedogCommented:
You will need to use this code to find the TextBox:
Button btn = (Button)sender;
FormViewPagerRow pager = (FormViewPagerRow)btn.NamingContainer;
FormView fv = (FormView)pager.NamingContainer;
TextBox txt = (TextBox)fv.FindControl("ID");

Open in new window

0
Mr_ShawAuthor Commented:
U CAN NOT BE SERIOUS...
0
David RobitailleAnalyst ProgrammerCommented:

please check the active mode in btnNext_Click
try something like this
Response.Write((FormView1.CurrentMode.toString());
0
Mr_ShawAuthor Commented:
Why do I need

 Button btn = (Button)sender; if i am writing my code within

protected void btnNext_Click(object sender, EventArgs e)
{
}
0
prairiedogCommented:
Because "sender" is an object.
0
Mr_ShawAuthor Commented:
The answer to Response.Write(FormView1.CurrentMode.ToString());  is ReadOnly
0
prairiedogCommented:
If you want, you can combine the first two lines together:
FormViewPagerRow pager = (FormViewPagerRow)(sender.NamingContainer, FormViewPagerRow);
Even more, you can combine the first three lines together if you want to. I separate them to be more readable.
0
prairiedogCommented:
BTW, what is your Page_Load?
0
David RobitailleAnalyst ProgrammerCommented:
ok, it answer strickdd question.
Did you try prairiedog`s way. i used that strategy numbers of times...
Are you sure the text box is not insde somethine more that the ItemTemplate, like a panel?
Also, are you sure of the ID for your textbox? try a cut and paste. It look stupid, but i dont know the number of time i did that error.
0
Mr_ShawAuthor Commented:
Button btn = (Button)sender;
FormViewPagerRow pager = (FormViewPagerRow)btn.NamingContainer;
FormView fv = (FormView)pager.NamingContainer;
TextBox txt = (TextBox)fv.FindControl("ID");

"Object reference not set to an instance of an object."
0
David RobitailleAnalyst ProgrammerCommented:
check your ID string.
Could we see the ItemTemplate from the ASPX???? PLEASE!!!!!!
0
prairiedogCommented:
Do you bind your FormView in a "if !(IsPostBack)" block in Page_Load?
0
Mr_ShawAuthor Commented:
Ok due to the nature of the project I can not give you all the code... here is a snap shot of the code
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="peer_assement.aspx.cs" Inherits="_Default" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        .style1
        {
            width: 100%;
        }
        .style5
        {
            width: 408px;
        }
        .style6
        {
            width: 247px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
    </div>
    <br />
    Speciality Description:
    <asp:DropDownList ID="DropDownList_SpecDescription" runat="server" 
        AutoPostBack="True" DataSourceID="SqlDataSource2" DataTextField="Specialty" 
        DataValueField="Spec_Code" Height="26px" Width="277px">
        <asp:ListItem></asp:ListItem>
    </asp:DropDownList>
    <asp:SqlDataSource ID="SqlDataSource2" runat="server" 
        ConnectionString="<%$ ConnectionStrings:ReferralConnectionString %>" 
        SelectCommand="SpecialtyLookup" SelectCommandType="StoredProcedure">
    </asp:SqlDataSource>
&nbsp;Status:
    <asp:DropDownList ID="DropDownList_Status" runat="server" AutoPostBack="True" 
        Width="130px" Height="26px">
        <asp:ListItem>Reviewed</asp:ListItem>
        <asp:ListItem Value="Not Reviewed">Not Reviewed</asp:ListItem>
    </asp:DropDownList>
<hr />
    <br />
    <asp:FormView ID="FormView1" runat="server" 
        AllowPaging="True" DataSourceID="PeerAssementDB" Width="1244px" 
        ondatabound="DataBound">
<PagerTemplate>
<asp:Button ID="btnFirst" runat="server" Width="20" CommandName="Page" CommandArgument="First" Text="|<" />
<asp:Button ID="btnPrev" runat="server" Width="20" CommandName="Page" CommandArgument="Prev" Text="<" />
<asp:Label runat="server" Font-Size="Smaller" Text="&nbsp;Record:"/>
<asp:Label ID="Label_RecordTotal" runat="server" Font-Size="Smaller" 
        onprerender="Label_RecordTotal_PreRender"/>
<asp:Button ID="btnNext" runat="server" Width="20" CommandName="Page" 
        CommandArgument="Next" Text=">" onclick="btnNext_Click" />
<asp:Button ID="btnLast" runat="server" Width="20" CommandName="Page" CommandArgument="Last" Text=">|" />
 
</PagerTemplate>
 
        <PagerSettings Mode="NextPreviousFirstLast" />
        
        
    <ItemTemplate>
                <asp:TextBox ID="Textbox_ID" runat="server" ReadOnly="True" Text='<%# Bind("ID") %>'></asp:TextBox>
        </ItemTemplate>       
    </asp:FormView>
    
    
    </form>
</body>
</html>

Open in new window

0
Mr_ShawAuthor Commented:
Nope.. I am not doing anything on postback
0
David RobitailleAnalyst ProgrammerCommented:
Textbox_ID!!!!!!!!!!!!!!!!!!!!!!!
 
TextBox ID = (TextBox)FormView1.FindControl("Textbox_ID");
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
strickddCommented:
FormView1.FindControl("Textbox_ID");
0
strickddCommented:
davrob60 beat me to it, sorry, but as I mentioned you should make sure that the ID is spelled correctly and cased correctly. You just had the wrong ID.
0
Mr_ShawAuthor Commented:
U CAN NOT BE SERIOUS..........................
0
prairiedogCommented:
I am sorry I have to refuse to take any credits because strickdd really deserves all credits.
0
David RobitailleAnalyst ProgrammerCommented:
I agree, must strickdd deserve must of the credit, but could i get an assist because i<m the one who insisted to see the ASPX?
0
prairiedogCommented:
I am not a moderator, but I agree with davrob60. The points should be split between davrob60 and strickdd.
0
Mr_ShawAuthor Commented:
ok... I really want to give credits to all those who spend time helping me.

I tell you what I will do davrob60 and strickdd. will both get 280 and prairiedog will get 20..

How about that?
0
Mr_ShawAuthor Commented:
thanks...
0
prairiedogCommented:
>>>will both get 280 and prairiedog will get 20..
You can't do that because the total will 580 then. :-)
0
David RobitailleAnalyst ProgrammerCommented:
You are the Author, you are the master of those beloved points.  We as experts, are here, mouth open, like little bird waiting for worms from our mother bird. I wont object to any split you make.
But, no matters what he said, I think Prairiedog deserve his share of points.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.