1
폴더의 파일 이름을 GridView 컨트롤에 표시해야합니다.ASP.Net에서 Gridview 컨트롤의 폴더에서 파일 이름 가져 오기 C#
디렉토리 클래스을 사용했다고 생각합니다. 내 데이터베이스에서
나는 각 행에 대해이 값을 열 에게 sFolder이 : 나는 웹이 tutorial 시도
control/Imp/foo
하지만 난에 폴더에서 파일 이름을 얻을 수 없다 GridView 컨트롤.
오류가 없지만 폴더 경로가 올바르더라도 GridView가 비어 있습니다.
아래 코드를 참조하십시오.
도와 주시겠습니까?
정말 # 수정 01.cs
dt2 = new DataTable();
ds2 = new DataSet();
sql = @String.Format(" SELECT * FROM tbl_2 WHERE sFolder IN ('control/Imp/foo'); ");
using (OdbcConnection cn =
new OdbcConnection(ConfigurationManager.ConnectionStrings["ConnMySQL"].ConnectionString))
{
using (OdbcCommand cmd =
new OdbcCommand(sql, cn))
{
OdbcDataAdapter adapter =
new OdbcDataAdapter(cmd);
adapter.Fill(ds2);
if (ds2.Tables.Count > 0)
{
dt2 = ds2.Tables[0];
FilePath = Server.MapPath("/myFolder/" + ds2.Tables[0].Rows[0]["sFolder"].ToString().Replace('/', '\\'));
Response.Write(FilePath);
// the response write FilePath is C:\inetpub\wwwroot\aspnet\myFolder\control\Imp\foo //
string[] filesLoc = Directory.GetFiles(FilePath);
List<ListItem> files = new List<ListItem>();
foreach (string file in filesLoc)
{
files.Add(new ListItem(Path.GetFileName(file)));
}
gvDownload.DataSource = files;
gvDownload.DataBind();
}
}
}
return ds2;
에서 .aspx
<asp:GridView ID="gvDownload" EmptyDataText="Data empty"
runat="server" AutoGenerateColumns="False"
BackColor="White" BorderColor="#999999" BorderStyle="None" BorderWidth="1px" GridLines="Vertical">
<AlternatingRowStyle BackColor="#DCDCDC" />
<Columns>
<asp:BoundField DataField="Text" HeaderText="FileName" />
</Columns>
</asp:GridView>
감사, 사전에 어떤 도움을 주셔서 감사합니다
protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { BindData(); } } private void BindData() { RetrieveProductsDowload(); } private DataSet RetrieveProductsDowload() { dt2 = new DataTable(); ds2 = new DataSet(); sql = @String.Format(" SELECT * FROM tbl_2 WHERE sFolder IN ('control/Imp/foo'); "); using (OdbcConnection cn = new OdbcConnection(ConfigurationManager.ConnectionStrings["ConnMySQL"].ConnectionString)) { using (OdbcCommand cmd = new OdbcCommand(sql, cn)) { OdbcDataAdapter adapter = new OdbcDataAdapter(cmd); adapter.Fill(ds2); if (ds2.Tables.Count > 0) { dt2 = ds2.Tables[0]; FilePath = Server.MapPath("/myFolder/" + ds2.Tables[0].Rows[0]["sFolder"].ToString().Replace('/', '\\')); Response.Write(FilePath); // the response write FilePath is C:\inetpub\wwwroot\aspnet\myFolder\control\Imp\foo // string[] filesLoc = Directory.GetFiles(FilePath); List<ListItem> files = new List<ListItem>(); foreach (string file in filesLoc) { files.Add(new ListItem(Path.GetFileName(file))); } gvDownload.DataSource = files; gvDownload.DataBind(); } } } return ds2; }
시도 :'gvDownload.DataSource = Directory.GetFiles (FilePath를); gvDownload.DataBind();','AutoGenerateColumns = "True"'를 사용하고 GridView에서 ' ... '을 제거하십시오. 작동합니까? –
Ritesh
@ 답 : 답장을 보내 주셔서 감사합니다. 작동하지 않으면 GV가 비어 있습니다. –
이 코드를 작성하려면 .cs 파일에서'Page_Load'를 사용 했습니까? – Ritesh