本文介绍了如何在textchanged事件上阻止或禁用按钮的回发?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我的'UserName'字段,我正在检查存在的名称,如果存在则会在label.so中发出错误如何禁用按钮的点击事件或回发?



这是我的代码:



For my 'UserName' field i am checking the exist name, if exist then will throw an error in label.so how to disable button's click event or postback?

this is my code:

protected void txtUserName_TextChanged(object sender, EventArgs e)
        {
            try
            {
                string userName = txtUserName.Text;
                if (connection.State == ConnectionState.Closed)
                    connection.Open();

                command = new SqlCommand();
                command.CommandText = "Get_UserName";
                command.CommandType = CommandType.StoredProcedure;
                command.Parameters.AddWithValue("@userName", userName);
                command.Connection = connection;

                SqlDataReader reader = command.ExecuteReader();
                if (reader.HasRows)
                {
                    lblUserNameError.Text = "Alredy Exist";
                    lblUserNameError.Visible = true;
                    
                }
                else
                {
                    lblUserNameError.Visible = false;
                    btnSave.Enabled = false;
                }
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }  
            finally //Close db Connection if it is open....  
            {
                if (connection.State == ConnectionState.Open)
                    connection.Close();
            }
        }

推荐答案

if (reader.HasRows)
{
    lblUserNameError.Text = "Alredy Exist";
    lblUserNameError.Visible = true;
    btnSave.Enabled = false;
}
else
{
    lblUserNameError.Visible = false;
    
}

按钮应该被禁用,如果 UserName 存在。

Button should be disabled, if UserName exists.


<asp:updatepanel id="uptxtUserName" runat="server" xmlns:asp="#unknown"><contenttemplate>
                                <asp:textbox id="txtUserName" runat="server" tabindex="8" autopostback="true">
                                        ontextchanged="txtUserName_TextChanged"></asp:textbox>
                                        <asp:label id="lblUserNameError" runat="server" visible="false" forecolor="Red"></asp:label>
            <asp:requiredfieldvalidator id="reqUName" controltovalidate="txtUserName" errormessage="Required" class="error" runat="server" forecolor="Red">
            </asp:requiredfieldvalidator>
            </contenttemplate>
                <triggers>
                    <asp:asyncpostbacktrigger controlid="txtUserName" eventname="TextChanged" />
                    <asp:asyncpostbacktrigger controlid="btnSave" eventname="Click" />
                    <asp:asyncpostbacktrigger controlid="btnCancel" eventname="Click" />
                </triggers>
            </asp:updatepanel>
<pre lang="xml"><asp:UpdatePanel ID="upbtnSave" runat="server"><ContentTemplate>
                        <asp:Button ID="btnSave" Text="Save" runat="server" TabIndex="31" class="btn btn-success" onclick="btnSave_Click"></asp:Button>
                                <asp:Button ID="btnCancel" Text="Cancel" CausesValidation="false" runat="server" TabIndex="32" class="btn" onclick="btnCancel_Click"></asp:Button>
                                    </ContentTemplate>
                                        <Triggers>
                                            <asp:AsyncPostBackTrigger ControlID="btnSave" EventName="Click" />
                                            <asp:AsyncPostBackTrigger ControlID="btnCancel" EventName="Click" />
                                            <asp:AsyncPostBackTrigger ControlID="txtUserName" EventName="TextChanged" />
                                        </Triggers>
                                    </asp:UpdatePanel>





然后代码背后将如下工作





then code behind side will work like below

protected void txtUserName_TextChanged(object sender, EventArgs e)
        {
            try
            {
                string userName = txtUserName.Text;
                if (connection.State == ConnectionState.Closed)
                    connection.Open();

                command = new SqlCommand();
                command.CommandText = "Get_UserName";
                command.CommandType = CommandType.StoredProcedure;
                command.Parameters.AddWithValue("@userName", userName);
                command.Connection = connection;

                SqlDataReader reader = command.ExecuteReader();
                if (reader.HasRows)
                {
                    lblUserNameError.Text = "Alredy Exist";
                    lblUserNameError.Visible = true;
                    btnSave.OnClientClick = "return false;";
                }
                else
                {
                    lblUserNameError.Visible = false;
                    btnSave.OnClientClick = "return true;";
                }
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
            finally //Close db Connection if it is open....
            {
                if (connection.State == ConnectionState.Open)
                    connection.Close();
                connection.Close();
                command.Dispose();
            }
        }


这篇关于如何在textchanged事件上阻止或禁用按钮的回发?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 01:16