while implementing Logout event Page validation events were raising in My form and It stops Firing Logout Method
Need to stop page validation events when I call Logout Method using on ServerClick()
<li><span>Logout</span></li>
My Code :
protected void logout_ServerClick(object sender, EventArgs e)
{
FormsAuthentication.SignOut();
Session.Abandon();
Session.Clear();
Session.RemoveAll();
Response.Redirect("Login.aspx");
}
use CausesValidation="False"
<asp:LinkButton id="logout" runat="server" Text="logout" CausesValidation="False"> </asp:LinkButton>
Related
I have first this
<li><asp:HyperLink id="logoutBtn" NavigateUrl="login.aspx" Text="Logout" runat="server"><i class="fa fa-sign-out fa-fw"></i> Logout </asp:HyperLink></li>
But I think it goes first to the page before he runs the back code which is this.
public void logoutBtn_Click(object sender, EventArgs e)
{
Session.Clear();
Response.Redirect("../login.aspx");
}
So basically I can still access any site because the Session is not cleared.
How can I achieve that whenever I click the HyperLink the code is fired?
The purpose of a hyperlink is to navigate the user to another destination. (Plus, you don't have a click eventhandler attached to you hyper link, that will run your code behind.)
If you want to run server-side code use a good old button:
<asp:Button ID="logoutBtn" runat="server" onclick="logoutBtn_Click" Text="Logout" />
public void logoutBtn_Click(object sender, EventArgs e)
{
Session.Clear();
Response.Redirect("../login.aspx");
}
If you want to use an asp:Hyperlink control to logout the user, navigate him to a specific logout page:
<li><asp:HyperLink id="logoutBtn" NavigateUrl="logout.aspx" Text="Logout" runat="server"><i class="fa fa-sign-out fa-fw"></i> Logout </asp:HyperLink></li>
Create the logout.aspx page and in the Page_Load event, log the user out:
protected void Page_Load(object sender, EventArgs e)
{
Session.Clear();
Response.Redirect("../login.aspx");
}
I created my own site and my own Facebook page.
I add follow me button on site.
in that user have to follow me either on facebook or on twitter.
Problem: How to navigate user to first login page and then after successfull login to my facebook page
Place image and navigate to login page on image click like following:-
<asp:Button ID="imgbtnFaceBook" CausesValidation="False" runat="server" OnClick="imgbtnFaceBook_Click"/>
protected void imgbtnFaceBook_Click(object sender, EventArgs e)
{
Response.Redirect("yourLoginPageLink");
}
On login page check credentials and redirect to facebook page like following:-
protected void btnLogin_Click(object sender, EventArgs e)
{
// check login Credentials
Response.Redirect("yourFaceBookPageLink");
}
<asp:Button ID="btnLogin" runat="server" Text="Login" OnClick="btnLogin_Click" />
<asp:Button ID="imgbtnFaceBook" CausesValidation="False" runat="server" OnClick="imgbtnFaceBook_Click"/>
protected void imgbtnFaceBook_Click(object sender, EventArgs e)
{
Response.Redirect("https://www.facebook.com/yourPageLink");
}
Here is my DataGridView
<asp:GridView ID="gvPredstave" runat="server" CssClass="gridview"
AlternatingRowStyle-CssClass="even" AutoGenerateColumns="True">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btnRezervisi" runat="server" Text="Rezervisi" onclick="Button1_Click" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
This is my code for click on button in GridView
protected void Button1_Click(object sender, EventArgs e)
{
Response.Redirect("AktivneRezervacije.aspx?korisnicko_ime=" + korisnicko_ime);
conn.Close();
}
When i click on button i got this error in browser:
Invalid postback or callback argument. Event validation is enabled using <pages enableEventValidation="true"/> in configuration or <%# Page EnableEventValidation="true" %> in a page. For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them. If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation.
Does anybody know the reason why is this happening.
I tried with EnableEventValidation="false" but it doesn't work.
I recreated the error using your gridView and binding it. The answer lies on the Page_Load event. If you have it like this:
protected void Page_Load(object sender, EventArgs e)
{
bindGridView(); //code to bind the GridView
}
You will get the exception. Change it to:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bindGridView();
}
}
and you should not get the error anymore.
if the page is in postback,
you have two ways to make it work
either add a postback event on your gridview
or control in on page load.
Regarding to the conn.close();
close first the connection after redirecting it to another page.
happy coding.
I have a ASP.NET page with 1 user controls registered.
i have one server control
<asp:LinkButton ID="vidoUpdatebtn" OnClick="vidoUpdatebtn_Click" runat="server">LinkButton</asp:LinkButton>
in .cs i handle
protected void vidoUpdatebtn_Click(object sender, EventArgs e)
{
//do something
ScriptManager.RegisterStartupScript(this, this.GetType(), "myscript", "doSomeThing()", true);
}
in user control i have function doSomeThing()
function doSomeThing() {
alert("TestReady");
}
when i click on LinckButon registred function dosen't work
Write this in PageLoad (the only modifications from what you were writing are the moment in time of the registration -- PageLoad not Click, Click is too late and the face that you must write the actual implementation of the javascript function here):
protected void Page_Load(object sender, EventArgs e) {
ScriptManager.RegisterStartupScript(this, this.GetType(), "myscript",
#"function doSomeThing() {
alert(""TestReady"");
}"
, true);
}
and this in the asp:LinkButton tag (you must specify the OnClientClick attribute):
<asp:LinkButton ID="vidoUpdatebtn" runat="server" OnClick="vidoUpdatebtn_Click" OnClientClick="doSomeThing()" >LinkButton</asp:LinkButton>
And it should work.
Have you tried this way:
Add Script manager on your aspx page
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true" />
Codebehind:
Page.ClientScript.RegisterStartupScript(this.GetType(),"myscript", "doSomeThing()",true);
If you want client side code to run on the button click, you could use the OnClickClick property instead of the OnClick event. That'll also avoid the need for a postback and will give immediate feedback to the user.
e.g.
<asp:LinkButton ID="vidoUpdatebtn" OnClientClick="alert('TestReady'); return false;" runat="server">LinkButton</asp:LinkButton>
I have multiple user controls on a page and each of them have multiple linkbuttons that perform the same logic on the server side. Is there a way for all the linkbuttons to have the same event handler that is defined in the code behind of the page?
If needed, I can change the linkbuttons to be any other HTML or ASP.NET control as long as it can support a clickable image.
Inside your usercontrol create an event and wire it up. Then call it from the linkbuttons OnClick event handler:
UserControl.ascx:
<asp:LinkButton
runat="server"
id="linkbutton1"
OnClick="LinkButtonClicked">
Click Me
</asp:LinkButton>
<asp:LinkButton
runat="server"
id="linkbutton2"
OnClick="LinkButtonClicked">
Click Me Again
</asp:LinkButton>
UserControl.ascx.cs
public event EventHandler UserControlLinkClick;
protected void LinkButtonClicked(object sender, EventArgs e)
{
if (this.UserControlLinkClick!= null)
{
this.UserControlLinkClick(this, e);
}
}
Inside your parent page wire up the user controls UserControlLinkClick:
protected override void OnInit(EventArgs e)
{
MyUserControl uc = LoadControl("~/PathToUserControl.ascx");
uc.UserControlLinkClick += new EventHandler(MyUserControl_UserControlLinkClick);
}
protected void MyUserControl_UserControlLinkClick(object sender, EventArgs e)
{
//this code will execute when the usercontrol's LinkButtonClicked event is fired.
}
Hope that helps!!
Create an Event/Delegate in your custom control. Then in your page code behind, you can add event handlers or subscribe methods to the delegate.
Link for Events
Link for Delegates
Hope this helps