locked
Accessing the TextBoxes in a FormView from C# code behind RRS feed

  • Question

  • User1618321365 posted

    I'd like to set the values in the text boxes in a FormView when I am adding a new record to a database. I have attempted to use the New_Clicked event for the EditItemTemplate which gives me a procedure in the C#. However, I cant seem to find the TextBox objects that are on the EditItemTemplate from the C#. I see the FormView1 object and can get to FormView1.EditItemTemplate -- but then I can access the textBoxes.

     I see the construct "FormView.EditItemTemplate.EditItemTemplate.yyyTextBox" in the properties window for the TextBox -- but I cant access the TextBox from C# using this either ...

     

    In the C# code I'd like to be able to express

    ...yyyTextBox.Text = "zzzzz";

    and have this show up as the default in the TextBox.

         ---Jim 

    Sunday, March 2, 2008 8:46 AM

Answers

  • User1564875471 posted

    I forget to change the TextBox Id  to "DateTimeStampTextBox"

     

    try this code :

     

     protected void FormView1_PreRender(object sender, System.EventArgs e)
     {
         // if the mode is Edit or Insert
         if (this.FormView1.CurrentMode == FormViewMode.Edit || this.FormView1.CurrentMode == FormViewMode.Insert) {
             TextBox yyyTextBox = FormView1.FindControl("DateTimeStampTextBox") as TextBox;
             yyyTextBox.Text = "02/10/2008";
         }
     }
      
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, March 2, 2008 3:11 PM

