在asp.net主页上通知图标更改范围文本

时间:2018-01-25 15:54:21

标签: c# sql asp.net

我有一个带有通知图标的母版页,它返回sql查询计数中的数字。我无法在页面加载时更改文本,只是保持在“7”。如何在pageload上更改该文本?以下是我的代码

 <li>
<table border="0" align="left" cellpadding="7" cellspacing="7" 
style="margin-top:20px;" ><tr>
 <td class="auto-style1">

 <div id="noti_Container">
 <asp:HyperLink ID="HyperLink1" runat="server" 
 NavigateUrl="../manage2.aspx"><i class="fa fa-envelope-o fa-fw"></i>
</asp:HyperLink>
<div class="noti_bubble"><asp:Label ID="Notifyme" runat="server"  Text="7"/> 
</asp:Label> </div>
</div>
</td>
<td class="auto-style1">
</li>

以下是Code Behind:

       SqlConnection CON = new SqlConnection(Testdb);
            {
                CON.Open();
                string ShiftTime = "SELECT count(discard) as alert FROM [Rejected].[dbo].[InQuestion] where discard = '0'";
                SqlCommand ShiftTimecalculate = new SqlCommand(ShiftTime, CON);
                SqlDataReader readershifttime = 
                ShiftTimecalculate.ExecuteReader();
                readershifttime.Read();
                if(readershifttime.hasrows)

      {
      Label noti_bubble = noti_Container.FindControl("Notifyme") as Label;
      Notifyme.innertext = readershifttime["alert"].ToString();
      readershifttime.Close();
      }

1 个答案:

答案 0 :(得分:1)

您的代码中的修改很少:

  1. 如果您想使用&#39; noti_container&#39;在代码中然后使用&#39; runat&#39;属性以解决错误。
  2. Notifyme.innertext:&#39; innertext&#39;不是asp:Label的属性。用户&#39;文字&#39;属性
  3. 避免使用代码行和不必要的对象创建
  4. 因此,您的代码将如下所示在母版页中:

        <li>
    <table border="0" align="left" cellpadding="7" cellspacing="7"
           style="margin-top:20px;">
        <tr>
            <td class="auto-style1">
    
                <div id="noti_Container" runat="server">
                    <asp:HyperLink ID="HyperLink1" runat="server"
                                   NavigateUrl="../manage2.aspx">
                        <i class="fa fa-envelope-o fa-fw"></i>
                    </asp:HyperLink>
                    <div class="noti_bubble">
                        <asp:Label ID="Notifyme" runat="server" Text="7" />
    
                    </div>
                </div>
            </td>
            <td class="auto-style1">
    </li>
    

    代码背后:

               SqlConnection CON = new SqlConnection(Testdb);
            {
                CON.Open();
                string ShiftTime = "SELECT count(discard) as alert FROM [Rejected].[dbo].[InQuestion] where discard = '0'";
                SqlCommand ShiftTimecalculate = new SqlCommand(ShiftTime, CON);
                SqlDataReader readershifttime =
                ShiftTimecalculate.ExecuteReader();
                readershifttime.Read();
                if (readershifttime.hasrows)
    
                {
                    Notifyme.Text = readershifttime["alert"].ToString();
                    readershifttime.Close();
                }
            }
    

    我希望这会有所帮助。