我在代码中创建了一个动态表,该表后面会加载页面。我创建了一个按钮,单击该按钮时,需要向表中的特定<div>
添加一个<td>
。但是,没有找到使用id我<td>
元素。我在做什么错了?
Function CalendarRefresh(Day As Integer, MonthDays As Integer)
Dim iDay As Integer = 1
Dim TableID As Integer
Dim TDCount As Integer = 0
Dim FullTDCount As Integer = 0 '42
Dim StringHtml As New StringBuilder
Dim DaysInMonth As Integer = MonthDays
clsWork.GetUnscheduledWork()
Dim ClientName = "Terence Creighton" ' Test Replace with DB Value
Dim JobName = "Install Job"
' Top structure of table
StringHtml.Append("<table id='calendar' runatserver='server'>")
StringHtml.Append("<tr class='weekdays'>")
StringHtml.Append("<th scope='col'>Sunday</th>")
StringHtml.Append("<th scope='col'>Monday</th>")
StringHtml.Append("<th scope='col'>Tuesday</th>")
StringHtml.Append("<th scope='col'>Wednesday</th>")
StringHtml.Append("<th scope='col'>Thursday</th>")
StringHtml.Append("<th scope='col'>Friday</th>")
StringHtml.Append("<th scope='col'>Saturday</th>")
StringHtml.Append("</tr>")
StringHtml.Append("<tr Class='days'>")
If Day > 1 Then
Do While iDay < (Day)
' add Previous month style
StringHtml.Append("<td class='day other-month'>")
StringHtml.Append("</td>")
iDay = iDay + 1
TDCount = TDCount + 1
FullTDCount = FullTDCount + 1
Loop
End If
For i As Integer = 1 To DaysInMonth
If TDCount = 7 Then
StringHtml.Append("</tr>")
StringHtml.Append("<tr class='days'>")
TDCount = 0
FullTDCount = FullTDCount + 1
i = i - 1
Else
StringHtml.Append("<td class='day' ")
StringHtml.Append("id='")
StringHtml.Append(i)
StringHtml.Append("' Runat='server'>")
StringHtml.Append("<div class='date'>")
StringHtml.Append(i)
StringHtml.Append("</div>")
'StringHtml.Append("<div id='")
'StringHtml.Append(i)
'StringHtml.Append("' Runat='server'>")
'StringHtml.Append("<div Class='panel panel-primary' draggable='true'>")
'StringHtml.Append("<div Class='panel-heading'>")
'StringHtml.Append(ClientName)
'StringHtml.Append("</div>")
'StringHtml.Append("<div Class='panel-body'>")
'StringHtml.Append(JobName)
'StringHtml.Append("</div>")
'StringHtml.Append("</div>")
StringHtml.Append("</div>")
StringHtml.Append("</td>")
TDCount = TDCount + 1
FullTDCount = FullTDCount + 1
End If
Next
StringHtml.Append("</tr>")
StringHtml.Append("</table>")
Return StringHtml.ToString
End Function
Public Sub ScheduledJobs()
Dim StringHtml As New StringBuilder
Dim ClientName As String
Dim JobName As String
Dim Work = clsWork.GetUnscheduledWork()
For Each i As Integer In Work.Rows.Count
ClientName = Work.Rows(i).Items("ClientName").ToString
JobName = Work.Rows(i).Items("JobName").ToString
ID = i.ToString
StringHtml.Append("<div class='panel panel-primary' draggable='true' ondragstart='OnDragStart' ondrop='OnDrop' ")
StringHtml.Append("id='")
StringHtml.Append(ID)
StringHtml.Append("'>")
StringHtml.Append("<div class='panel-heading'>")
StringHtml.Append(ClientName)
StringHtml.Append("</div>")
StringHtml.Append("<div class='panel-body'>")
StringHtml.Append(JobName)
StringHtml.Append("</div>")
Next
Dim MyTable As HtmlTable = Page.FindControl("calendar")
Dim MyCell As HtmlTableCell
MyCell.ID = "19"
If MyCell Is Nothing Then
messageResponse = "Tablecell not found"
Else
MyCell.InnerHtml = StringHtml.ToString
End If
End Sub
Private Sub cmdLoadJobs_ServerClick(sender As Object, e As EventArgs) Handles cmdTry.ServerClick
ScheduledJobs()
End Sub
我希望找到带有ID的td并将html字符串添加到(“ td Element”)。innerhtml中。我已经尝试过findcontrol的各种组合,但是全部变成空的
答案 0 :(得分:1)
您必须创建真正的动态控件。这是一个如何与动态创建的表进行交互的非常基本的示例。
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack == false)
{
//do not create dynamic control in an ispostback check
}
//create some table and it's rows and cells. note the assignent of an ID
Table table = new Table()
{
ID = "MyTable1"
};
TableRow row = new TableRow()
{
ID = "MyRow1"
};
TableCell cell = new TableCell()
{
ID = "MyCell1",
Text = "My 1st cell"
};
//add the cell to the row
row.Controls.Add(cell);
//add the row to the table
table.Controls.Add(row);
//add the table to the page
PlaceHolder1.Controls.Add(table);
}
protected void Button1_Click(object sender, EventArgs e)
{
//use findcontrol to locate the cell
TableCell cell = PlaceHolder1.FindControl("MyCell1") as TableCell;
//interact with it
cell.Text = "Cell Found!";
}
答案 1 :(得分:1)
我看到了:
StringHtml.Append("<table id='calendar' runatserver='server'>")
虽然它的确精致通过建立一个HTML字符串到原始的HTML推到一个网页浏览器,的您不能创建服务器控件这样。的你将不能够在HTML访问什么从您的代码后面。 runat='server'
部分首先输入了错误的密码,但是即使正确书写也将毫无用处。到您进入Page_Load事件时,寻找runat='server'
属性的所有内容均已已完成。