Accessing FormView elements in code (VB)

Discussion in 'ASP.NET 2.0' started by Blenster, Sep 5, 2007.

  1. I am building a form that inserts data into the database from values entered into a FromView hooked to a dataset object and wish to automatically upload a file at the same time that I save the filename to the database. The problem is that in this sub ArticleFileTextBox, a valid object within the FormView, is not recognized as a defined element and if I move the file-upload control within the formview it also disappears and .NET doesn't know how to reference it. If someone could show me the tiny stupid thing I'm doing wrong so I can get this to work I'd sure appreciate it!!!


    My code:

    Protected Sub InsertButton_Click(ByVal sender As Object, ByVal e As System.EventArgs)

    If FileUpload1.HasFile Then
    Try

    ArticleFileTextBox.text = FileUpload1.PostedFile.FileName

    'How do I address this textbox (above)? I need to change it's value to the
    'name of the file and save that in the database
    'there's a FileUpload2 in the form but I can't access it, either
    'I figure the answer is the same for both, some notation I don't know

    FileUpload1.SaveAs('e:\web\elecom00001\htdocs\data\docs\' & _
    FileUpload1.FileName)
    Label1.Text = 'File name: ' & _
    FileUpload1.PostedFile.FileName & '' & _
    'File Size: ' & _
    FileUpload1.PostedFile.ContentLength & ' kb' & _
    'Content type: ' & _
    FileUpload1.PostedFile.ContentType
    Catch ex As Exception
    Label1.Text = 'ERROR: ' & ex.Message.ToString()
    End Try
    Else
    Label1.Text = 'You have not specified a file.'
    End If
    End Sub
    End Class
     
  2. I've asked a few experts and no one knows, apparently you cannot address an object within an object in the .NET component structure. Feel free to show me how if it's possible.
     
  3. Can you also post the ASPX codeso we can see how the controls are nested.




    Aristotle

    DiscountASP.NET
    www.DiscountASP.NET
     
  4. <%@ Page Language='VB' MasterPageFile='~/Site.master' Title='NRRA' %>
    <asp:Content ID='Content1' ContentPlaceHolderID='ContentPlaceHolder1' Runat='Server'>
    <asp:FormView ID='Footer' runat='server' DataKeyNames='ID' DataSourceID='pageData'
    HorizontalAlign='Left'>
    <ItemTemplate>
    <asp:Label ID='SiteContentLabel' runat='server' Text='<%# Bind('SiteContent') %>'></asp:Label>
    </ItemTemplate>
    </asp:FormView>
    <asp:ObjectDataSource ID='pageData' runat='server' DeleteMethod='Delete' InsertMethod='Insert'
    OldValuesParameterFormatString='original_{0}' SelectMethod='GetDataByContentName'
    TypeName='AdminUsersTableAdapters.SiteContentTableAdapter' UpdateMethod='Update'>
    <SelectParameters>
    <asp:parameter DefaultValue='PostArticle' Name='ContentName' Type='String' />
    </SelectParameters>
    </asp:ObjectDataSource>

    <asp:DetailsView ID='DetailsView1' runat='server' AutoGenerateRows='False' CellPadding='4'
    DataKeyNames='ID' DataSourceID='ArticleDataSource1' ForeColor='#333333' GridLines='None'
    Height='50px' Width='125px'>
    <FooterStyle BackColor='#5D7B9D' Font-Bold='True' ForeColor='White' />
    <CommandRowStyle BackColor='#E2DED6' Font-Bold='True' />
    <EditRowStyle BackColor='#999999' />
    <RowStyle BackColor='#F7F6F3' ForeColor='#333333' />
    <PagerStyle BackColor='#284775' ForeColor='White' HorizontalAlign='Center' />
    <Fields>
    <asp:BoundField DataField='ID' HeaderText='ID' InsertVisible='False' ReadOnly='True'
    SortExpression='ID' />
    <asp:BoundField DataField='Titile' HeaderText='Titile' SortExpression='Titile' />
    <asp:BoundField DataField='Keywords' HeaderText='Keywords' SortExpression='Keywords' />
    <asp:TemplateField HeaderText='File Name' SortExpression='ArticleFile'>
    <EditItemTemplate>
    <asp:TextBox ID='TextBox1' runat='server' Text='<%# Bind('ArticleFile') %>'></asp:TextBox>
    </EditItemTemplate>
    <InsertItemTemplate>
    <asp:TextBox ID='TextBox1' runat='server' Text='<%# Bind('ArticleFile') %>'></asp:TextBox>
    <asp:FileUpload ID='FileUpload1' runat='server' />
    </InsertItemTemplate>
    <ItemTemplate>
    <asp:Label ID='Label1' runat='server' Text='<%# Bind('ArticleFile') %>'></asp:Label>
    </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText='Date' SortExpression='TimeDate'>
    <EditItemTemplate>
    <asp:TextBox ID='TextBox2' runat='server' Text='<%# Bind('TimeDate') %>'></asp:TextBox>
    </EditItemTemplate>
    <InsertItemTemplate>
    <asp:Calendar ID='Calendar1' runat='server' BackColor='White' BorderColor='White'
    BorderWidth='1px' Font-Names='Verdana' Font-Size='9pt' ForeColor='Black' Height='190px'
    NextPrevFormat='FullMonth' SelectedDate='<%# Bind('TimeDate') %>' Width='350px'>
    <SelectedDayStyle BackColor='#333399' ForeColor='White' />
    <TodayDayStyle BackColor='#CCCCCC' />
    <OtherMonthDayStyle ForeColor='#999999' />
    <NextPrevStyle Font-Bold='True' Font-Size='8pt' ForeColor='#333333' VerticalAlign='Bottom' />
    <DayHeaderStyle Font-Bold='True' Font-Size='8pt' />
    <TitleStyle BackColor='White' BorderColor='Black' BorderWidth='4px' Font-Bold='True'
    Font-Size='12pt' ForeColor='#333399' />
    </asp:Calendar>
    </InsertItemTemplate>
    <ItemTemplate>
    <asp:Calendar ID='Calendar1' runat='server' BackColor='White' BorderColor='White'
    BorderWidth='1px' Enabled='False' Font-Names='Verdana' Font-Size='9pt' ForeColor='Black'
    Height='190px' NextPrevFormat='FullMonth' SelectedDate='<%# Bind('TimeDate') %>'
    Width='350px'>
    <SelectedDayStyle BackColor='#333399' ForeColor='White' />
    <TodayDayStyle BackColor='#CCCCCC' />
    <OtherMonthDayStyle ForeColor='#999999' />
    <NextPrevStyle Font-Bold='True' Font-Size='8pt' ForeColor='#333333' VerticalAlign='Bottom' />
    <DayHeaderStyle Font-Bold='True' Font-Size='8pt' />
    <TitleStyle BackColor='White' BorderColor='Black' BorderWidth='4px' Font-Bold='True'
    Font-Size='12pt' ForeColor='#333399' />
    </asp:Calendar>
    </ItemTemplate>
    </asp:TemplateField>
    <asp:CommandField ShowInsertButton='True' />
    </Fields>
    <FieldHeaderStyle BackColor='#E9ECF1' Font-Bold='True' />
    <HeaderStyle BackColor='#5D7B9D' Font-Bold='True' ForeColor='White' />
    <AlternatingRowStyle BackColor='White' ForeColor='#284775' />
    </asp:DetailsView>
    <asp:ObjectDataSource ID='ArticleDataSource1' runat='server' DeleteMethod='Delete'
    InsertMethod='Insert' OldValuesParameterFormatString='original_{0}' SelectMethod='GetDataByArticleID'
    TypeName='AdminUsersTableAdapters.ArticlesTableAdapter' UpdateMethod='Update'>
    <DeleteParameters>
    <asp:parameter Name='Original_ID' Type='Int32' />
    </DeleteParameters>
    <UpdateParameters>
    <asp:parameter Name='Titile' Type='String' />
    <asp:parameter Name='Keywords' Type='String' />
    <asp:parameter Name='ArticleFile' Type='String' />
    <asp:parameter Name='TimeDate' Type='DateTime' />
    <asp:parameter Name='Original_ID' Type='Int32' />
    <asp:parameter Name='ID' Type='Int32' />
    </UpdateParameters>
    <SelectParameters>
    <asp:parameter DefaultValue='1' Name='ArticleID' Type='Int32' />
    </SelectParameters>
    <InsertParameters>
    <asp:parameter Name='Titile' Type='String' />
    <asp:parameter Name='Keywords' Type='String' />
    <asp:parameter Name='ArticleFile' Type='String' />
    <asp:parameter Name='TimeDate' Type='DateTime' />
    </InsertParameters>
    </asp:ObjectDataSource>
    </asp:Content>
     
  5. I don't see a control named ArticleFileTextBox on the page.

    Aristotle

    DiscountASP.NET
    www.DiscountASP.NET
     

Share This Page