Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

FormView ItemTemplate Textbox value

Posted on 2009-07-07
34
Medium Priority
?
1,566 Views
Last Modified: 2013-12-17
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.
0
Comment
Question by:Mr_Shaw
  • 13
  • 9
  • 9
  • +1
34 Comments
 
LVL 18

Expert Comment

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

0
 

Author Comment

by:Mr_Shaw
ID: 24794168
yes it is inside the ItemTemplate.

When I try and use FormView1.ItemTempalte the FindControl() is not listed in the intellicense.
0
 
LVL 22

Expert Comment

by:prairiedog
ID: 24794216
It seems that there is button on the page. Is it outside the FormView control?
0
Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

 
LVL 28

Expert Comment

by:strickdd
ID: 24794255
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
 
LVL 18

Expert Comment

by:David Robitaille
ID: 24794259
you dont need to use specify the ItemTempalte if it s the active template.
Coul i see some of your ASPX code?
0
 

Author Comment

by:Mr_Shaw
ID: 24794260
No. It is inside the FormView.

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

0
 

Author Comment

by:Mr_Shaw
ID: 24794308
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
 
LVL 18

Expert Comment

by:David Robitaille
ID: 24794334
"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
 

Author Comment

by:Mr_Shaw
ID: 24794427
I have created them using aspx
0
 
LVL 22

Assisted Solution

by:prairiedog
prairiedog earned 80 total points
ID: 24794457
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
 

Author Comment

by:Mr_Shaw
ID: 24794470
U CAN NOT BE SERIOUS...
0
 
LVL 18

Expert Comment

by:David Robitaille
ID: 24794488

please check the active mode in btnNext_Click
try something like this
Response.Write((FormView1.CurrentMode.toString());
0
 

Author Comment

by:Mr_Shaw
ID: 24794498
Why do I need

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

protected void btnNext_Click(object sender, EventArgs e)
{
}
0
 
LVL 22

Expert Comment

by:prairiedog
ID: 24794502
Because "sender" is an object.
0
 

Author Comment

by:Mr_Shaw
ID: 24794520
The answer to Response.Write(FormView1.CurrentMode.ToString());  is ReadOnly
0
 
LVL 22

Expert Comment

by:prairiedog
ID: 24794523
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
 
LVL 22

Expert Comment

by:prairiedog
ID: 24794561
BTW, what is your Page_Load?
0
 
LVL 18

Expert Comment

by:David Robitaille
ID: 24794594
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
 

Author Comment

by:Mr_Shaw
ID: 24794659
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
 
LVL 18

Expert Comment

by:David Robitaille
ID: 24794679
check your ID string.
Could we see the ItemTemplate from the ASPX???? PLEASE!!!!!!
0
 
LVL 22

Expert Comment

by:prairiedog
ID: 24794680
Do you bind your FormView in a "if !(IsPostBack)" block in Page_Load?
0
 

Author Comment

by:Mr_Shaw
ID: 24794687
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
 

Author Comment

by:Mr_Shaw
ID: 24794729
Nope.. I am not doing anything on postback
0
 
LVL 18

Accepted Solution

by:
David Robitaille earned 960 total points
ID: 24794738
Textbox_ID!!!!!!!!!!!!!!!!!!!!!!!
 
TextBox ID = (TextBox)FormView1.FindControl("Textbox_ID");
0
 
LVL 28

Assisted Solution

by:strickdd
strickdd earned 960 total points
ID: 24794761
FormView1.FindControl("Textbox_ID");
0
 
LVL 28

Expert Comment

by:strickdd
ID: 24794768
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
 

Author Comment

by:Mr_Shaw
ID: 24794805
U CAN NOT BE SERIOUS..........................
0
 
LVL 22

Expert Comment

by:prairiedog
ID: 24794907
I am sorry I have to refuse to take any credits because strickdd really deserves all credits.
0
 
LVL 18

Expert Comment

by:David Robitaille
ID: 24794954
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
 
LVL 22

Expert Comment

by:prairiedog
ID: 24795005
I am not a moderator, but I agree with davrob60. The points should be split between davrob60 and strickdd.
0
 

Author Comment

by:Mr_Shaw
ID: 24795641
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
 

Author Closing Comment

by:Mr_Shaw
ID: 31600579
thanks...
0
 
LVL 22

Expert Comment

by:prairiedog
ID: 24795692
>>>will both get 280 and prairiedog will get 20..
You can't do that because the total will 580 then. :-)
0
 
LVL 18

Expert Comment

by:David Robitaille
ID: 24795756
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

Featured Post

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Simulator games are perfect for generating sample realistic data streams, especially for learning data analysis. It is even useful for demoing offerings such as Azure stream analytics, PowerBI etc.
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

926 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question