I have a logout menu using css in my project. Usually css menu only accepts link via
Now, what I want is the Logout menu will call function from codebehind.
My code for logout button:
<li><div class="some css class here"><a>Logout</a></div></li>
and the function I want to call:
protected void btnLogout_Click(object sender, EventArgs e)
{
FormsAuthentication.SignOut();
Session.Clear();
Session.Abandon();
Response.Redirect("~/login.aspx");
}
Any possible solutions for calling btnLogout_Click on my Logout menu? Thanks in advance..
Try this...
<li><div class="some css class here"><a id="atag" onserverclick="atag_ServerClick" runat="server">Logout</a></div></li>
In codebehind you have to call the anchor tag's function:
private void atag_ServerClick(object sender, System.EventArgs e)
{
atag.Target="url";
}
OR
As per your Requirement:
private void atag_ServerClick(object sender, System.EventArgs e)
{
FormsAuthentication.SignOut();
Session.Clear();
Session.Abandon();
Response.Redirect("~/login.aspx");
}
Hope this helps...
Instead of using anchor use LinkButton
<asp:LinkButton id="LinkButton1"
Text="Logout"
OnClick="atag_ServerClick"
runat="server"/>
and get your code work :)
In short just replace <a>Logout</a> with my code.
Related
I have a HyperLink given below,
<a href="www.abc.com/" id="click1" target="_blank" >Apply now</a>
I want to click the Link programatically on page load. can I do it?
Try with trigger on page load like
$(function(){
$('#click1').trigger('click');
});
Or even you can use simulate plugin and you can use like
$('#click1').simulate('click');
It triggers a native browser event.May be it also useful for you.
Or even simply you can try with
window.location.href = 'www.abc.com/';
Try this
$(document).ready(function(){
$("#click1").click(function(){
//your code
});
});
protected void Page_Load(object sender, EventArgs e)
{
click1.ServerClick += new EventHandler(ClearClick);
}
public void btnClear_Click(object sender, EventArgs e)
{
}
This will give u the anchor tag click on page load.
<a runat="server" id="click1">Apply now</a>
I have a news page in witch comments can be added. If i click a comment i go to a comment page where i can choose to delete the comment. If i delete the comment i want to go back to the news page and refresh the page so that the comments disapears.
I tryed adding this but then the remove function was not reached
<asp:Button runat="server" ID="btnRemoveComment" Text="Ta bort" OnClientClick="JavaScript: window.history.back(1); return false;" />
Commentpage.aspx
<asp:Button runat="server" ID="btnRemoveComment" />
Commentpage.aspx.cs
protected override void OnLoad(EventArgs e)
{
btnRemoveComment.Click += btnRemoveComment_Click;
}
private void btnRemoveComment_Click(object sender, EventArgs e)
{
CommentFactory.RemoveComment(CurrentPage);
}
a button can't apply two events, it's javascript or code-behind. Try to redirect in code-behind and use Ajax for dynamic update.
I don't think it is ok going back and forth between the news page and comments page. Try this
private void btnRemoveComment_Click(object sender, EventArgs e)
{
CommentFactory.RemoveComment(CurrentPage);
Response.Redirect("news.aspx");
}
New to asp.net, I am having a problem on a website I am creating, I am using a master page to build my pages. I am trying to change the css class of a li tag using the onclick event in linkbuttons:
<asp:LinkButton runat="server" id="AboutButton" OnClick="about_click" PostBackUrl="about.aspx"><span>About</span></asp:LinkButton>
This linkbutton calls a function in the master page's code behind:
protected void about_click(object sender, EventArgs e)
{
if(Page.IsPostBack)
{
about.Attributes.Add`enter code here`("class", "current");
}
}
This only works when the page is loaded and the button is clicked again. Any help would be greatly appreciated.
By adding: if(Page.IsPostBack) you're specifically telling it not to execute that code the first time the page is loaded, but you want it to happen when the page is first loaded, by the sounds of the question.
Why did you add if(Page.IsPostBack). Try this
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
about.Attributes.Add("class", "current"); //initial setting here
}
}
protected void about_click(object sender, EventArgs e)
{
about.Attributes.Add("class", "current");
}
I want to know if I could hide/show an asp user control when pressing a button in another user control on the same master page?
The Visible property will do this for you. The Click handler on your Button would be something like:
protected void ToggleButton_click(object sender, EventArgs e)
{
TargetControl.Visible = !TargetControl.Visible;
}
if you have got 2 users controls in same page . you can use this code :
in usercontrol 1
<asp:button Text="hide" runat="server" ID="B1" OnClick="HideOtherUserControl" />
and in code-behind
protected void HideOtherUserControl(object sender, EventArgs e)
{
Parent.FindControl("WebUserControl1").Visible = false;
}
I'm gonna post some more code to show exactly what I'm trying to do,
I'm adding the button using programming code and not markup but the OnClick won't work (giving the following error:
System.Web.UI.WebControls.Button.OnClick(System.EventArgs)' is inaccessible due to its protection level.
Button btnopslaan = new Button();
btnopslaan.Text = "Opslaan";
btnopslaan.ID = "btnOpslaan";
btnopslaan.CssClass = ".opslaan";
btnopslaan.Click += new EventHandler(btnopslaanClick);
btnopslaan_arr[btn_count] = btnopslaan;
add_button(btnopslaan);
protected void btnopslaanClick(object sender, EventArgs e)
{
Debug.WriteLine("success");
}
I just can't find out why this isn't working.
Anyone who can help me out?
You need to use OnClick for server side clicks rather than OnClientClick
Either you can use it inline >
<asp:Button id="btnopslaan" runat="server' OnClick="btnopslaanClick" />
Or in Code behind >
btnopslaan.Click+=new EventHandler(btnopslaanClick);
or you make it a postback call to the server. in your
aspx write:
<asp:Button runat="server" ID="buttonOpslaan" Text="opslaan" ></asp:Button>
codebehind write this:
protected void Page_Init(object sender, EventArgs e)
{
buttonOpslaan.Click += new EventHandler(buttonOpslaan_Click);
}
// mind: this method can be private
void buttonOpslaan_Click(object sender, EventArgs e)
{
//do something
}
or handle it with the AutoEventWireUp (recommended) like:
<asp:Button runat="server"
ID="buttonOpslaan"
OnClick="buttonOpslaan_Click"
Text="opslaan" ></asp:Button>
// mind: this method cannot be private, but has to be protected at least.
protected void buttonOpslaan_Click(object sender, EventArgs e)
{
//do something
}
or do it completely from code behind:
// note: buttonOpslaan must have an (autoassigned) ID.
protected void Page_Init(object sender, EventArgs e)
{
Button buttonOpslaan = new Button();
buttonOpslaan.Text = "opslaan!";
buttonOpslaan.Click += new EventHandler(buttonOpslaan_Click);
form1.Controls.Add(buttonOpslaan);
}
protected void buttonOpslaan_Click(object sender, EventArgs e)
{
//do something
}
or handle it clientside with javascript in your ASPX (it will not reach the server)
<script type="text/javascript">
function buttonOpslaan_Click(){
alert("test");
return false;
}
</script>
<asp:Button runat="server"
ID="buttonOpslaan"
OnClientClick="buttonOpslaan_Click()"
Text="opslaan" ></asp:Button>
Update: (by comments)
if you add the control via an eventhandler (like the onchange event of a dropdownlist), the control is 'lost' on next postback, or even as soon as the Page is send to the client (due to the stateless (there is no mechanism to maintain the state of application) behaviour and lifecycle of .Net).
So simply adding a control once is never going to work.
That means you have to rebuild the control every time a postback occurs. My preferred way to do this is store a list/document somewhere that descrbes what controls must be created each time. Possible locations are, from worse to good (IMHO):
Session
Viewstate
Cache
XML/IO
Database
After all, you are posting "data" to the server (that represents a control) and you want to save that for further use.
If the controls to be created aren't that complex you could implement a Factory Pattern like a WebControlFactory that stores only a few properties in a List or Dictionary, which is read every time to recreate the controls again (and again, and again, and again)
btnopslaanClick should be client side, in the .aspx itself have:
<script type="text/javascript">
function btnopslaanClick() {
alert("success");
}
</script>
btnopslaan.Click+=new EventHandler(btnopslaanClick);
protected void btnopslaanClick(object sender, EventArgs e)
{
Debug.WriteLine("succes");
}