在page_load时验证访问数据库中的用户名是否存在

时间:2015-07-22 10:54:52

标签: c# asp.net ms-access username pageload

我有一个标签来识别Windows登录用户。

<table>
        <tr><td><b>Utilizador:</b></td><td><asp:Label ID="username" runat="server"></asp:Label></td></tr>
</table> 

页面加载时我的代码落后

protected void Page_Load(object sender, EventArgs e)
{
    username.Text = "[" + HttpContext.Current.User.Identity.Name + "]"; 
}

如何警告(message.box或label)用户他已经存在于访问数据库中?

(我已经插入代码)

1 个答案:

答案 0 :(得分:0)

如何简单地添加标签或验证器以显示用户已存在的消息。在后面的代码中,您可以在用户已存在时简单地显示消息。像这样:

<table>
  <tr>
    <td>
      <b>Utilizador:</b>
    </td>
    <td>
      <asp:Label ID="username" runat="server"></asp:Label>
    </td>
  </tr>
  <tr id="userAlreadyExistsRow" runat="server">
    <td colspan="2">
      <asp:Label ID="userAlreadyExistsLbl" runat="server" 
         Text="This user exists already" </asp:Label>
    <td>
  </tr>
</table>

protected void Page_Load(object sender, EventArgs e)
{
    username.Text = "[" + HttpContext.Current.User.Identity.Name + "]"; 

    var userAlreadyExists = true; // replace by appropriate condition
    userAlreadyExistsRow.Visible = userAlreadyExists;
}