检查是否在asp.net的形式存在的用户名

问题描述:

我目前创造了asp.net和c#登记表。我希望能够做的是让用户输入用户名,当他们从文本框中删除焦点将检查一个MySQL数据库来查看这个用户名是否存在。如果这样做则示出一个CSS容器以通知用户名已被拍摄的用户。

I am currently creating a registration form in asp.net and c#. What I want to be able to do is to allow the user to enter a username, when they remove focus from the text box it checks a mysql database to see if the username exists or not. If it does then a css container is shown to inform the user that the username has already been taken.

我曾尝试使用JavaScript / jQuery来隐藏这是工作的罚款CSS的容器。我的计划是在其上运行文本框TextChanged事件,然后如果这个函数返回true,那么将使用JavaScript / jQuery来重新显示的CSS容器。然而,我发现,使用的文本改变事件,我需要在球场上这也意味着形式刷新启用自动回发,并试图所以提交表单的工作停止我的计划。

I have tried to use Javascript/JQuery to hide the CSS container which is working fine. My plan was to run the textchanged event on the text box and then if this function returned true would then use javascript/jquery to reshow the css container. However, I found that to use the text changed event I needed to enable autopostback on the field which also meant that the form refreshed and tried to submit the form therefore stopping my plan from working.

修改
在回答有关sfomate使用更新面板。我已经使用下面的code段从形式尝试过这种方法

EDIT In response to sfomate about using the update panel. I have tried this method using the following code segment from the form

   <asp:ScriptManager ID="scriptManager" runat="server" />
    <asp:UpdatePanel runat="server" ID="userUpdate" UpdateMode="Conditional">
<Triggers>
    <asp:AsyncPostBackTrigger ControlID="txtUsername" EventName="TextChanged" />
  </Triggers>
   <ContentTemplate>
    <asp:TextBox id="txtUsername" TextMode="SingleLine" required 
          placeholder="Username" runat="server" 
         ontextchanged="txtUsername_TextChanged" AutoPostBack="True" />
         <span class="tooltip">Please enter your username</span>
     <span class="fieldExists">Username exists</span>
   </ContentTemplate>
</asp:UpdatePanel>

这是停止整个pagewhen文本框失去使用文本改变了事件的焦点自动回发。然而,没有任何被​​加入到与回复于()code我已经添加的页面。我曾尝试只是打印的Hello World或显示使用Javascript的CSS容器,但似乎没有任何文本框失去焦点后直接显示出来。然而,当我点击提交按钮的Response.Write()随后表示这不是我想要的。

This is stopping the auto post back of the whole pagewhen the text box loses focus using the text changed event. However, nothing is added to the page with the Response.Write() code I have added. I have tried to just print hello world or show the css container using Javascript but nothing seems to show up straight after the text box losing focus. However, when I click the submit button the response.write() is then shown which is not what I want.

感谢您的帮助。

您可以使用UpdatePanel的形式asp.net Ajax工具包,以prevent回发。或者您可以在使用jQuery AJAX和得到JSON响应。

You can use the Updatepanel form asp.net Ajax toolkit to prevent postback. Alternatively you can you AJAX with JQuery and get a JSON response.