1
I have the following situation: on my page . aspx I have an Asp.Button that, after running an Onclick event on Behind, runs a Postbackurl to another page carrying information through Hiddenfields.
What I am trying to do is: when trying to register a client, if the email entered is already registered in the database, the button does not run the Postbackurl or change its URL so that the page does not change. Otherwise, run normally and proceed to the profile.
My button on the front :
<asp:Button ID="continuebtn" OnClick="Continuebtn_Click" runat="server" PostBackUrl="~/cliente/perfil.aspx" CssClass="btn btn-success btn-sm" Text="Continuar"/>
And the Behind code looks like this (briefly) :
protected void Continuebtn_Click(object sender, EventArgs e)
{
using(MySqlConnection conn = new MySqlConnection(conexao))
{
try
{
string consulta = "SELECT * FROM tb_cliente WHERE Email = @email"
MySqlCommand cmd = new MySqlCommand(consulta,conn);
cmd.Parameter.AddWithValue("@email",clienteEmail);
MySqlDataReader reader = cmd.ExecuteReader();
if(reader.Read())//caso exista um cliente cadastrado com o email inserido
//altera o PostBackUrl para se manter na mesma página
else//se o email inserido for novo
//mantém o PostBackUrl do botão
}
catch (MySqlException ex)
{
EscreveLog(ex.Message);
}
finnaly
{
conn.Close();
reader.Close();
}
}
}
So far I’ve tried :
continuebtn.PostBackUrl = "";
continuebtn.Attributes["PostBackUrl"] = "return false";
continuebtn.Attributes.Remove("OnClick");
continuebtn.Attributes.Add("OnClick","return false");
continuebtn.OnClientClick = "return false;";
colocar só um return no else
And no alternative worked, all continue to run Postback with the url "~/client/profile.aspx"
I hope I’ve been clear on the explanation, any help is welcome.
Any validation component does this for you, only this snippet of code can’t help, could put a minimal example?
– novic
I changed the Behind code.
– Fabio Hagiwara
I think you’re doing it wrong.
– novic
Could be more specific?
– Fabio Hagiwara
you mess with aspnet Forms if you validate the screen do not need to do anything on the button.
– novic
I don’t think you understand what I’m asking.
– Fabio Hagiwara
I think you don’t know what you’re doing, the difference is there: I know how to do what you want to do, just validate the screen with the validation component.
– novic
First thing: tell us what you’re doing? Don’t just ask how, but, what do you intend to do?
– novic
It’s in the second paragraph, I edited for better understanding.
– Fabio Hagiwara
You click the button: and the same checks whether or not the email exists, if it does not exist it proceeds to another page? (if it does not return/stays in the same?)?
– novic
This, if it already exists I want the application to remain on the same page, ignoring the Postbackurl button.
– Fabio Hagiwara
you want an asynchronous process, and actually an asynchronous validation
– novic
you can do this: click on the button in the code checks the email if yes a redirect does not send a message and remove that address from there and put in the code
PostBackUrl="~/cliente/perfil.aspx"
– novic
Let’s go continue this discussion in chat.
– Fabio Hagiwara
After a long time researching a Stackoverflow user in English responded my post there. It is not possible to change the Postbackurl path after the button is clicked, its property only changes after the automated postback of Asp.Button. The recommended output is to use Server.Transfer, which also maintains the information as a normal cross-page.
– Fabio Hagiwara
But ueh, I said this to you Fabio
– novic