0

标题标记标记如下所示,它在页面源中生成一个 id,如 FeaturedContent_day3date

<asp:Content ID="Content2" ContentPlaceHolderID="FeaturedContent" Runat="Server">
<div id="pagecontent">
<fieldset id="weeklyPnLGoals">
        <legend>Weekly PnL Targets with Actual for <%=DateTime.Now.ToString("MMM,   yyyy")%></legend>
            <div id="monthlywrapper" style="width:100%;">
                   <div id="firstweekwrapper" class="weekcontainer">
                        <div id="day1" class="daytile daytilefirst">
                            <h3 id="day1date" class="dateh3" runat="server"></h3><br />
                            <p id="day1equities"><span class="targettext"> <span   class="bluetext">Equities</span> Target PnL:&nbsp;</span><span id="day1equitytargetvalue" class="targettext"></span><br /><span class="actualtext">Actual <span class="bluetext">Equities</span> PnL:&nbsp;</span><span id="day1equityactualvalue" class="actualtext"></span></p>
                            <p id="day1options"><span class="targettext"> <span class="bluetext">Options</span> Target PnL:&nbsp;</span><span id="day1optiontargetvalue" class="targettext"></span><br /><span class="actualtext">Actual <span class="bluetext">Options</span> PnL:&nbsp;</span><span id="day1optionactualvalue" class="actualtext"></span></p>
                            <p id="day1futures"><span class="targettext"> <span class="bluetext">Futures</span> Target PnL:&nbsp;</span><span id="day1futuretargetvalue" class="targettext"></span><br /><span class="actualtext">Actual <span class="bluetext">Futures</span> PnL:&nbsp;</span><span id="day1futureactualvalue" class="actualtext"></span></p>
                        </div>
                        <div id="day2" class="daytile">
                            <h3 id="day2date" class="dateh3" runat="server"></h3><br />
                            <p id="day2equities"><span class="targettext"> <span class="bluetext">Equities</span> Target PnL:&nbsp;</span><span id="day2equitytargetvalue" class="targettext"></span><br /><span class="actualtext">Actual <span class="bluetext">Equities</span> PnL:&nbsp;</span><span id="day2equityactualvalue" class="actualtext"></span></p>
                            <p id="day2options"><span class="targettext"> <span class="bluetext">Options</span> Target PnL:&nbsp;</span><span id="day2optiontargetvalue" class="targettext"></span><br /><span class="actualtext">Actual <span class="bluetext">Options</span> PnL:&nbsp;</span><span id="day2optionactualvalue" class="actualtext"></span></p>
                            <p id="day2futures"><span class="targettext"> <span class="bluetext">Futures</span> Target PnL:&nbsp;</span><span id="day2futuretargetvalue" class="targettext"></span><br /><span class="actualtext">Actual <span class="bluetext">Futures</span> PnL:&nbsp;</span><span id="day2futureactualvalue" class="actualtext"></span></p>
                        </div>
                         <div id="day3" class="daytile">
                            <h3 id="day3date" class="dateh3" runat="server"></h3><br />
                            <p id="day3equities"><span class="targettext"> <span class="bluetext">Equities</span> Target PnL:&nbsp;</span><span id="day3equitytargetvalue" class="targettext"></span><br /><span class="actualtext">Actual <span class="bluetext">Equities</span> PnL:&nbsp;</span><span id="day3equityactualvalue" class="actualtext"></span></p>
                            <p id="day3options"><span class="targettext"> <span class="bluetext">Options</span> Target PnL:&nbsp;</span><span id="day3optiontargetvalue" class="targettext"></span><br /><span class="actualtext">Actual <span class="bluetext">Options</span> PnL:&nbsp;</span><span id="day3optionactualvalue" class="actualtext"></span></p>
                            <p id="day3futures"><span class="targettext"> <span class="bluetext">Futures</span> Target PnL:&nbsp;</span><span id="day3futuretargetvalue" class="targettext"></span><br /><span class="actualtext">Actual <span class="bluetext">Futures</span> PnL:&nbsp;</span><span id="day3futureactualvalue" class="actualtext"></span></p>
                        </div>
 </div>
 </div>
</fieldset>

 Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
    Dim firstdate As DateTime = FirstTradingDayInMonth(DateTime.Now.Date)
    Dim dayofweeknumber As String = firstdate.Day.ToString()
    Dim tradingdaysthismonth As New List(Of Date)
    Dim tempDate As Date = firstdate
    Do While tempDate.Day < LastTradingDayInMonth(DateTime.Now.Date).Day

        If tempDate = firstdate Then
            tradingdaysthismonth.Add(tempDate)
            tempDate = AddBusinessDays(tempDate)
        Else
            tempDate = AddBusinessDays(tempDate)
            tradingdaysthismonth.Add(tempDate)
        End If
    Loop
    Dim h3Tags As New List(Of HtmlGenericControl)

    Dim h3Tagname As String = "day" & dayofweeknumber & "date"


    Dim htmlcontrol As HtmlGenericControl = TryCast(Me.Page.FindControl("firstweekwrapper").FindControl(h3Tagname), HtmlGenericControl)
    htmlcontrol.InnerText = firstdate.ToLongDateString()
End Sub
4

3 回答 3

1

根据您发布的错误,您似乎正在将 c# 字符串连接与 vb 字符串连接混合。这:

Dim h3Tagname As String = "FeaturedContent_day" + dayofweeknumber + "date"

应该:

Dim h3Tagname As String = "FeaturedContent_day" & dayofweeknumber & "date"

这可能无法解决 NullReferenceException,但它至少是一个起点。

编辑

你需要找到你的ContentPlaceHolder第一个,这应该会有所帮助:

Dim cph As ContentPlaceHolder = TryCast(Master.FindControl("FeaturedContent"), ContentPlaceHolder)
Dim htmlcontrol As HtmlGenericControl = TryCast(cph.FindControl(h3Tagname), HtmlGenericControl)
于 2013-06-12T01:19:26.293 回答
1

好的,所以问题是 FindControl 没有渗透到命名容器中。换句话说,Me.Page.FindControl("firstweekwrapper") 不返回 DIV,因为 Me.Page 开始在母版页中搜索,当它到达内容占位符时,它停止查找;因此为空(无)。

试试这个:

Dim FeaturedContent As ContentPlaceHolder = TryCast(Page.Master.FindControl("FeaturedContent"), ContentPlaceHolder)

Dim h3Tagname As String = "day" & dayofweeknumber & "date"

Dim htmlcontrol As HtmlGenericControl = TryCast(FeaturedContent.FindControl("firstweekwrapper").FindControl(h3Tagname), HtmlGenericControl)`
于 2013-06-12T02:15:42.287 回答
0

第 29 行:

Dim mainControl As HtmlGenericControl = DirectCast(Page.FindControl("monthlywrapper")

在您的标记中,该 DIV 未设置为runat="server",因此服务器端无法“看到”它。

于 2013-06-12T00:45:46.517 回答