在网格视图中,我上传了带有图片图标的文件。为了在网格视图中有图像图标并获取行中上传文件的类型(命令Argument),我使用了图像按钮,它显示了图像图标但是当我点击图像按钮时,我有错误“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>