如何在asp.net中的标签中显示状态? [英] how to show status in a label in asp .net?

查看:67
本文介绍了如何在asp.net中的标签中显示状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好!我有一个文本框和一个标签。在这里我的查询是,每当我将立即开始在文本框中书写时,标签将显示消息此用户名不可用或此用户名可用(文本框数据将在数据库中搜索用户名)。我已经写了以下代码。但它只在我按下文本框中的Enter开关时才起作用。我希望不使用Enter开关就可以实现。



我已经为文本框启用了AutoPostBack = true。



源代码:

Hello! i have one textbox and a label. Here my query is that whenever i"ll start writing in the textbox immediately label will show a message "This username is unavailable" or "This username is available"(textbox data will search in database for username). I''ve written the following code. But it''s working only whenever I"m Pressing "Enter" switch in the textbox. I want that should happen without using of "Enter" switch.

I have enabled AutoPostBack=true for textbox.

Source Code:

<asp:TextBox ID="TextBox1" runat="server" AutoPostBack="True" CausesValidation="True" OnTextChanged="TextBox1_TextChanged"></asp:TextBox>
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>





C#代码:



C# Code:

protected void TextBox1_TextChanged(object sender, EventArgs e)
    {
        try
        {
            SqlCn.Open();
            SqlCommand SqlCmd = new SqlCommand("SELECT Manufacturer FROM MOBILE WHERE Manufacturer='" + TextBox1.Text + "'", SqlCn);
            SqlDataAdapter adapter = new SqlDataAdapter(SqlCmd);
            SqlDataReader SqlDr = SqlCmd.ExecuteReader();
            if (SqlDr.Read())
            {
                Label1.Text = "This username is unavailable.";
            }
            else
            {
                Label1.Text = "This username is available";
            }
        }
        catch (Exception ex)
        {
            string msg = ex.Message;
        }

推荐答案

首先在.ASPX中编写此代码。

First write this code in your .ASPX.
<div id="divMessage" runat="server" class="" style="display: none;">
       <asp:Literal runat="server" ID="ltlMessage" Text="test"></asp:Literal>
   </div>



然后只需将此代码写在您身后.CS页面。


Then just write this code behind you .CS page.

public static void ShowMessage(string _class, string msg, System.Web.UI.HtmlControls.HtmlGenericControl divMessage, Literal ltlMessage)
       {
           divMessage.Style.Add(&quot;display&quot;, &quot;block&quot;);
           divMessage.Attributes.Add(&quot;class&quot;, _class);
           ltlMessage.Text = msg;
       }</pre>




try
               {
                   UISettings.ShowMessage("alert alert-success", "Insert Successfully", divMessage, ltlMessage);
               }
               catch (Exception ex)
               {
                   ShowMessage("alert alert-error", ex.InnerException.Message, divMessage, ltlMessage);
               }


这篇关于如何在asp.net中的标签中显示状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