1

我正在尝试访问存储在数据库列中的文件。在使用标签时,我可以让单击命令进行操作,但完全无法在窗口中打开文件路径以进行查看。我过去曾使用过 Gridview,但从来不必执行按钮单击事件来打开列中的文件路径。

公共.aspx

<asp:GridView ID="GridView1" runat="server" BackColor="White"
              BorderColor="#3366CC" 
              BorderStyle="None" 
              BorderWidth="1px" 
              CellPadding="4" ShowFooter="True" 
              EnableModelValidation="True">
    <Columns>
        <asp:TemplateField>
          <ItemTemplate>
           <a href='<%# DataBinder.Eval(Container.DataItem, "fullpath") %>'>Download</a>
           </ItemTemplate> 
        </asp:TemplateField>
    </Columns>
    <FooterStyle BackColor="#99CCCC" ForeColor="#003399" />
    <RowStyle BackColor="White" ForeColor="#003399" />
    <PagerStyle BackColor="#99CCCC" ForeColor="#003399" HorizontalAlign="Left" />
    <SelectedRowStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" />
    <HeaderStyle BackColor="#003399" Font-Bold="True" ForeColor="#CCCCFF" />
</asp:GridView>

公共.aspx.cs

public void Page_Load(Object sender, EventArgs e)
{
     string connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;

     OdbcConnection myConnection = new OdbcConnection(connectionString);

     OdbcDataAdapter ad = new OdbcDataAdapter("SELECT report_description, rtrim(report_name),report_location, cat_description, rtrim(report_location) || '' || rtrim(report_name) AS FullPath FROM reports, report_categories WHERE reports.report_cat != 'SEC' AND reports.report_cat = 'PUB' AND report_categories.report_cat = reports.report_cat AND report_type IN ('CF','CR') ORDER BY cat_description, report_description", myConnection);

     DataSet ds = new DataSet();
     ad.Fill(ds, "reports");
     GridView1.DataSource = ds;
     GridView1.DataBind();
}

protected void Downloadbtn_Click(object sender, EventArgs e)
{
     string sourceDir = @"\\fs2\Crystal Reports 11\Test Reports\";

     //MessageLabel.Text = "Hello";
     Button clickedButton = sender as Button;
     GridViewRow clickedGridViewRow = (GridViewRow)clickedButton.Parent.Parent;
     MessageLabel.Text = clickedGridViewRow.Cells[5].Text;
     string sUrl = clickedGridViewRow.Cells[1].ToString();

     Process compiler = new Process();
     compiler.StartInfo.UseShellExecute = false;
     compiler.StartInfo.RedirectStandardOutput = true;

     ProcessStartInfo sInfo = new ProcessStartInfo(sourceDir + sUrl);

     Process.Start(sInfo);
}
4

1 回答 1

0

在您的内部<ItemTemplate>使用服务器控件而不是 HTML 控件 ( <a>),如下所示:

<ItemTemplate>
    <asp:LinkButton ID="LinkButtonDownload" runat="server" onclick="Downloadbtn_Click">   
    </asp:LinkButton>
</ItemTemplate>

现在您的服务器端事件将触发。

于 2013-09-12T17:26:28.927 回答