【问题标题】:How to open a document onclick of linkbutton according如何根据点击链接按钮打开文档
【发布时间】:2015-04-29 10:51:41
【问题描述】:

我想根据相应的链接按钮 ID 在新窗口中打开我上传的文档。文档以完整路径保存在数据库中。

<asp:LinkButton ID="lnkstdres" runat="server" Text='<%#Eval("Alias") %>' CommandArgument='<%#Eval("Idinst") %>' CommandName="StudentRes" OnClick="lnkstdres_Click"></asp:LinkButton>  

protected void lnkstdres_Click(object sender, EventArgs e)
    {

    }

我不知道该怎么做。 谢谢你

【问题讨论】:

    标签: c# .net


    【解决方案1】:
    if( ( File1.PostedFile != null ) && ( File1.PostedFile.ContentLength > 0 ) )
    {
        string fn = System.IO.Path.GetFileName(File1.PostedFile.FileName);
        string SaveLocation = Server.MapPath("Data") + "\\" +  fn;
        try
        {
            File1.PostedFile.SaveAs(SaveLocation);
            Response.Write("The file has been uploaded.");
        }
        catch ( Exception ex )
        {
            Response.Write("Error: " + ex.Message);
            //Note: Exception.Message returns a detailed message that describes the current exception. 
            //For security reasons, we do not recommend that you return Exception.Message to end users in 
            //production environments. It would be better to put a generic error message. 
        }
    }
    else
    {
        Response.Write("Please select a file to upload.");
    }
    

    【讨论】:

      【解决方案2】:

      我正在发布我的问题的解决方案。

       <asp:GridView ID="grvstdRes" runat="server" CssClass="table table-bordered table-hover" OnRowCommand="grvstdRes_RowCommand" AutoGenerateColumns="false">
                                                      <Columns>
                                                          <asp:TemplateField HeaderText="Document">
                                                              <ItemTemplate>
                                                                  <asp:LinkButton ID="lnkstdres" runat="server" Text='<%#Eval("Alias") %>' 
      
      CommandArgument='<%#Eval("Idinstance") %>' CommandName="StudentResAliasName" ></asp:LinkButton>
                                                                  </ItemTemplate>
                                                              </asp:TemplateField>
                                                          </Columns>
                                                     </asp:GridView>
      
         protected void grvstdRes_RowCommand(object sender, GridViewCommandEventArgs e)
          { 
              if(e.CommandName=="StudentResAliasName")
              {  
                  using(objResultUnitBLL=new UnitBLL())
                  {
                      using(objUnitDTO=new UnitDTO())
                      {
                          objUnitDTO = objResultUnitBLL.GetStudentResourceFile(Convert.ToInt64(e.CommandArgument));
                           string fileName=objUnitDTO.FileName; //to get the full path from DB
                           string[] pathArr = fileName.Split('\\'); // To split the path 
                           fileName = pathArr.Last().ToString(); //To get the file name  with extension
                           Page.ClientScript.RegisterStartupScript(this.GetType(), "OpenWindow", "window.open('../Upload/Instance/" + fileName + "','_newtab');", true); //to display the file in new window.
                      }
                  }
              } 
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-07-16
        • 2016-03-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-06-10
        • 1970-01-01
        相关资源
        最近更新 更多