我有Tab容器,里面有一个GridView。我点击特定行时需要上传图片。我也在使用更新面板,在网格视图之外,我能够使用PostBack Trigger将图像上传到“预览按钮”,但在gridview中我不知道如何访问它。我刚刚在gridview之外创建了一个名为btngrvPreview的按钮,并将其click事件调用到按钮btnPreview1,该按钮位于gridView中。我认为这种方法是正确的..但我不知道为什么这个触发器到btnPreview1不起作用。
显示if(vFile.HasFile)条件是否总是变错..
帮我解决这个问题。
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>
按钮单击“执行”
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);
}
}
提前致谢。
答案 0 :(得分:0)
你可以调试,并确保findcontrol方法不返回null,并且你将fileupload控件放在EditTemplate中,所以你必须在GridView _PreRender事件中以这种方式检查
if (this.GridView1.EditIndex != -1)
{
FileUpload upload = GridView1.Rows[GridView1.EditIndex].FindControl("fupImage1") as Button;
if (upload != null)
{
//do something
}
}
可能会对你有所帮助......