主题:C#转VB.net问题
C# Code 测试后完全可以用. 没有一点问题
前台:
<head id="Head1" runat="server">
<title>Untitled Page</title>
</head>
<html xmlns="http://www.w3.org/1999/xhtml" >
<body>
<form id="form1" runat="server">
<div>
<table width="100%">
<tr>
<td align="right">
<asp:Button ID="btnAddNewRow" runat="server" Font-Bold="True" ForeColor="SteelBlue"
OnClick="btnAddNewRow_Click" Text="Add New Row" /></td>
</tr>
</table>
<asp:Table ID="Table1" Width="100%" runat="server" BorderStyle="Solid" BackColor="#FFFFC0" BorderWidth="1px" Font-Names="Tahoma" Font-Size="Small" GridLines="Both">
<asp:TableRow ID="trTitle" runat="server" BackColor ="Black" ForeColor="white">
<asp:TableCell ColumnSpan="6" ID="trTitleTC0" runat="server" HorizontalAlign="Center">To be Completed bu Hospital / Health Plan</asp:TableCell>
<asp:TableCell ID="trTitleTC1" runat="server" HorizontalAlign="Right">To be Completed by Me</asp:TableCell>
</asp:TableRow>
<asp:TableRow ID="trHeader" runat="server" BackColor ="white" ForeColor="Black">
<asp:TableCell ID="trHeaderTC0" runat="server" Width="12%">Mother's Mame</asp:TableCell>
<asp:TableCell ID="trHeaderTC1" runat="server" Width="12%">Mother'sCIN</asp:TableCell>
<asp:TableCell ID="trHeaderTC2" runat="server" Width="12%">Newborn's Mame</asp:TableCell>
<asp:TableCell ID="trHeaderTC3" runat="server" Width="12%">Newborn's CIN</asp:TableCell>
<asp:TableCell ID="trHeaderTC4" runat="server" Width="12%">Sex (M/F)</asp:TableCell>
<asp:TableCell ID="trHeaderTC5" runat="server" Width="12%">Newborn's DOB</asp:TableCell>
<asp:TableCell ID="trHeaderTC6" runat="server">Comments</asp:TableCell>
</asp:TableRow> </asp:Table>
</div>
</form>
</body>
</html>
后台cs:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
public partial class TestTable : System.Web.UI.Page
{
List<TableRow> myRows = new List<TableRow>();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Table1.Rows.Add(GetNewRow());
}
else
{
myRows = (List<TableRow>)Session["myRows"];
foreach (TableRow dr in myRows)
{
Table1.Rows.Add(dr);
}
}
}
private TableRow GetNewRow()
{
TableRow myRow = new TableRow();
myRow.Cells.Add(new TableCell());
myRow.Cells.Add(new TableCell());
myRow.Cells.Add(new TableCell());
myRow.Cells.Add(new TableCell());
myRow.Cells.Add(new TableCell());
myRow.Cells.Add(new TableCell());
myRow.Cells.Add(new TableCell());
TextBox txtMothorName = new TextBox();
TextBox txtMotherCIN = new TextBox();
TextBox txtNewbornName = new TextBox();
TextBox txtNewbornCIN = new TextBox();
RadioButtonList rdbSex = new RadioButtonList();
ListItem m = new ListItem("Male", "M");
ListItem f = new ListItem("Female", "F");
rdbSex.Items.Add(m);
rdbSex.Items.Add(f);
TextBox txtNewbornDOB = new TextBox();
TextBox txtComments = new TextBox();
txtMothorName.ID = "txtMothorName" + Table1.Rows.Count;
txtMotherCIN.ID = "txtMotherCIN" + Table1.Rows.Count;
txtNewbornName.ID = "txtNewbornName" + Table1.Rows.Count;
txtNewbornCIN.ID = "txtNewbornCIN" + Table1.Rows.Count;
rdbSex.ID = "rdbSex" + Table1.Rows.Count;
txtNewbornDOB.ID = "txtNewbornDOB" + Table1.Rows.Count;
txtComments.ID = "txtComments" + Table1.Rows.Count;
myRow.Cells[0].Controls.Add(txtMothorName);
myRow.Cells[1].Controls.Add(txtMotherCIN);
myRow.Cells[2].Controls.Add(txtNewbornName);
myRow.Cells[3].Controls.Add(txtNewbornCIN);
myRow.Cells[4].Controls.Add(rdbSex);
myRow.Cells[5].Controls.Add(txtNewbornDOB);
myRow.Cells[6].Controls.Add(txtComments);
myRows.Add(myRow);
Session["myRows"] = myRows;
return myRow;
}
protected void btnAddNewRow_Click(object sender, EventArgs e)
{
Table1.Rows.Add(GetNewRow());
}
}
这里是VB.NET code, 前台和C#里的一样,就是名字改了, run起来是没问题, 就是添加行的时候就出问题了.
Imports System
Imports System.Data
Imports System.Configuration
Imports System.Collections
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
Imports System.Collections.Generic
Partial Public Class TestTable1
Inherits System.Web.UI.Page
Private myRows As New List(Of TableRow)()
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
If Not IsPostBack Then
Table1.Rows.Add(GetNewRow())
Else
myRows = DirectCast(Session("myRows"), List(Of TableRow))
For Each dr As TableRow In myRows
Table1.Rows.Add(dr)
Next
End If
End Sub
Private Function GetNewRow() As TableRow
Dim myRow As New TableRow()
myRow.Cells.Add(New TableCell())
myRow.Cells.Add(New TableCell())
myRow.Cells.Add(New TableCell())
myRow.Cells.Add(New TableCell())
myRow.Cells.Add(New TableCell())
myRow.Cells.Add(New TableCell())
myRow.Cells.Add(New TableCell())
Dim txtMothorName As New TextBox()
Dim txtMotherCIN As New TextBox()
Dim txtNewbornName As New TextBox()
Dim txtNewbornCIN As New TextBox()
Dim rdbSex As New RadioButtonList()
Dim m As New ListItem("Male", "M")
Dim f As New ListItem("Female", "F")
rdbSex.Items.Add(m)
rdbSex.Items.Add(f)
Dim txtNewbornDOB As New TextBox()
Dim txtComments As New TextBox()
txtMothorName.ID = "txtMothorName" + Table1.Rows.Count.ToString()
txtMotherCIN.ID = "txtMotherCIN" + Table1.Rows.Count.ToString()
txtNewbornName.ID = "txtNewbornName" + Table1.Rows.Count.ToString()
txtNewbornCIN.ID = "txtNewbornCIN" + Table1.Rows.Count.ToString()
rdbSex.ID = "rdbSex" + Table1.Rows.Count.ToString()
txtNewbornDOB.ID = "txtNewbornDOB" + Table1.Rows.Count.ToString()
txtComments.ID = "txtComments" + Table1.Rows.Count.ToString()
myRow.Cells(0).Controls.Add(txtMothorName)
myRow.Cells(1).Controls.Add(txtMotherCIN)
myRow.Cells(2).Controls.Add(txtNewbornName)
myRow.Cells(3).Controls.Add(txtNewbornCIN)
myRow.Cells(4).Controls.Add(rdbSex)
myRow.Cells(5).Controls.Add(txtNewbornDOB)
myRow.Cells(6).Controls.Add(txtComments)
myRows.Add(myRow)
Session("myRows") = myRows
Return myRow
End Function
Protected Sub btnAddNewRow_Click(ByVal sender As Object, ByVal e As EventArgs)
Table1.Rows.Add(GetNewRow())
End Sub
End Class
好像是DirectCast这里面出问题了..谢谢大家了.兄弟感激不尽.~!!!
前台:
<head id="Head1" runat="server">
<title>Untitled Page</title>
</head>
<html xmlns="http://www.w3.org/1999/xhtml" >
<body>
<form id="form1" runat="server">
<div>
<table width="100%">
<tr>
<td align="right">
<asp:Button ID="btnAddNewRow" runat="server" Font-Bold="True" ForeColor="SteelBlue"
OnClick="btnAddNewRow_Click" Text="Add New Row" /></td>
</tr>
</table>
<asp:Table ID="Table1" Width="100%" runat="server" BorderStyle="Solid" BackColor="#FFFFC0" BorderWidth="1px" Font-Names="Tahoma" Font-Size="Small" GridLines="Both">
<asp:TableRow ID="trTitle" runat="server" BackColor ="Black" ForeColor="white">
<asp:TableCell ColumnSpan="6" ID="trTitleTC0" runat="server" HorizontalAlign="Center">To be Completed bu Hospital / Health Plan</asp:TableCell>
<asp:TableCell ID="trTitleTC1" runat="server" HorizontalAlign="Right">To be Completed by Me</asp:TableCell>
</asp:TableRow>
<asp:TableRow ID="trHeader" runat="server" BackColor ="white" ForeColor="Black">
<asp:TableCell ID="trHeaderTC0" runat="server" Width="12%">Mother's Mame</asp:TableCell>
<asp:TableCell ID="trHeaderTC1" runat="server" Width="12%">Mother'sCIN</asp:TableCell>
<asp:TableCell ID="trHeaderTC2" runat="server" Width="12%">Newborn's Mame</asp:TableCell>
<asp:TableCell ID="trHeaderTC3" runat="server" Width="12%">Newborn's CIN</asp:TableCell>
<asp:TableCell ID="trHeaderTC4" runat="server" Width="12%">Sex (M/F)</asp:TableCell>
<asp:TableCell ID="trHeaderTC5" runat="server" Width="12%">Newborn's DOB</asp:TableCell>
<asp:TableCell ID="trHeaderTC6" runat="server">Comments</asp:TableCell>
</asp:TableRow> </asp:Table>
</div>
</form>
</body>
</html>
后台cs:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
public partial class TestTable : System.Web.UI.Page
{
List<TableRow> myRows = new List<TableRow>();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Table1.Rows.Add(GetNewRow());
}
else
{
myRows = (List<TableRow>)Session["myRows"];
foreach (TableRow dr in myRows)
{
Table1.Rows.Add(dr);
}
}
}
private TableRow GetNewRow()
{
TableRow myRow = new TableRow();
myRow.Cells.Add(new TableCell());
myRow.Cells.Add(new TableCell());
myRow.Cells.Add(new TableCell());
myRow.Cells.Add(new TableCell());
myRow.Cells.Add(new TableCell());
myRow.Cells.Add(new TableCell());
myRow.Cells.Add(new TableCell());
TextBox txtMothorName = new TextBox();
TextBox txtMotherCIN = new TextBox();
TextBox txtNewbornName = new TextBox();
TextBox txtNewbornCIN = new TextBox();
RadioButtonList rdbSex = new RadioButtonList();
ListItem m = new ListItem("Male", "M");
ListItem f = new ListItem("Female", "F");
rdbSex.Items.Add(m);
rdbSex.Items.Add(f);
TextBox txtNewbornDOB = new TextBox();
TextBox txtComments = new TextBox();
txtMothorName.ID = "txtMothorName" + Table1.Rows.Count;
txtMotherCIN.ID = "txtMotherCIN" + Table1.Rows.Count;
txtNewbornName.ID = "txtNewbornName" + Table1.Rows.Count;
txtNewbornCIN.ID = "txtNewbornCIN" + Table1.Rows.Count;
rdbSex.ID = "rdbSex" + Table1.Rows.Count;
txtNewbornDOB.ID = "txtNewbornDOB" + Table1.Rows.Count;
txtComments.ID = "txtComments" + Table1.Rows.Count;
myRow.Cells[0].Controls.Add(txtMothorName);
myRow.Cells[1].Controls.Add(txtMotherCIN);
myRow.Cells[2].Controls.Add(txtNewbornName);
myRow.Cells[3].Controls.Add(txtNewbornCIN);
myRow.Cells[4].Controls.Add(rdbSex);
myRow.Cells[5].Controls.Add(txtNewbornDOB);
myRow.Cells[6].Controls.Add(txtComments);
myRows.Add(myRow);
Session["myRows"] = myRows;
return myRow;
}
protected void btnAddNewRow_Click(object sender, EventArgs e)
{
Table1.Rows.Add(GetNewRow());
}
}
这里是VB.NET code, 前台和C#里的一样,就是名字改了, run起来是没问题, 就是添加行的时候就出问题了.
Imports System
Imports System.Data
Imports System.Configuration
Imports System.Collections
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
Imports System.Collections.Generic
Partial Public Class TestTable1
Inherits System.Web.UI.Page
Private myRows As New List(Of TableRow)()
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
If Not IsPostBack Then
Table1.Rows.Add(GetNewRow())
Else
myRows = DirectCast(Session("myRows"), List(Of TableRow))
For Each dr As TableRow In myRows
Table1.Rows.Add(dr)
Next
End If
End Sub
Private Function GetNewRow() As TableRow
Dim myRow As New TableRow()
myRow.Cells.Add(New TableCell())
myRow.Cells.Add(New TableCell())
myRow.Cells.Add(New TableCell())
myRow.Cells.Add(New TableCell())
myRow.Cells.Add(New TableCell())
myRow.Cells.Add(New TableCell())
myRow.Cells.Add(New TableCell())
Dim txtMothorName As New TextBox()
Dim txtMotherCIN As New TextBox()
Dim txtNewbornName As New TextBox()
Dim txtNewbornCIN As New TextBox()
Dim rdbSex As New RadioButtonList()
Dim m As New ListItem("Male", "M")
Dim f As New ListItem("Female", "F")
rdbSex.Items.Add(m)
rdbSex.Items.Add(f)
Dim txtNewbornDOB As New TextBox()
Dim txtComments As New TextBox()
txtMothorName.ID = "txtMothorName" + Table1.Rows.Count.ToString()
txtMotherCIN.ID = "txtMotherCIN" + Table1.Rows.Count.ToString()
txtNewbornName.ID = "txtNewbornName" + Table1.Rows.Count.ToString()
txtNewbornCIN.ID = "txtNewbornCIN" + Table1.Rows.Count.ToString()
rdbSex.ID = "rdbSex" + Table1.Rows.Count.ToString()
txtNewbornDOB.ID = "txtNewbornDOB" + Table1.Rows.Count.ToString()
txtComments.ID = "txtComments" + Table1.Rows.Count.ToString()
myRow.Cells(0).Controls.Add(txtMothorName)
myRow.Cells(1).Controls.Add(txtMotherCIN)
myRow.Cells(2).Controls.Add(txtNewbornName)
myRow.Cells(3).Controls.Add(txtNewbornCIN)
myRow.Cells(4).Controls.Add(rdbSex)
myRow.Cells(5).Controls.Add(txtNewbornDOB)
myRow.Cells(6).Controls.Add(txtComments)
myRows.Add(myRow)
Session("myRows") = myRows
Return myRow
End Function
Protected Sub btnAddNewRow_Click(ByVal sender As Object, ByVal e As EventArgs)
Table1.Rows.Add(GetNewRow())
End Sub
End Class
好像是DirectCast这里面出问题了..谢谢大家了.兄弟感激不尽.~!!!