Asp.net web controls problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • JimWu
    New Member
    • Aug 2007
    • 14

    Asp.net web controls problem

    I'd like to add designed by myself web control "TextBox" on my page.

    But something wrong to appear message " Control 'ctl13' of type 'TextBox must be placed inside a form tag with runat=server. " while performing SaveImage method.

    Why the TextBox control can not add to my page, but Label control do.

    My code is listed as follows.

    using System;
    using System.Data;
    using System.Configur ation;
    using System.Collecti ons;
    using System.Web;
    using System.Web.Secu rity;
    using System.Web.UI;
    using System.Web.UI.W ebControls;
    using System.Web.UI.W ebControls.WebP arts;
    using System.Web.UI.H tmlControls;


    public partial class AddMetaData : System.Web.UI.P age
    {
    System.Web.UI.W ebControls.Imag e[] images = new System.Web.UI.W ebControls.Imag e[5];
    System.Web.UI.W ebControls.Labe l[,] labels = new Label[5, 5];
    System.Web.UI.W ebControls.Text Box[,] textboxs = new System.Web.UI.W ebControls.Text Box[5, 5];


    protected void Page_Load(objec t sender, EventArgs e)
    {

    if (PreviousPage != null)
    {
    if (PreviousPage.I sCrossPagePostB ack == true)
    this.SaveImages ();
    }
    }


    private System.Boolean SaveImages()
    {

    //loop through the files uploaded
    System.Web.Http FileCollection _files = System.Web.Http Context.Current .Request.Files;

    //Message to the user
    System.Text.Str ingBuilder _message = new System.Text.Str ingBuilder("Fil es Uploaded:<br>") ;
    try
    {
    for (System.Int32 _iFile = 0; _iFile < _files.Count; _iFile++)
    {
    // Check to make sure the uploaded file is a jpg or gif
    System.Web.Http PostedFile _postedFile = _files[_iFile];
    System.String _fileName, _fileExtension;
    _fileName = System.IO.Path. GetFileName(_po stedFile.FileNa me);
    _fileExtension = System.IO.Path. GetExtension(_f ileName);

    //現在只先做jpeg
    if ((_fileExtensio n == ".jpg") || (_fileExtension == ".JPG") || (_fileExtension == ".jpeg") || (_fileExtension == ".JPEG"))
    {


    //Save File to the proper directory
    _postedFile.Sav eAs(System.Web. HttpContext.Cur rent.Request.Ma pPath("jpgs/") + _fileName);
    //textboxs[_iFile, 0].Text = _fileName;

    txtTitle.Text = _fileName;
    labels[_iFile,0].Text = "Album";
    labels[_iFile, 1].Text = "Name";
    labels[_iFile, 2].Text = "Tag";
    labels[_iFile, 3].Text = "Descriptio n";
    textboxs[_iFile, 0].Text ="";
    textboxs[_iFile, 1].Text = _fileName;
    textboxs[_iFile, 2].Text ="";
    textboxs[_iFile, 3].Text ="";

    images[_iFile].ImageUrl = "./jpgs/" + _fileName;

    this.Controls.A dd(images[_iFile]);

    this.Controls.A dd(labels[_iFile, 0] );
    this.Controls.A dd(labels[_iFile, 1] );
    this.Controls.A dd(labels[_iFile, 2] );
    this.Controls.A dd(labels[_iFile, 3] );

    this.Controls.A dd(textboxs[_iFile, 0]);
    this.Controls.A dd(textboxs[_iFile, 1]);
    this.Controls.A dd(textboxs[_iFile, 2]);
    this.Controls.A dd(textboxs[_iFile, 3]);


    _message.Append (_fileName + "<BR>");
    }
    else
    {
    if (_fileExtension != "")
    {
    _message.Append (_fileName + " <font color=\"red\">f ailed!! Only .gif and .jpg images allowed!</font> <BR>");
    }
    }

    }
    Label5.Text = _message.ToStri ng();
    return true;
    }
    catch (System.Excepti on Ex)
    {
    Label1.Text = Ex.Message;
    return false;
    }
    }

    #region Web Form Designer generated code

    override protected void OnInit(System.E ventArgs e)
    {
    //
    // CODEGEN: This call is required by the ASP.NET Web Form Designer.
    //

    for (int i = 0; i < 5; i++) {

    images[i] = new Image();

    for (int j = 0; j < 5; j++)
    {
    labels[i,j] = new Label();
    textboxs[i,j] = new TextBox();

    }
    }


    InitializeCompo nent();
    base.OnInit(e);

    }
    Have any know how to solve this problem or something else to replace controls of TextBox of use.

    Thank you.

    Jim
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Greetings, you posted your question in the Articles section; it belongs in the
    corresponding Forum section. I'll move it over for you.

    kind regards,

    Jos

    Comment

    • JimWu
      New Member
      • Aug 2007
      • 14

      #3
      Thank you very much.

      I have already find out the answer during the few past days.

      TextBox control can not be added in Page.

      Since TextBox control does not belong to Page collections, but Form collections.

      Thank you for your kindly assistence.

      Jim.


      Originally posted by JosAH
      Greetings, you posted your question in the Articles section; it belongs in the
      corresponding Forum section. I'll move it over for you.

      kind regards,

      Jos

      Comment

      Working...