I have Tab Container, and i have a GridView inside that. I need to upload image while i am clicking on the particular row. I am using update Panel too, outside of the grid view, i was able to upload image using PostBack Trigger to “preview Button”, but inside gridview i don know how to access it. I just created one button called btngrvPreview outside of the gridview and called its click event to the button btnPreview1 which is placed inside gridView. I think this approach is correct only.. But i don know why this Trigger to btnPreview1 is not working.
Its showing if(vFile.HasFile ) condition is always getting false..
Help me to solve this problem.
GridView
<asp:GridView ID="grvItem" runat="server" AutoGenerateColumns="False" OnRowDataBound="grvItem_RowDataBound" ShowFooter="True" SkinID="grid" Width="100%">
<Columns>
<asp:BoundField HeaderText="Reference ID" />
<asp:TemplateField HeaderText="Design">
<ItemTemplate>
<asp:Label ID="grvlbldesign" runat="server" CssClass="lbl">
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Upload Image">
<ItemTemplate>
<asp:HiddenField ID="hifgrvImage" runat="server" />
<a ID="grvuploadimgPopup" runat="server">
<asp:Image ID="grvUploadImage" runat="server" Width="90px" />
</a> </ItemTemplate>
<EditItemTemplate>
<asp:FileUpload ID="fupImage1" runat="server" CssClass="fileuploadbtn" TabIndex="5" ToolTip="Browse For Image" Width="152px"/>
<asp:ImageButton ID="btnPreview1" runat="server" ImageUrl="~/images/view.gif" TabIndex="5" ToolTip="Preview" onclick="btngrvPreview_Click"/>
</EditItemTemplate>
</asp:TemplateField>
<Triggers>
<asp:PostBackTrigger ControlID="btngrvPreview" />
</Triggers>
Button Click EVent
protected void btnPreview1_Click(object sender, ImageClickEventArgs e)
{
try
{
int rowIndex = Convert.ToInt32(hifRecordID.Value);
Image vimgView = (Image)grvItem.Rows[rowIndex].FindControl("grvUploadImage");
System.Web.UI.WebControls.FileUpload vFile = (System.Web.UI.WebControls.FileUpload)grvItem.Rows[rowIndex].FindControl("fupImage1");
string strimage;
string strfilename, strextn;
if (vFile.HasFile)
{
System.IO.Path.GetExtension(vFile.FileName);
string strfileExtension = System.IO.Path.GetExtension(vFile.FileName).ToLower();
string[] strAllowedFileExtensions = { ".gif", ".jpeg" };
if (strfileExtension == ".jpeg")
strfileExtension = ".gif";
if (FileExtensionIsApproved(strfileExtension) == true)
{
strfilename = System.IO.Path.GetFileName(vFile.PostedFile.FileName);
strextn = System.IO.Path.GetExtension(strfilename);
strimage = ObjDataAccess.LoginName + "_" + DateTime.Now.ToString("yyyyMMddHHmmssfffffff") + strextn;
ViewState["Filename"] = strimage;
string strpath = ConfigurationManager.AppSettings["ITEMPHOTO"] + strimage;
//Server.MapPath("empphoto") + "\\" + strimage;
vFile.PostedFile.SaveAs(strpath);
if (hifimgbrowser.Value == "IE")
{
vimgView.ImageUrl = strpath;
// tstimg.HRef = vimgView.ImageUrl;
}
else
{
vimgView.ImageUrl = "~/Handler.ashx?id=" + strpath;
// tstimg.HRef = imgView.ImageUrl;
}
vimgView.Visible = true;
System.IO.Stream fs = vFile.PostedFile.InputStream;
byte[] bytMyData = new byte[fs.Length + 1];
fs.Read(bytMyData, 0, Convert.ToInt32(fs.Length));
fs.Close();
}
else
{
// DeleteImageFile();
vimgView.ImageUrl = "";
// tstimg.HRef = "";
vimgView.Visible = true;
}
}
}
catch (Exception ex)
{
strMsg = ex.Message;
ScriptManager.RegisterStartupScript(Page, this.GetType(), "alertScript", "showMsgbox('" + strMsg + "','','2','');", true);
}
}
Thanks in advance.
can you debug, and make sure that findcontrol method is not returning null, and also you place fileupload control in EditTemplate, so you have to check in this way in GridView _PreRender events
may be this will help you…