在下面的代码中,我有一个网格视图,当我尝试分页时,我有数据,它会在链接按钮中抛出错误,无法转换类型为' System.Web.UI.WebControls.GridView&#39的对象;键入' System.Web.UI.WebControls.LinkButton.pls帮助我解决问题。
protected void Attchdwnld_RowCommand(object sender, GridViewCommandEventArgs e)
{
var row = ((LinkButton)e.CommandSource).NamingContainer as GridViewRow;
var attachmentNameLabel = row.FindControl("AttachmentFileName") as Label;
// var SearchDoc = (SearchDoc)Session["Documentname"];
lblMessage.Text = attachmentNameLabel.Text;
string fileurl = "C:\\Search\\" + strClientName + "\\" + strAttachmentFolder + "\\" + lblMessage.Text;
string filename = fileurl;
if (filename != "")
{
string path = filename;
System.IO.FileInfo file = new System.IO.FileInfo(path);
if (file.Exists)
{
Response.Clear();
Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
Response.AddHeader("Content-Length", file.Length.ToString());
Response.ContentType = "application/octet-stream";
Response.WriteFile(file.FullName);
Response.End();
}
else
{
Response.Write("This file does not exist.");
}
}
var SearchDoc = (SearchDoc)Session["Documentname"];
string odoc = SearchDoc.DocumentID.ToString();
DocumentServiceClient Doc = new DocumentServiceClient();
DataTable dtAttachment = Doc.GetDocumentsByDocumentID(odoc).Tables[0];
Attchdwnld.DataSource = dtAttachment;
Attchdwnld.DataBind();
}
<asp:GridView Width="100%" runat="server" ID="Attchdwnld" AutoGenerateColumns="false" CellPadding="4" ForeColor="#333333" ShowFooter="false"
PageSize-Mode="NumericPages" PageSize="1" PagerStyle-Visible="true" OnPageIndexChanging="Attchdwnld_PageIndex" AllowPaging="true" AllowSorting="true"
OnRowCommand="Attchdwnld_RowCommand"
CssClass="mGrid"
PagerStyle-CssClass="pgr" AlternatingRowStyle-CssClass="alt">
<Columns>
<asp:TemplateField HeaderText="DocumentID" ItemStyle-Width="200px" Visible="false" >
<ItemTemplate>
<asp:Label ID="DocumentID" runat="server" Text='<%#Eval("DocumentID") %>'> </asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="AttachmentFileName" ItemStyle-Width="200px" >
<ItemTemplate>
<asp:Label ID="AttachmentFileName" runat="server" Text='<%#Eval("AttachmentFileName") %>'> </asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="DownLoad" itemstyle-width="150px">
<ItemTemplate>
<asp:LinkButton ID="btnEdit" runat="server" CommandName="View" Text="Download" CausesValidation="false"/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<HeaderStyle Font-Bold="True" ForeColor="White" />
</asp:GridView>
答案 0 :(得分:0)
RowCommand事件可能是由GridView中的许多操作引起的(分页,排序,删除,e.t.c。)。您必须使用CommandName来区分事件,例如,您可以尝试:
protected void Attchdwnld_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "View")
{
var row = ((LinkButton)e.CommandSource).NamingContainer as GridViewRow;
var attachmentNameLabel = row.FindControl("AttachmentFileName") as Label;
// var SearchDoc = (SearchDoc)Session["Documentname"];
lblMessage.Text = attachmentNameLabel.Text;
string fileurl = "C:\\Search\\" + strClientName + "\\" + strAttachmentFolder + "\\" + lblMessage.Text;
string filename = fileurl;
if (filename != "")
{
string path = filename;
System.IO.FileInfo file = new System.IO.FileInfo(path);
if (file.Exists)
{
Response.Clear();
Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
Response.AddHeader("Content-Length", file.Length.ToString());
Response.ContentType = "application/octet-stream";
Response.WriteFile(file.FullName);
Response.End();
}
else
{
Response.Write("This file does not exist.");
}
}
var SearchDoc = (SearchDoc)Session["Documentname"];
string odoc = SearchDoc.DocumentID.ToString();
DocumentServiceClient Doc = new DocumentServiceClient();
DataTable dtAttachment = Doc.GetDocumentsByDocumentID(odoc).Tables[0];
Attchdwnld.DataSource = dtAttachment;
Attchdwnld.DataBind();
}
}
其中CommandName == "View"
是您链接按钮的命令。
答案 1 :(得分:0)
错误显然在这里:
var row = ((LinkButton)e.CommandSource).NamingContainer as GridViewRow;
原因是e.CommandSource
实际上属于GridView
类型而非类型LinkButton
。您需要将行更改为:
var row = (GridView)e.CommandSource;
上面的代码将摆脱你所获得的ClassCastException
,但我不确定它是否会使你的代码按预期运行。我建议你改用这个策略:
在CommandArgument
:
<asp:TemplateField HeaderText="DownLoad" itemstyle-width="150px">
<ItemTemplate>
<asp:LinkButton ID="btnEdit" runat="server" CommandName="View" Text="Download" CausesValidation="false"
CommandArgument='<%# Container.DataItemIndex %>'/>
</ItemTemplate>
</asp:TemplateField>
现在在您的代码中,只需执行此操作:
protected void Attchdwnld_RowCommand(object sender, GridViewCommandEventArgs e)
{
int index = Convert.ToInt32(e.CommandArgument);
// Retrieve the row that contains the button clicked
// Use the CommandSource property to access the GridView control.
GridView yourGrid = (GridView)e.CommandSource;
GridViewRow row = yourGrid.Rows[index];
// rest of your code... you now have the instance of the row that fired the event.