[英]VB.net to C# conversion in asp.net
我在這個領域很新。 我有用vb.net編寫的腳本,但我想在C#中進行轉換,但不能作為vb.net代碼使用。因此請幫助。
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="2"
ForeColor="#333333" GridLines="None" AllowPaging="True">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lnkdownload" runat="server" Text="Download" CommandName="Download"
CommandArgument='<%#Eval("FullName") +";" + Eval("Name") %>'></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Name" HeaderText="File Name" />
<asp:BoundField DataField="Length" HeaderText="Size (Bytes)" />
</Columns>
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#999999" />
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
</asp:GridView>
。
Private Sub BindGrid()
Dim DataDirectory As String = "~/Uploads"
Dim files() As FileInfo = New DirectoryInfo(Server.MapPath(DataDirectory)).GetFiles
GridView1.DataSource = files
GridView1.DataBind()
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
BindGrid()
End If
End Sub
Private Sub Downloadfile(ByVal fileName As String, ByVal FullFilePath As String)
Response.AddHeader("Content-Disposition", "attachment; filename=" & fileName)
Response.TransmitFile(FullFilePath)
Response.End()
End Sub
Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand
If e.CommandName = "Download" Then
Dim fileInfo() As String = e.CommandArgument.ToString().Split(";")
Dim FileName As String = fileInfo(1)
Dim FullPath As String = fileInfo(0)
Downloadfile(FileName, FullPath)
End If
End Sub
Protected Sub GridView1_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles GridView1.PageIndexChanging
GridView1.PageIndex = e.NewPageIndex
BindGrid()
End Sub
這是使用VB.NET到C#轉換器轉換為c#的代碼:
private void BindGrid()
{
string DataDirectory = "~/Uploads";
FileInfo[] files = new DirectoryInfo(Server.MapPath(DataDirectory)).GetFiles;
GridView1.DataSource = files;
GridView1.DataBind();
}
protected void Page_Load(object sender, System.EventArgs e)
{
if (!IsPostBack) {
BindGrid();
}
}
private void Downloadfile(string fileName, string FullFilePath)
{
Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName);
Response.TransmitFile(FullFilePath);
Response.End();
}
protected void GridView1_RowCommand(object sender,
System.Web.UI.WebControls.GridViewCommandEventArgs e)
{
if (e.CommandName == "Download") {
string[] fileInfo = e.CommandArgument.ToString().Split(";");
string FileName = fileInfo[1];
string FullPath = fileInfo[0];
Downloadfile(FileName, FullPath);
}
}
protected void GridView1_PageIndexChanging(object sender,
System.Web.UI.WebControls.GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
BindGrid();
}
C#沒有VB.Net那樣的“ Handles”關鍵字,因此看來您的GridView沒有正確連接到必要的處理程序。 嘗試將以下參數添加到GridView標記中:
OnRowCommand="GridView1_RowCommand"
請注意,您還需要對頁面索引更改事件執行相同的操作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.