0

在我的网站上,我通过 asp:FileUpload [多个文件 - 图像] 上传照片。上传后,我将它们与文本框一起显示在面板中,以便为上传的图像编写描述。然后将它们保存在数据库中。但我无法找到文本框的值或在保存事件中找到我添加到面板的控件。但是图像和文本框将显示在面板中。

我的前端代码是这样的:

  <form id="form1" runat="server">
    <div class="transbox" id="mainbk" runat="server" style="position:absolute; top:0px; left:0px; width: 100%; height: 100%;" >
      <asp:FileUpload runat="server" ID="UploadImages" style="background-color:white; position:absolute; font-family:'Palatino Linotype'; font-size:medium; top: 4px; left: 350px; right: 251px;" Width="500px" AllowMultiple="true"/>
        <asp:Button runat="server" ID="uploadedFile" style="position:absolute;  font-family:'Palatino Linotype'; font-size:medium; top: 4px; left: 870px; width: 112px; height: 29px;" Text="Upload" OnClick="uploadFile_Click" />
        <asp:Panel ID="updtpanel" runat="server" CssClass="transbox" style="width:100%;height:100%;left:0px;top:0px;position:absolute" Visible="false">
             <asp:Button ID="btnsave" runat="server" Text="Save" OnClick="btnsave_Click" Font-Bold="true" BackColor="Yellow" />
        </asp:Panel>
     </div>
  </form>

我的后端代码是这样的:

protected void uploadFile_Click(object sender, EventArgs e)
    {
        if (UploadImages.HasFiles)
        {
           int tid = 0;

           string fileExt = Path.GetExtension(UploadImages.FileName).ToLower();
           if (fileExt == ".jpeg" || fileExt == ".png" || fileExt == ".jpg" || fileExt == ".bmp")
           {
               HtmlGenericControl dh = new HtmlGenericControl("div");
               dh.Attributes.Add("class", "head");
               dh.InnerText = "Write Description";
               updtpanel.Controls.Add(dh);

               HtmlGenericControl dload;
               foreach (HttpPostedFile uploadedFile in UploadImages.PostedFiles)
               {
                   tid = tid + 1;
                   textid = "txt" + tid;
                   Image img = new Image();
                   TextBox ta = new TextBox();
                   ta.TextMode = TextBoxMode.MultiLine;
                   filepath = Server.MapPath("~/Images/Gallery/" + uploadedFile.FileName);
                   uploadedFile.SaveAs(filepath);
                   newpath = "../Images/Gallery/" + uploadedFile.FileName;
                   try
                   {
                       dload = new HtmlGenericControl("div");
                       updtpanel.Visible = true;
                       dload.Attributes.Add("class", "dataload");
                       dload.Attributes.Add("runat", "server");
                       dload.ID = "ind" + tid;
                       img.CssClass = "loadimg";
                       img.ImageUrl = newpath.ToString();
                       img.ID = "img"+tid;
                       img.Attributes.Add("runat", "server");
                       ta.Attributes.Add("class", "txtdes");
                       ta.ID = textid;
                       ta.Attributes.Add("runat", "server");
                       dload.Controls.Add(img);
                       dload.Controls.Add(ta);
                       updtpanel.Controls.Add(dload);
                   }

                   catch (Exception ex)
                   {
                       Response.Write(ex.Message);
                   }

               }
           }
           else
           {
               Page.ClientScript.RegisterStartupScript(GetType(), "msgbox", "alert('Please Select only Image Files!!');", true);
           }

        }
        else
        {
            Page.ClientScript.RegisterStartupScript(GetType(), "msgbox", "alert('Please Select a File First!!');", true);        
        }
    }


    protected void btnsave_Click(object sender, EventArgs e)
    {
        foreach (Control c in updtpanel.Controls) 
        {
            cnt1 += 1;
            HtmlGenericControl div = ((HtmlGenericControl)updtpanel.FindControl("ind"+cnt1.ToString()));
            foreach (Control nc in div.Controls)
            {
                string str = "";
                string iurl = "";
                TextBox txt = (TextBox)div.FindControl("txt" + cnt1.ToString());
                Image img = (Image)div.FindControl("img" + cnt1.ToString());
                str = txt.Text;
                iurl = img.ImageUrl;
                id += 1;

                string Insert = "Insert into slider (slid,slurl,slalt) values (@id,@IMAGE_PATH,@alter)";
                SqlCommand cmd = new SqlCommand(Insert, con);
                cmd.Parameters.AddWithValue("@IMAGE_PATH", iurl);
                cmd.Parameters.AddWithValue("@id", id);
                cmd.Parameters.AddWithValue("@alter", str);
                try
                {
                    con.Open();
                    cmd.ExecuteNonQuery();
                    con.Close();

                }
                catch (Exception e1)
                {
                    Response.Write(e1.Message);
                }
            }
        }
        updtpanel.Visible = false;
    }

但是它在保存时给出了一个错误,它表示在保存单击事件中 Findcontrol 之后对象引用未设置为对象的实例。我要去哪里错了。从过去的 3 天开始,我真的在工作,但仍然无法得到它。请告诉我一个解决办法。

我还有什么需要补充的吗?我遇到了一些网站,其中一些人说回发后需要在 Page_Load 中重新创建控件。但是,当我在事件中创建控件时,如何在 Page_Load 中重新创建它们。

4

1 回答 1

1

试试这个。为我工作尝试用表单 ID 替换 contentplaceholder 可能会起作用。

  String Value=   (this.Form.FindControl("ContentPlaceHolder1").FindControl("panel").FindControl("txtbx" ) as TextBox).Text;
于 2013-07-10T05:41:50.117 回答