找到h3标签控件并设置内部文本

时间:2013-06-12 00:33:25

标签: asp.net

标题标记标记如下所示,它在页面源中生成一个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

3 个答案:

答案 0 :(得分: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)

答案 1 :(得分: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)`

答案 2 :(得分:0)

第29行:

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

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