在网格视图中,我上传了带有图像图标的文件。为了在网格视图中有图像图标并在行中获取上传文件的类型(命令参数),我使用了图像按钮,它显示图像图标,但是当我单击图像按钮时,出现错误“System.ArgumentException:无效的回发或回调参数”如果我可以使用图像控件而不是图像按钮对我来说更好,但我不知道如何从行中获取扩展文件类型。此外,如果我们可以在这种情况下禁用链接图像按钮,也没问题。
tblfile 的数据库设计如下:
Fileid(int)
Docid(int)
TransId(int)
Content type varchar(75)
Data varbinary(max)
Extention varchar(50)
CS:
protected void GridViewEfile_DataBound(object sender, EventArgs e)
{
foreach (GridViewRow gvr in GridViewEfile.Rows) //loop each row
{
ImageButton ib = (ImageButton)gvr.FindControl("ib1"); //find imagebutton
string exten = ib.CommandArgument;; //get index of '.'
if (exten == ".pdf")
ib.ImageUrl = @"~/Images/iconpdf.gif"; //set image for imagebutton in each row
else if (exten == ".docx")
ib.ImageUrl = @"~/Images/icondoc.gif";
}
}
protected void ib1_Command(object sender, CommandEventArgs e)
{
_DataContext = new EDMSDataContext();
//you can get your command argument values as follows
string FileId = e.CommandName.ToString();
int _FileId = Convert.ToInt32(FileId);
tblFile Efile = (from ef in _DataContext.tblFiles
where ef.FileId == _FileId
select ef).Single();
if (Efile != null)
{
download(Efile);
}
}
.aspx:
<asp:GridView ID="GridViewEfile" runat="server" AutoGenerateColumns="False" CellPadding="4"
OnDataBound="GridViewEfile_DataBound" ForeColor="Black" DataKeyNames="FileID"
GridLines="None" ShowHeader="False">
<AlternatingRowStyle BackColor="Yellow" />
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton runat="server" ID="ib1" OnCommand="ib1_Command" CommandName='<%#Eval("FileID")%>'
CommandArgument='<%# Eval("Exten") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" OnCommand="LinkButton1_Command" CommandName="Download"
CommandArgument='<%#Eval("FileID")%>'><%#Eval("FileName")%></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="ContentType" />
</Columns>
<EditRowStyle BackColor="#7C6F57" />
<FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="Red" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#E3EAEB" />
<SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
</asp:GridView>