C# aspx中的链接按钮不返回数据

C# aspx中的链接按钮不返回数据,c#,asp.net,gridview,C#,Asp.net,Gridview,这是我在aspx中的网格视图代码: <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" Width="294px" onselectedindexchanged="GridView1_SelectedIndexChanged" > <Column

这是我在aspx中的网格视图代码:

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
        DataSourceID="SqlDataSource1" 
         Width="294px" onselectedindexchanged="GridView1_SelectedIndexChanged" 

         >
        <Columns>
            <asp:BoundField DataField="FID" HeaderText="FID" SortExpression="FID" />
            <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
           <asp:TemplateField ItemStyle-HorizontalAlign="Center">
                <ItemTemplate>
                    <asp:LinkButton ID="lnkDownload" runat="server"

  CommandArgument='<%Eval("FID")%>' OnClick="DownloadFile"

                        Text="Download" />
                </ItemTemplate>
            </asp:TemplateField>

    </Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
                   ConnectionString="<%$ ConnectionStrings:ProjectConnectionString %>" 
                   SelectCommand="SELECT [FID], [Name] FROM [Personal_det]">
</asp:SqlDataSource>
正如您所看到的,下面提到的是我的C代码,用于根据他们的FID下载教员的特定信息,但此代码没有返回任何内容