All replies

  • User1759999623 posted
    To access child controls from their parent control, you need to use the FindControl method:
    TextBox yyyTextBox = (TextBox)FormView1.FindControl("yyyTextBox");
    yyyTextBox.Text = "zzzzz";
    Now you have control of your TextBox and its properties and methods.
    Sunday, March 2, 2008 8:58 AM
  • User1564875471 posted

    you need to use Findcontrol function of the FormView to get a reference to its controls

    try this :

     

         if (FormView1.CurrentMode == FormViewMode.Edit) 
           {
             TextBox yyyTextBox = (TextBox)FormView1.FindControl("yyyTextBox");
           
             // here you can access the yyyTextBox control
             yyyTextBox.Text = " any text";
           }
      
     
    hope it helps 
      
    Sunday, March 2, 2008 9:01 AM
  • User537870505 posted
    Try FormView.FindControl("txtBoxId")
    Sunday, March 2, 2008 9:01 AM
  • User1618321365 posted

    Thanks for the excellent responses ... tried the suggestions and am getting closer. I inserted the code below. 

    When I execute the page, I click the "New" link in the bottom of the "item" template from my FormView and using the debugger, I can look at the c# code results. If I leave off the if clause, the  yyyTextBox variable ends up with a null value (guess it cand frin the child control). If I add the if test, it doesnt pass -- so i guess ...

    FormView1.CurrentMode != FormViewMode.Edit.

    I cant seem to find how to get this situation to occur. Possible the FormView1.CurrentMode isnt changed to Edit status at the event iof clicking "New".

        ---Jim 

     

     

    protected void NewButton_Click(object sender, EventArgs e)

    {

    if (FormView1.CurrentMode == FormViewMode.Edit)

    {

    TextBox yyyTextBox = (TextBox)FormView1.FindControl("DateTimeStampTextBox");yyyTextBox.Text = "02/10/2008";

    }

    }

    Sunday, March 2, 2008 10:43 AM
  • User1564875471 posted

    you need to put that code in ModeChanging event instead ,

     

     protected void FormView1_ModeChanging(object sender, System.Web.UI.WebControls.FormViewModeEventArgs e)
     {
        
         // if the new mode is edit
         if (e.NewMode == FormViewMode.Edit) {
            //here you can access the controls inside EditItemTemplate
             TextBox yyyTextBox = FormView1.FindControl("DateTimeStampTextBox") as TextBox ;
             yyyTextBox.Text = "02/10/2008";
         }
        
         // if the new mode is insert
         else if (e.NewMode == FormViewMode.Insert) {
             //here you can access the controls inside InsertItemTemplate
             //Dim yyyTextBox As TextBox = FormView1.FindControl("DateTimeStampTextBox") as TextBox ;
             //yyyTextBox.Text = "02/10/2008"
         }
        
     }

     

    Hope it helps 

    Sunday, March 2, 2008 11:11 AM
  • User1618321365 posted

    After investigating further --- I see the problem seems to be that there are two TextBoxes for each item in the formView -- one for the Edit Template and another for the Insert Template. When you use the findControl method, I need to describe which template i am referring to. thats what the if clause was doing -- picking out the right template. However, when I use the New_Clicked event, seems the FormViewMode hasnt changed to Edit at that time --- so I need a new event to trigger my C#??

         --jim

    Sunday, March 2, 2008 11:14 AM
  • User1618321365 posted

    Ahhh -- so close --- the following code was used for my codeBhind. Now I get through the tests for FormViewMode and execute the FindControl method. However, It doesnt seem to find the TextBox control. The value for yyyTextBox is returned NULL and the code halts -- telling me about the NUll value. I went into the properties of the TextBox for the Template and actually cut the value for "ID" and then pasted it into the C# expression -- so Im sure i didnt have a missspelling.

    Thanks for your excellent and timely assistance. -- jim

     

    protected void FormView1_ModeChanging(object sender, FormViewModeEventArgs e)

    {

    if (e.NewMode == FormViewMode.Edit)

    {

    TextBox yyyTextBox = FormView1.FindControl("DateTimeStampTextBox") as TextBox;yyyTextBox.Text = "02/10/2008";

    }

    if (e.NewMode == FormViewMode.Insert)

    {

    TextBox yyyTextBox = FormView1.FindControl("DateTimeStampTextBox") as TextBox;

    yyyTextBox.Text = "02/10/2008";

    }

    }

    Sunday, March 2, 2008 11:52 AM
  • User1618321365 posted

    as another note -- i see from some other forums that the fact that I am using a masterpage may be an issue. I see some discussions regarding recursive usage of the FindControl() ... basically my simple test page places a formview onto the contentplaceholder of my testpage, sets up the sqldatasource to allow edit/delete/insert ... all works fine for the basic purpose. So my intent is to simply cause stuff determined in my C# codebehind to create data for any NEW entry to the database.

         ---jim

    Sunday, March 2, 2008 12:56 PM
  • User1564875471 posted

     Jim, it seems that you need to use FormView  PreRender event instead of modeChanged ,

    PreRender event fires later and so the Controls must be there in the Template, try  the following code :

     

     protected void FormView1_PreRender(object sender, System.EventArgs e)
     {
         // if the mode is Edit or Insert
         if (this.FormView1.CurrentMode == FormViewMode.Edit || this.FormView1.CurrentMode == FormViewMode.Insert) {
             TextBox yyyTextBox = FormView1.FindControl("NameTextBox");
             yyyTextBox.Text = "02/10/2008";
         }
     }

      

    Regards, 

    Sunday, March 2, 2008 1:20 PM
  • User1618321365 posted

    Thanks for the suggestion --- here's the code I used. had to add the "as TextBox" to instantiate the yyyTextBox variable. Went through this in the debugger and the mode tests worked fine as expected and all events seemed to fire at the right time. However, alas, the yyyTextBox variable still returns a NULL. I've read lots of the forums relating to this and it seems the control ID is still ambiguous even though my event is triggering on the correct template ..... thanks for your assistance!!

                        ---Jim

    protected void FormView1_PreRender(object sender, System.EventArgs e)

    {

    // if the mode is Edit or Insert

    if (this.FormView1.CurrentMode == FormViewMode.Edit || this.FormView1.CurrentMode == FormViewMode.Insert)

    {

    TextBox yyyTextBox = FormView1.FindControl("NameTextBox") as TextBox; yyyTextBox.Text = "02/10/2008";

    }

    }

    Sunday, March 2, 2008 1:43 PM
  • User1564875471 posted

    can you post the ASPX code ? we need to see the FormView code !

    i tried the last code and its working .


     

     

    Sunday, March 2, 2008 2:22 PM
  • User1618321365 posted

    here's the aspx source

    <%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="PilotExpenseReports.aspx.cs" Inherits="_Default" Title="Untitled Page" %>

    <script runat="server">

     

    </script>

    <asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">

    </asp:Content>

    <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">

    <asp:GridView ID="GridView1" runat="server" AllowPaging="True"

    AllowSorting="True" DataSourceID="PilotExpenseReport1">

    <Columns>

    <asp:CommandField ShowSelectButton="True" />

    </Columns>

    </asp:GridView>

    <asp:SqlDataSource ID="PilotExpenseReport1" runat="server"

    ConnectionString="<%$ ConnectionStrings:ConnectionString %>"

    SelectCommand="SELECT PilotExpenseReport.DateTimeStamp, PilotExpenseReport.ExpenseStartDate, PilotExpenseReport.ExpenseEndDate, Pilots.UserName FROM PilotExpenseReport INNER JOIN Pilots ON PilotExpenseReport.PilotID = Pilots.PilotID WHERE (Pilots.UserName = @PilotUserName)">

    <SelectParameters>

    <asp:ProfileParameter Name="PilotUserName" PropertyName="UserName" />

    </SelectParameters>

    </asp:SqlDataSource>

    <asp:FormView ID="FormView1" runat="server" DataKeyNames="ExenseReportID"

    DataSourceID="ExpenseReport2" onprerender="FormView1_PreRender">

    <EditItemTemplate>

    ExenseReportID:

    <asp:Label ID="ExenseReportIDLabel1" runat="server"

    Text='<%# Eval("ExenseReportID") %>' />

    <br />

    PilotID:

    <asp:TextBox ID="PilotIDTextBox" runat="server" Text='<%# Bind("PilotID") %>' />

    <br />

    DateTimeStamp:

    <asp:TextBox ID="DateTimeStampTextBox" runat="server"

    Text='<%# Bind("DateTimeStamp") %>' />

    <br />

    OrderID_1:

    <asp:TextBox ID="OrderID_1TextBox" runat="server"

    Text='<%# Bind("OrderID_1") %>' />

    <br />

    PerCentOrderID_1:

    <asp:TextBox ID="PerCentOrderID_1TextBox" runat="server"

    Text='<%# Bind("PerCentOrderID_1") %>' />

    <br />

    OrderID_2:

    <asp:TextBox ID="OrderID_2TextBox" runat="server"

    Text='<%# Bind("OrderID_2") %>' />

    <br />

    PerCentOrderID_2:

    <asp:TextBox ID="PerCentOrderID_2TextBox" runat="server"

    Text='<%# Bind("PerCentOrderID_2") %>' />

    <br />

    OverheadExpense:

    <asp:CheckBox ID="OverheadExpenseCheckBox" runat="server"

    Checked='<%# Bind("OverheadExpense") %>' />

    <br />

    ExpenseStartDate:

    <asp:TextBox ID="ExpenseStartDateTextBox" runat="server"

    Text='<%# Bind("ExpenseStartDate") %>' />

    <br />

    ExpenseEndDate:

    <asp:TextBox ID="ExpenseEndDateTextBox" runat="server"

    Text='<%# Bind("ExpenseEndDate") %>' />

    <br />

    Hotel:

    <asp:TextBox ID="HotelTextBox" runat="server" Text='<%# Bind("Hotel") %>' />

    <br />

    Meals:

    <asp:TextBox ID="MealsTextBox" runat="server" Text='<%# Bind("Meals") %>' />

    <br />

    Transportation:

    <asp:TextBox ID="TransportationTextBox" runat="server"

    Text='<%# Bind("Transportation") %>' />

    <br />

    Misc:

    <asp:TextBox ID="MiscTextBox" runat="server" Text='<%# Bind("Misc") %>' />

    <br />

    <asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True"

    CommandName="Update" Text="Update" />

    &nbsp;<asp:LinkButton ID="UpdateCancelButton" runat="server"

    CausesValidation="False" CommandName="Cancel" Text="Cancel" />

    </EditItemTemplate>

    <InsertItemTemplate>

    PilotID:

    <asp:TextBox ID="PilotIDTextBox" runat="server" Text='<%# Bind("PilotID") %>' />

    <br />

    DateTimeStamp:

    <asp:TextBox ID="DateTimeStampTextBox" runat="server"

    Text='<%# Bind("DateTimeStamp") %>' />

    <br />

    OrderID_1:

    <asp:TextBox ID="OrderID_1TextBox" runat="server"

    Text='<%# Bind("OrderID_1") %>' />

    <br />

    PerCentOrderID_1:

    <asp:TextBox ID="PerCentOrderID_1TextBox" runat="server"

    Text='<%# Bind("PerCentOrderID_1") %>' />

    <br />

    OrderID_2:

    <asp:TextBox ID="OrderID_2TextBox" runat="server"

    Text='<%# Bind("OrderID_2") %>' />

    <br />

    PerCentOrderID_2:

    <asp:TextBox ID="PerCentOrderID_2TextBox" runat="server"

    Text='<%# Bind("PerCentOrderID_2") %>' />

    <br />

    OverheadExpense:

    <asp:CheckBox ID="OverheadExpenseCheckBox" runat="server"

    Checked='<%# Bind("OverheadExpense") %>' />

    <br />

    ExpenseStartDate:

    <asp:TextBox ID="ExpenseStartDateTextBox" runat="server"

    Text='<%# Bind("ExpenseStartDate") %>' />

    <br />

    ExpenseEndDate:

    <asp:TextBox ID="ExpenseEndDateTextBox" runat="server"

    Text='<%# Bind("ExpenseEndDate") %>' />

    <br />

    Hotel:

    <asp:TextBox ID="HotelTextBox" runat="server" Text='<%# Bind("Hotel") %>' />

    <br />

    Meals:

    <asp:TextBox ID="MealsTextBox" runat="server" Text='<%# Bind("Meals") %>' />

    <br />

    Transportation:

    <asp:TextBox ID="TransportationTextBox" runat="server"

    Text='<%# Bind("Transportation") %>' />

    <br />

    Misc:

    <asp:TextBox ID="MiscTextBox" runat="server" Text='<%# Bind("Misc") %>' />

    <br />

    <asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True"

    CommandName="Insert" Text="Insert" />

    &nbsp;<asp:LinkButton ID="InsertCancelButton" runat="server"

    CausesValidation="False" CommandName="Cancel" Text="Cancel" />

    </InsertItemTemplate>

    <ItemTemplate>

    ExenseReportID:

    <asp:Label ID="ExenseReportIDLabel" runat="server"

    Text='<%# Eval("ExenseReportID") %>' />

    <br />

    PilotID:

    <asp:Label ID="PilotIDLabel" runat="server" Text='<%# Bind("PilotID") %>' />

    <br />

    DateTimeStamp:

    <asp:Label ID="DateTimeStampLabel" runat="server"

    Text='<%# Bind("DateTimeStamp") %>' />

    <br />

    OrderID_1:

    <asp:Label ID="OrderID_1Label" runat="server" Text='<%# Bind("OrderID_1") %>' />

    <br />

    PerCentOrderID_1:

    <asp:Label ID="PerCentOrderID_1Label" runat="server"

    Text='<%# Bind("PerCentOrderID_1") %>' />

    <br />

    OrderID_2:

    <asp:Label ID="OrderID_2Label" runat="server" Text='<%# Bind("OrderID_2") %>' />

    <br />

    PerCentOrderID_2:

    <asp:Label ID="PerCentOrderID_2Label" runat="server"

    Text='<%# Bind("PerCentOrderID_2") %>' />

    <br />

    OverheadExpense:

    <asp:CheckBox ID="OverheadExpenseCheckBox" runat="server"

    Checked='<%# Bind("OverheadExpense") %>' Enabled="false" />

    <br />

    ExpenseStartDate:

    <asp:Label ID="ExpenseStartDateLabel" runat="server"

    Text='<%# Bind("ExpenseStartDate") %>' />

    <br />

    ExpenseEndDate:

    <asp:Label ID="ExpenseEndDateLabel" runat="server"

    Text='<%# Bind("ExpenseEndDate") %>' />

    <br />

    Hotel:

    <asp:Label ID="HotelLabel" runat="server" Text='<%# Bind("Hotel") %>' />

    <br />

    Meals:

    <asp:Label ID="MealsLabel" runat="server" Text='<%# Bind("Meals") %>' />

    <br />

    Transportation:

    <asp:Label ID="TransportationLabel" runat="server"

    Text='<%# Bind("Transportation") %>' />

    <br />

    Misc:

    <asp:Label ID="MiscLabel" runat="server" Text='<%# Bind("Misc") %>' />

    <br />

    <asp:LinkButton ID="EditButton" runat="server" CausesValidation="False"

    CommandName="Edit" Text="Edit" />

    &nbsp;<asp:LinkButton ID="DeleteButton" runat="server" CausesValidation="False"

    CommandName="Delete" Text="Delete" />

    &nbsp;<asp:LinkButton ID="NewButton" runat="server" CausesValidation="False"

    CommandName="New" Text="New" />

    </ItemTemplate>

    </asp:FormView>

    <asp:SqlDataSource ID="ExpenseReport2" runat="server"

    ConflictDetection="CompareAllValues"

    ConnectionString="<%$ ConnectionStrings:ConnectionString %>"

    DeleteCommand="DELETE FROM [PilotExpenseReport] WHERE [ExenseReportID] = @original_ExenseReportID AND [PilotID] = @original_PilotID AND [DateTimeStamp] = @original_DateTimeStamp AND [OrderID_1] = @original_OrderID_1 AND [PerCentOrderID_1] = @original_PerCentOrderID_1 AND [OrderID_2] = @original_OrderID_2 AND [PerCentOrderID_2] = @original_PerCentOrderID_2 AND [OverheadExpense] = @original_OverheadExpense AND [ExpenseStartDate] = @original_ExpenseStartDate AND [ExpenseEndDate] = @original_ExpenseEndDate AND [Hotel] = @original_Hotel AND [Meals] = @original_Meals AND [Transportation] = @original_Transportation AND [Misc] = @original_Misc"

    InsertCommand="INSERT INTO [PilotExpenseReport] ([PilotID], [DateTimeStamp], [OrderID_1], [PerCentOrderID_1], [OrderID_2], [PerCentOrderID_2], [OverheadExpense], [ExpenseStartDate], [ExpenseEndDate], [Hotel], [Meals], [Transportation], [Misc]) VALUES (@PilotID, @DateTimeStamp, @OrderID_1, @PerCentOrderID_1, @OrderID_2, @PerCentOrderID_2, @OverheadExpense, @ExpenseStartDate, @ExpenseEndDate, @Hotel, @Meals, @Transportation, @Misc)"

    OldValuesParameterFormatString="original_{0}"

    SelectCommand="SELECT * FROM [PilotExpenseReport]"

    UpdateCommand="UPDATE [PilotExpenseReport] SET [PilotID] = @PilotID, [DateTimeStamp] = @DateTimeStamp, [OrderID_1] = @OrderID_1, [PerCentOrderID_1] = @PerCentOrderID_1, [OrderID_2] = @OrderID_2, [PerCentOrderID_2] = @PerCentOrderID_2, [OverheadExpense] = @OverheadExpense, [ExpenseStartDate] = @ExpenseStartDate, [ExpenseEndDate] = @ExpenseEndDate, [Hotel] = @Hotel, [Meals] = @Meals, [Transportation] = @Transportation, [Misc] = @Misc WHERE [ExenseReportID] = @original_ExenseReportID AND [PilotID] = @original_PilotID AND [DateTimeStamp] = @original_DateTimeStamp AND [OrderID_1] = @original_OrderID_1 AND [PerCentOrderID_1] = @original_PerCentOrderID_1 AND [OrderID_2] = @original_OrderID_2 AND [PerCentOrderID_2] = @original_PerCentOrderID_2 AND [OverheadExpense] = @original_OverheadExpense AND [ExpenseStartDate] = @original_ExpenseStartDate AND [ExpenseEndDate] = @original_ExpenseEndDate AND [Hotel] = @original_Hotel AND [Meals] = @original_Meals AND [Transportation] = @original_Transportation AND [Misc] = @original_Misc">

    <DeleteParameters>

    <asp:Parameter Name="original_ExenseReportID" Type="Int32" />

    <asp:Parameter Name="original_PilotID" Type="Int32" />

    <asp:Parameter Name="original_DateTimeStamp" Type="DateTime" />

    <asp:Parameter Name="original_OrderID_1" Type="Int32" />

    <asp:Parameter Name="original_PerCentOrderID_1" Type="Double" />

    <asp:Parameter Name="original_OrderID_2" Type="Int32" />

    <asp:Parameter Name="original_PerCentOrderID_2" Type="Double" />

    <asp:Parameter Name="original_OverheadExpense" Type="Boolean" />

    <asp:Parameter Name="original_ExpenseStartDate" Type="DateTime" />

    <asp:Parameter Name="original_ExpenseEndDate" Type="DateTime" />

    <asp:Parameter Name="original_Hotel" Type="Decimal" />

    <asp:Parameter Name="original_Meals" Type="Decimal" />

    <asp:Parameter Name="original_Transportation" Type="Decimal" />

    <asp:Parameter Name="original_Misc" Type="Decimal" />

    </DeleteParameters>

    <UpdateParameters>

    <asp:Parameter Name="PilotID" Type="Int32" />

    <asp:Parameter Name="DateTimeStamp" Type="DateTime" />

    <asp:Parameter Name="OrderID_1" Type="Int32" />

    <asp:Parameter Name="PerCentOrderID_1" Type="Double" />

    <asp:Parameter Name="OrderID_2" Type="Int32" />

    <asp:Parameter Name="PerCentOrderID_2" Type="Double" />

    <asp:Parameter Name="OverheadExpense" Type="Boolean" />

    <asp:Parameter Name="ExpenseStartDate" Type="DateTime" />

    <asp:Parameter Name="ExpenseEndDate" Type="DateTime" />

    <asp:Parameter Name="Hotel" Type="Decimal" />

    <asp:Parameter Name="Meals" Type="Decimal" />

    <asp:Parameter Name="Transportation" Type="Decimal" />

    <asp:Parameter Name="Misc" Type="Decimal" />

    <asp:Parameter Name="original_ExenseReportID" Type="Int32" />

    <asp:Parameter Name="original_PilotID" Type="Int32" />

    <asp:Parameter Name="original_DateTimeStamp" Type="DateTime" />

    <asp:Parameter Name="original_OrderID_1" Type="Int32" />

    <asp:Parameter Name="original_PerCentOrderID_1" Type="Double" />

    <asp:Parameter Name="original_OrderID_2" Type="Int32" />

    <asp:Parameter Name="original_PerCentOrderID_2" Type="Double" />

    <asp:Parameter Name="original_OverheadExpense" Type="Boolean" />

    <asp:Parameter Name="original_ExpenseStartDate" Type="DateTime" />

    <asp:Parameter Name="original_ExpenseEndDate" Type="DateTime" />

    <asp:Parameter Name="original_Hotel" Type="Decimal" />

    <asp:Parameter Name="original_Meals" Type="Decimal" />

    <asp:Parameter Name="original_Transportation" Type="Decimal" />

    <asp:Parameter Name="original_Misc" Type="Decimal" />

    </UpdateParameters>

    <InsertParameters>

    <asp:Parameter Name="PilotID" Type="Int32" />

    <asp:Parameter Name="DateTimeStamp" Type="DateTime" />

    <asp:Parameter Name="OrderID_1" Type="Int32" />

    <asp:Parameter Name="PerCentOrderID_1" Type="Double" />

    <asp:Parameter Name="OrderID_2" Type="Int32" />

    <asp:Parameter Name="PerCentOrderID_2" Type="Double" />

    <asp:Parameter Name="OverheadExpense" Type="Boolean" />

    <asp:Parameter Name="ExpenseStartDate" Type="DateTime" />

    <asp:Parameter Name="ExpenseEndDate" Type="DateTime" />

    <asp:Parameter Name="Hotel" Type="Decimal" />

    <asp:Parameter Name="Meals" Type="Decimal" />

    <asp:Parameter Name="Transportation" Type="Decimal" />

    <asp:Parameter Name="Misc" Type="Decimal" />

    </InsertParameters>

    </asp:SqlDataSource>

    </asp:Content>

     

     

     

    Here's the .cs codebehind

     

    using System;

    using System.Collections;

    using System.Configuration;

    using System.Data;

    using System.Linq;

    using System.Web;

    using System.Web.Security;

    using System.Web.UI;

    using System.Web.UI.HtmlControls;

    using System.Web.UI.WebControls;

    using System.Web.UI.WebControls.WebParts;

    using System.Xml.Linq;

    public partial class _Default : System.Web.UI.Page

    {

    protected void Page_Load(object sender, EventArgs e)

    {

    }

    protected void FormView1_PreRender(object sender, EventArgs e)

    {

    if (this.FormView1.CurrentMode == FormViewMode.Edit || this.FormView1.CurrentMode == FormViewMode.Insert)

    {

    TextBox yyyTextBox = FormView1.FindControl("NameTextBox") as TextBox; yyyTextBox.Text = "02/10/2008";

    }

    }

    }

     Here's the web.config

    </configSections><appSettings/>

     

    <connectionStrings>

    <!--

    <add name="ConnectionString" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True"

    providerName="System.Data.SqlClient" />

    -->

    <clear/>

    <remove name="ConnectionString"/>

    <add name="ConnectionString" connectionString="Data Source=tcp:sql2k511.discountasp.net;Initial Catalog=SQL2005_441416_gvops;User ID=SQL2005_441416_gvops_user;Password=pi314159;"

    providerName="System.Data.SqlClient" />

     

    <
    remove name="LocalSqlServer" />

    <add name="LocalSqlServer" connectionString="Data Source=tcp:sql2k511.discountasp.net;Integrated Security=false;Initial Catalog=SQL2005_441416_gvops;User ID=SQL2005_441416_gvops_user;Password=pi314159"

    providerName="System.Data.SqlClient" />

    </connectionStrings>

    <system.web><!--

    Set compilation debug="true" to insert debugging

    symbols into the compiled page. Because this

    affects performance, set this value to true only

    during development.

    Visual Basic options:

    Set strict="true" to disallow all data type conversions

    where data loss can occur.

    Set explicit="true" to force declaration of all variables.

    -->

     

    <
    profile enabled="true">

    <properties>

    <add name="UserName" type="string"/>

    </properties>

    </profile>

    <authorization>

    <allow users="administrator" />

    <deny roles="guest" />

    <deny users="?" />

    </authorization>

    <roleManager enabled="true" />

    <compilation debug="true" strict="false" explicit="true">

    <assemblies>

    <add assembly="System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>

    <add assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>

    <add assembly="System.Data.DataSetExtensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>

    <add assembly="System.Xml.Linq, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>

    </assemblies>

    </compilation>

    <pages>

    <namespaces>

    <clear/>

    <add namespace="System"/>

    <add namespace="System.Collections"/>

    <add namespace="System.Collections.Generic"/>

    <add namespace="System.Collections.Specialized"/>

    <add namespace="System.Configuration"/>

    <add namespace="System.Text"/>

    <add namespace="System.Text.RegularExpressions"/>

    <add namespace="System.Linq"/>

    <add namespace="System.Xml.Linq"/>

    <add namespace="System.Web"/>

    <add namespace="System.Web.Caching"/>

    <add namespace="System.Web.SessionState"/>

    <add namespace="System.Web.Security"/>

    <add namespace="System.Web.Profile"/>

    <add namespace="System.Web.UI"/>

    <add namespace="System.Web.UI.WebControls"/>

    <add namespace="System.Web.UI.WebControls.WebParts"/>

    <add namespace="System.Web.UI.HtmlControls"/>

    </namespaces>

    <controls>

    <add tagPrefix="asp" namespace="System.Web.UI" assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>

    <add tagPrefix="asp" namespace="System.Web.UI.WebControls" assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>

    </controls>

    </pages>

    <!--

    The <authentication> section enables configuration

    of the security authentication mode used by

    ASP.NET to identify an incoming user.

    -->

    <authentication mode="Forms" />

    <!--

    The <customErrors> section enables configuration

    of what to do if/when an unhandled error occurs

    during the execution of a request. Specifically,

    it enables developers to configure html error pages

    to be displayed in place of a error stack trace.

    <customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">

    <error statusCode="403" redirect="NoAccess.htm" />

    <error statusCode="404" redirect="FileNotFound.htm" />

    </customErrors>

    -->

    <httpHandlers>

    <remove verb="*" path="*.asmx"/>

    <add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>

    <add verb="*" path="*_AppService.axd" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>

    <add verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" validate="false"/>

    </httpHandlers>

    <httpModules>

    <add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>

    </httpModules>

    </system.web>

    <system.codedom>

    <compilers>

    <compiler language="c#;cs;csharp" extension=".cs" warningLevel="4" type="Microsoft.CSharp.CSharpCodeProvider, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">

    <providerOption name="CompilerVersion" value="v3.5"/>

    <providerOption name="WarnAsError" value="false"/>

    </compiler>

    <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" warningLevel="4" type="Microsoft.VisualBasic.VBCodeProvider, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">

    <providerOption name="CompilerVersion" value="v3.5"/>

    <providerOption name="OptionInfer" value="true"/>

    <providerOption name="WarnAsError" value="false"/>

    </compiler>

    </compilers>

    </system.codedom>

    <!--

    The system.webServer section is required for running ASP.NET AJAX under Internet

    Information Services 7.0. It is not necessary for previous version of IIS.

    -->

    <system.webServer>

    <validation validateIntegratedModeConfiguration="false"/>

    <modules>

    <remove name="ScriptModule"/>

    <add name="ScriptModule" preCondition="managedHandler" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>

    </modules>

    <handlers>

    <remove name="WebServiceHandlerFactory-Integrated"/>

    <remove name="ScriptHandlerFactory"/>

    <remove name="ScriptHandlerFactoryAppServices"/>

    <remove name="ScriptResource"/>

    <add name="ScriptHandlerFactory" verb="*" path="*.asmx" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>

    <add name="ScriptHandlerFactoryAppServices" verb="*" path="*_AppService.axd" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>

    <add name="ScriptResource" preCondition="integratedMode" verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>

    </handlers>

    </system.webServer>

    <runtime>

    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">

    <dependentAssembly>

    <assemblyIdentity name="System.Web.Extensions" publicKeyToken="31bf3856ad364e35"/>

    <bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="3.5.0.0"/>

    </dependentAssembly>

    <dependentAssembly>

    <assemblyIdentity name="System.Web.Extensions.Design" publicKeyToken="31bf3856ad364e35"/>

    <bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="3.5.0.0"/>

    </dependentAssembly>

    </assemblyBinding>

    </runtime>

    </configuration>

     

     

    Sunday, March 2, 2008 2:55 PM
  • User1564875471 posted

    I forget to change the TextBox Id  to "DateTimeStampTextBox"

     

    try this code :

     

     protected void FormView1_PreRender(object sender, System.EventArgs e)
     {
         // if the mode is Edit or Insert
         if (this.FormView1.CurrentMode == FormViewMode.Edit || this.FormView1.CurrentMode == FormViewMode.Insert) {
             TextBox yyyTextBox = FormView1.FindControl("DateTimeStampTextBox") as TextBox;
             yyyTextBox.Text = "02/10/2008";
         }
     }
      
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, March 2, 2008 3:11 PM
  • User1618321365 posted

    It worked!!! but let me do a few more tests ...

          -----Jim

    Sunday, March 2, 2008 3:30 PM
  • User1618321365 posted

    all works well and seems to provide a ton of functionality with minimal code of any kind -- just the few lines of C#. I was trivially able to insert a calender on the page and insert DateTime.Now.ToString() into the DateTimeStamp record ....

        Thanks again for the persistance.

        ---Jim 

    Sunday, March 2, 2008 3:56 PM