FormView: InsertMode: Inserting Text from another FormView Textbox to a databound TextBox

Last post 11-04-2007 9:07 PM by Techie Zhang - MSFT. 4 replies.

Sort Posts:

  • FormView: InsertMode: Inserting Text from another FormView Textbox to a databound TextBox

    10-31-2007, 6:55 AM
    • Member
      point Member
    • philburton
    • Member since 10-31-2007, 10:42 AM
    • Posts 2

    Hello. I have a page with one formview [formview1] displaying information from a database such as stock number, make, model etc.

     I then have another formview [formview2] on the same page that I use to insert in to a DB.

     On formview2, I want to pre-populate 3 of the fields with information displayed in formview1. What ive tried to do is write c# code to get the text from formview in the 'OnDataBound' event of Formview2. It doesn't work. I have to admit I'm rubbish at c#, so I need some help.

     This is what I've got so far:

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

    {

     

    protected void Page_Load(object sender, EventArgs e)

    {

    }

    protected void FormView2_OnLoad(object sender, EventArgs e)

    {

    if (FormView2.CurrentMode == FormViewMode.Insert)

    {

    ((
    TextBox)FormView1.FindControl("StockNoInterestedTextBox")).Text = ((TextBox)FormView1.FindControl("Stock")).Text;

    }

    }

    }

     ===============

    AND this in my aspx page:

    FormView1:

    <asp:TextBox ID="stock" runat="server" Text='<% Eval("StockNumber") %>'></asp:TextBox> 

    FormView2: 

    <asp:TextBox ID="StockNoInterestedTextBox" runat="server" Text='<%# Bind("StockNoInterested") %>'>

    </asp:TextBox>

    <asp:AccessDataSource OnLoad="FormView2_OnLoad" ... >

    ==============

    When I run that, it says that "The name 'FormView2' does not exist in the current context", but I can't see what Im doing wrong.

     Can someone post me some coding that allows me to take text from FormView1 TextBox Stock and automatically put it in FormView2 TextBox StockNoInterestedTextBox

     

    Thanks In Advance, Phil

  • Re: FormView: InsertMode: Inserting Text from another FormView Textbox to a databound TextBox

    10-31-2007, 9:46 AM
    • Star
      8,182 point Star
    • Careed
    • Member since 06-24-2002, 3:37 AM
    • Lubbock, TX
    • Posts 1,597

    Where is the AccessDataSource?  Inside FormView2?  What is FormView2's DataSourceID?

     Also, you have FormView1.FindControl("StockNoInterestedTextBox").  Should this be from FormView2?
     

    Christopher Reed
    "The oxen are slow, but the earth is patient."
  • Re: FormView: InsertMode: Inserting Text from another FormView Textbox to a databound TextBox

    10-31-2007, 9:55 AM
    • Member
      point Member
    • philburton
    • Member since 10-31-2007, 10:42 AM
    • Posts 2

    Apologies, I made a couple of errors in the post above, but since posting it ive come closer to fixing it. So, this is what I have now:

    protected void FormView2_OnLoad(object sender, EventArgs e)

    {

    TextBox StockNoInterestedTextBox = (TextBox)FormView2.Row.FindControl("StockNoInterestedTextBox");
    StockNoInterestedTextBox.Text = "hello";

    }

     That writes hello in the text box I want to write in to, but when I do this:

    {

    TextBox StockNoInterestedTextBox = (TextBox)FormView2.Row.FindControl("StockNoInterestedTextBox");
    TextBox StockNo = (TextBox)FormView1.Row.FindControl("stock");
    StockNoInterestedTextBox.Text = StockNo.Text;

     

    }

    It doesn't like it, saying Object null reference or something... How do I declare a textbox like in the line "TextBox StockNoInterestedTextBox =..." but from FormView1 ?

  • Re: FormView: InsertMode: Inserting Text from another FormView Textbox to a databound TextBox

    11-01-2007, 1:42 AM
    • Star
      8,182 point Star
    • Careed
    • Member since 06-24-2002, 3:37 AM
    • Lubbock, TX
    • Posts 1,597

    The problem is that you do not have access to FormView1.Row from a FormView2 event.

    Christopher Reed
    "The oxen are slow, but the earth is patient."
  • Re: FormView: InsertMode: Inserting Text from another FormView Textbox to a databound TextBox

    11-04-2007, 9:07 PM
    Answer

    philburton:

    <asp:AccessDataSource OnLoad="FormView2_OnLoad" ... >

    I think the problem is the FormView1 isn't loaded when you access its controls. So you get null reference. 

    So make sure the FormView1 is initiated before FormView2.

    Please try access the textbox in FormView2 in Load or PreRender event hander like:

    protected void FormView2_PreRender(object sender, EventArgs e)
        {
            if (FormView2.CurrentMode == FormViewMode.Insert)
            {
                TextBox textBox = (TextBox)FormView1.FindControl("StockNoInterestedTextBox");
                TextBox textBox1 = (TextBox)FormView1.FindControl("stock");
                if (textBox != null && textBox1!=null)
                {
                    textBox.Text = textBox1.Text;
                }
            }
        
        }

     ASPX code:

     <asp:FormView ID="FormView2" runat="server"  OnPreRender="FormView2_PreRender" OnLoad="FormView2_Load">

    Please have a try.

    Sincerely,
    Techie Zhang
    Microsoft Online Community Support
Page 1 of 1 (5 items)