Click here to Skip to main content
15,886,055 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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();
            }
        }
Posted
Comments
Abhinav S 9-Apr-15 3:20am    
btnSave.Enabled = false; is already there.
Are you sure your code is hitting the else part?
Dawood Abbas 9-Apr-15 3:25am    
yeah but its not working..
I tried alredy
Sinisa Hajnal 9-Apr-15 3:23am    
You should have btnSave.Enabled in if (reader.HasRows) not in else...as-is, you're disabling the button if the user in NOT found :)
Dawood Abbas 9-Apr-15 3:29am    
yeah i did that alredy,its not working

I think you should write that Enabled=false in else part.
C#
if (reader.HasRows)
{
    lblUserNameError.Text = "Alredy Exist";
    lblUserNameError.Visible = true;
    btnSave.Enabled = false;
}
else
{
    lblUserNameError.Visible = false;
    btnSave.Enabled = false;
}
Button should be disabled, if UserName exists.
 
Share this answer
 
Comments
Dawood Abbas 9-Apr-15 3:30am    
yes I did like that only, but its still going to postback.
Sinisa Hajnal 9-Apr-15 3:55am    
That is impossible. Disabled button cannot be clicked, therefore it cannot cause postback. Either you have some javascript that invokes form submit or you're talking about something else and didn't explain well.

What do you mean by IT (what is it) is still going to postback? - textchanged event will trigger the postback everytime text changes...

NOTE: you should dispose of the command object along with the connection in finally block.
It should. You might have some other issues.
Dawood Abbas 9-Apr-15 8:18am    
I not have any scripting which invoke it, and I need to disable in textbox change when data is alredy exist.I disposed now.
You wanted to disable TextChanged, but you mentioned about Button's disability.
your textbox is inside an UpdatePanel also the Button has to be inside an UpdatePanel or else it won't be refreshed.

<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

C#
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();
            }
        }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900