I have a user control that upload image and show it to users. After upload image in parent page I want to save the image URL to database but I can't. This is my code that I used but the image is null.
Image img = new Image();
img = this.Uploade1.FindControl("Image1") as Image;
My code: My User Control:
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<contenttemplate>
<table class="auto-style1">
<tr>
<td class="auto-style2">
</td>
<td rowspan="3">
<asp:Image ID="Image1" runat="server" Height="100" ImageAlign="Middle" Width="100" />
<br />
<asp:Label ID="StatusLabel" runat="server" Font-Names="Tahoma" Font-Size="8pt" Visible="False"></asp:Label>
</td>
</tr>
<tr>
<td class="auto-style2">
<asp:FileUpload ID="FileUploadControl" runat="server" />
</td>
</tr>
<tr>
<td class="auto-style2">
<asp:Button ID="btnUpload" runat="server" OnClick="ButtonUpload_Click" Text="بارگذاری" />
</td>
</tr>
</table>
<br />
</contenttempl
protected void ButtonUpload_Click(object sender, EventArgs e) { //Path to store uploaded files on server - make sure your paths are unique string Id = Request.QueryString["id"]; //string thumbPath = "../upFiles/_thumb_" + Guid.NewGuid().ToString() + ".jpg"; //string filePath = thumbPath.Replace("_thumb_", ""); string filePath = "../upFiles/" + Guid.NewGuid().ToString() + ".jpg"; try { // Check that there is a file if (FileUploadControl.HasFile) { if (FileUploadControl.PostedFile.ContentType == "image/jpeg") { if (FileUploadControl.PostedFile.ContentLength 0) && (System.IO.Path.GetExtension(myFile.FileName).ToLower() == ".jpg")) { // Read file into a data stream byte[] myData = new Byte[nFileLen]; myFile.InputStream.Read(myData, 0, nFileLen); myFile.InputStream.Dispose(); // Save the stream to disk as temporary file. // make sure the path is unique! System.IO.FileStream newFile = new System.IO.FileStream(Server.MapPath(filePath.Remove(0,3)), System.IO.FileMode.Create); newFile.Write(myData, 0, myData.Length); newFile.Close(); System.IO.File.Delete(Server.MapPath(filePath.Remove(0, 3) +"_temp.jpg")); //. new cpode Image1.ImageUrl = filePath.Remove(0, 3); } } } catch (Exception ex) { StatusLabel.ForeColor = System.Drawing.Color.Red; StatusLabel.Text = ex.Message; } }