通过按钮单击时存储在数据库列中的路径打开表单

时间:2013-09-12 16:43:38

标签: c# asp.net gridview

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

Public.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>

Public.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);
}

1 个答案:

答案 0 :(得分:0)

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

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

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