【问题标题】:Open form through path stored in database column on button click单击按钮时通过存储在数据库列中的路径打开表单
【发布时间】:2013-09-13 01:26:28
【问题描述】:

我正在尝试访问存储在数据库列中的文件。在使用标签时,我可以让单击命令进行操作,但完全无法在窗口中打开文件路径以进行查看。我过去曾使用过 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);
}

【问题讨论】:

标签: c# asp.net gridview


【解决方案1】:

&lt;ItemTemplate&gt; 中使用服务器控件而不是 HTML 控件 (&lt;a&gt;),如下所示:

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

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

【讨论】:

  • 很高兴这对您有所帮助,请随时为答案投票。 :-)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-11-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多