protected void DownloadFile(object sender, EventArgs e)
{
    string FID = ((LinkButton)sender).CommandArgument;


    var dt = GetData("select * from Personal_det where FID='" + FID + "'");

    if (dt != null && dt.Rows.Count > 0)
    {
        DataRow dr = dt.Rows[0];

        Document doc = new Document(PageSize.A4, 88f, 88f, 10f, 10f);
        Font NormalFont = FontFactory.GetFont("Arial", 12, Font.NORMAL, BaseColor.BLACK);
        using (System.IO.MemoryStream m = new System.IO.MemoryStream())
        {
            PdfWriter w = PdfWriter.GetInstance(doc, m);
            Phrase phrase = null;
            PdfPCell cell = null;
            PdfPTable table = null;
           // BaseColor color = null;
            Paragraph para = null;
            Font times = null;
            BaseFont bfTimes = null;



            doc.Open();
            table = new PdfPTable(2);

            cell = PhraseCell(new Phrase("Faculty Profile", FontFactory.GetFont("Arial", 12, Font.UNDERLINE, BaseColor.BLACK)), PdfPCell.ALIGN_CENTER);
            //table.SpacingBefore = 20f;

            cell.Colspan = 2;
            table.AddCell(cell);
            cell = PhraseCell(new Phrase(), PdfPCell.ALIGN_CENTER);
            cell.Colspan = 2;
            cell.PaddingBottom = 30f;


            //FID
            table.AddCell(PhraseCell(new Phrase("Faculty Code:", FontFactory.GetFont("Arial", 8, Font.BOLD, BaseColor.BLACK)), PdfPCell.ALIGN_LEFT));
            table.AddCell(PhraseCell(new Phrase("mahe" + dr["FID"].ToString(), FontFactory.GetFont("Arial", 8, Font.NORMAL, BaseColor.BLACK)), PdfPCell.ALIGN_LEFT));
            cell = PhraseCell(new Phrase(), PdfPCell.ALIGN_CENTER);
            cell.Colspan = 2;
            cell.PaddingBottom = 10f;
            table.AddCell(cell);

            //Name
            table.AddCell(PhraseCell(new Phrase("Name:", FontFactory.GetFont("Arial", 8, Font.BOLD, BaseColor.BLACK)), PdfPCell.ALIGN_LEFT));
            table.AddCell(PhraseCell(new Phrase(dr["Name"].ToString(), FontFactory.GetFont("Arial", 8, Font.BOLD, BaseColor.BLACK)), PdfPCell.ALIGN_LEFT));
            cell = PhraseCell(new Phrase(), PdfPCell.ALIGN_CENTER);
            cell.Colspan = 2;
            cell.PaddingBottom = 10f;
            table.AddCell(cell);


            //Date of Birth
            table.AddCell(PhraseCell(new Phrase("Date of Birth:", FontFactory.GetFont("Arial", 8, Font.BOLD, BaseColor.BLACK)), PdfPCell.ALIGN_LEFT));
            table.AddCell(PhraseCell(new Phrase(Convert.ToDateTime(dr["DOB"]).ToString("dd MMMM, yyyy"), FontFactory.GetFont("Arial", 8, Font.NORMAL, BaseColor.BLACK)), PdfPCell.ALIGN_LEFT));
            cell = PhraseCell(new Phrase(), PdfPCell.ALIGN_CENTER);
            cell.Colspan = 2;
            cell.PaddingBottom = 10f;
            table.AddCell(cell);

            //Phone Number
            table.AddCell(PhraseCell(new Phrase("Phone Number:", FontFactory.GetFont("Arial", 8, Font.BOLD, BaseColor.BLACK)), PdfPCell.ALIGN_LEFT));
            table.AddCell(PhraseCell(new Phrase(Convert.ToInt64(dr["MobileNo"]).ToString(), FontFactory.GetFont("Arial", 8, Font.NORMAL, BaseColor.BLACK)), PdfPCell.ALIGN_LEFT));
            cell = PhraseCell(new Phrase(), PdfPCell.ALIGN_CENTER);
            cell.Colspan = 2;
            cell.PaddingBottom = 10f;
            table.AddCell(cell);

            //Mail Id
            table.AddCell(PhraseCell(new Phrase("Email ID:", FontFactory.GetFont("Arial", 8, Font.BOLD, BaseColor.BLACK)), PdfPCell.ALIGN_LEFT));
            table.AddCell(PhraseCell(new Phrase(dr["EmailId"].ToString(), FontFactory.GetFont("Arial", 8, Font.BOLD, BaseColor.BLACK)), PdfPCell.ALIGN_LEFT));
            cell = PhraseCell(new Phrase(), PdfPCell.ALIGN_CENTER);
            cell.Colspan = 2;
            cell.PaddingBottom = 10f;
            table.AddCell(cell);

            //Address
            table.AddCell(PhraseCell(new Phrase("Res Address:", FontFactory.GetFont("Arial", 8, Font.BOLD, BaseColor.BLACK)), PdfPCell.ALIGN_LEFT));
            phrase = new Phrase(new Chunk(dr["add1"] + "\n " + dr["add2"] + "\n " + dr["add3"] + "\n " + dr["Pincode"], FontFactory.GetFont("Arial", 8, Font.NORMAL, BaseColor.BLACK)));
            table.AddCell(PhraseCell(phrase, PdfPCell.ALIGN_LEFT));
            cell = PhraseCell(new Phrase(), PdfPCell.ALIGN_CENTER);
            cell.Colspan = 2;
            cell.PaddingBottom = 10f;
            table.AddCell(cell);
            table.SpacingAfter = 30f;
            doc.Add(table);

            //Education Details
            PdfPTable tabl = new PdfPTable(2);
            tabl.SpacingBefore = 10f;
            bfTimes = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, false);
            times = new Font(bfTimes, 12f, Font.UNDERLINE);
            para = new Paragraph("Education Details:", times);
            para.Alignment = Element.ALIGN_LEFT;
            doc.Add(para);
            //tabl.BorderWidth=1;
            //tabl.BorderColor=new BaseColor(0,0,255);
            //cell = new PdfPCell(new Phrase("Education Detail"));
            //cell.Headers=true;
            cell.HorizontalAlignment = 1;
            cell.Colspan = 4;
            cell.Rowspan = 2;
            cell.BorderColor = new BaseColor(0, 0, 0);
            DataRow dr1 = GetData("select * from Education_det ").Rows[0];
            tabl.AddCell(new Phrase("Degree:", FontFactory.GetFont("Arial", 8, Font.BOLD, BaseColor.BLACK)));
            tabl.AddCell(new Phrase(dr1["Degree"].ToString(), FontFactory.GetFont("Arial", 8, Font.BOLD, BaseColor.BLACK)));
            tabl.AddCell(new Phrase("Year:", FontFactory.GetFont("Arial", 8, Font.BOLD, BaseColor.BLACK)));
            tabl.AddCell(new Phrase(Convert.ToInt32(dr1["year"]).ToString(), FontFactory.GetFont("Arial", 8, Font.NORMAL, BaseColor.BLACK)));
            tabl.AddCell(new Phrase("Institution:", FontFactory.GetFont("Arial", 8, Font.BOLD, BaseColor.BLACK)));
            tabl.AddCell(new Phrase(dr1["Institution"].ToString(), FontFactory.GetFont("Arial", 8, Font.BOLD, BaseColor.BLACK)));
            tabl.AddCell(new Phrase("Specialization:", FontFactory.GetFont("Arial", 8, Font.BOLD, BaseColor.BLACK)));
            tabl.AddCell(new Phrase(dr1["Specialization"].ToString(), FontFactory.GetFont("Arial", 8, Font.BOLD, BaseColor.BLACK)));
            cell.Rowspan = 2;
            cell.Colspan = 2;
            tabl.AddCell(cell);
            tabl.SpacingAfter = 30f;
            doc.Add(tabl);
 }
试试这个:

ASPX:

用这个,, 在您的aspx页面中添加此代码

 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1"
    Width="294px" OnRowCommand="GridView1_RowCommand">
    <Columns>
        <asp:TemplateField ItemStyle-HorizontalAlign="Center">
            <ItemTemplate>
                <asp:LinkButton ID="lnkDownload" runat="server" CommandArgument='<%Eval("FID")%>'
                    CommandName="DownloadFile" Text="Download" />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>
您正在使用ItextSharp创建PDF。 在这种情况下,您忘记了关闭文档;单据打开后;
在doc.Addtabl之后;增加单据关闭;然后它就开始工作了。

你为什么要访问网格..你可以直接从数据库中获取..你想在哪里显示检索到的数据?请发布你的GridView_RowDataBound方法。@KarlAnderson我正需要GridView_RowDataBoundMethod@coder如何从数据库中检索它?@samieymehdith答案看起来不错,但我需要数据在DownloadFile链接按钮的旁边,受保护的void DownloadFileobject sender,EventArgs e{var dt=GetDataselect*from Personal_det,其中Fid='+Fid+';如果dt!=null&&dt.Rows.Count>0{DataRow dr=dt.Rows[0];Document doc=new DocumentPageSize.A4、88f、88f、10f、10f;Font-NormalFont=FontFactory.GetFontArial、12、Font.NORMAL、BaseColor.BLACK;使用System.IO.MemoryStream m=new System.IO.MemoryStream{}我的代码看起来有点像这样,但我建议您创建一个处理程序,并使用java脚本调用它来下载文件,而不是将整个页面发布到服务器上。我的代码中包含了这些内容。这是一个很长的代码,我只是添加了一部分,直到它不返回任何内容为止。使用OnCommand=DownloadFile而不是CommandName=DownloadFile,然后检查我如果我使用OnCommand,它会说它不包含OnCommand的定义,这意味着你使用command参数并创建事件,比如protectedvoid DownloadFileobject sender,CommandEventArgs e{string FID=e.CommandArgument.ToString;}。
<asp:TemplateField ItemStyle-HorizontalAlign="Center">
            <ItemTemplate>
                 <asp:LinkButton ID="lnkDownload" runat="server" 
                     CommandArgument='<%Eval("FID")%>' OnClick="DownloadFile"
                      Text="Download" />
            </ItemTemplate>
        </asp:TemplateField>
protected void DownloadFile(object sender, EventArgs e)
{
    string FID = ((LinkButton)sender).CommandArgument;
    var dt = GetData("select * from Personal_det where Fid='" + FID + "'");
    //and so on
}
 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1"
    Width="294px" OnRowCommand="GridView1_RowCommand">
    <Columns>
        <asp:TemplateField ItemStyle-HorizontalAlign="Center">
            <ItemTemplate>
                <asp:LinkButton ID="lnkDownload" runat="server" CommandArgument='<%Eval("FID")%>'
                    CommandName="DownloadFile" Text="Download" />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "DownloadFile")
    {
            //write your code here
    }
}