This is the html ,
<asp:LinkButton ID="hlnkLogoffF" runat="server" Text="Logoff" OnClick="hlnkLogoffF_Click" ></asp:LinkButton>
This is the code behind,
protected void hlnkLogoffF_Click(Object sender, EventArgs e)
{
//do something here
}
When i run this, i get the following error,
"The resource cannot be found. "
You have to use
if (!Page.IsPostBack)
{
// Your code here
}
in code behind of master page.
Also
What happens if you turn off validating with CausesValidation setting to false?
i tried to run your code
.aspx
<asp:LinkButton ID="hlnkLogoffF" runat="server" Text="Logoff" OnClick="hlnkLogoffF_Click" ></asp:LinkButton>
.Cs
protected void hlnkLogoffF_Click(object sender, EventArgs e)
{
}
i Haven't get any error.Could you please post the code inside the click event.?
Related
i have 3 pages
Login
Home
Add employeeNow i've a back button in "Add Employee" page which should redirect to the previous page! but when i click on it the required field validators will turn on!! how can i avoid that for back button? I tried using two types of code but both gets stuck in there for validation.
protected void Button2_Click(object sender, System.EventArgs e)
{
string prevPage = Request.UrlReferrer.ToString();
Response.Redirect(prevPage);
}
And this
protected void Button2_Click(object sender, EventArgs e)
{
Response.Redirect("home.aspx");
}
On aspx Page you need to set CausesValidation = false for back button. See below
<asp:Button ID="btnBack" runat="server" CausesValidation="false" Text="Back" />
By Adding CausesValidation = false. The Validation event will not fire for that button click and your redirect will work properly.
Hope that helps
I have an button in my aspx web page, which should be 'clicked' in my code behind c# code. The OnClientClick method then call a JS confirm dialog box and so the btn_Click function only get executed when user click 'OK'...
This is my code so far:
Variante 1
aspx:
<asp:Button runat="server" ID="btnConfirm" Text="Confirm" OnClick="btnConfirm_Click" />
aspx.cs:
protected void Page_Load(object sender, EventArgs e)
{
// set OnClientClick
btnConfirm.OnClientClick = "confirm('Save in DB?');";
// invoke method
Type t = typeof(System.Web.UI.WebControls.Button);
object[] p = new object[1];
p[0] = EventArgs.Empty;
MethodInfo m = t.GetMethod("OnClick", BindingFlags.NonPublic | BindingFlags.Instance);
m.Invoke(btnConfirm, p);
}
protected void btnConfirm_Click(object sender, EventArgs e)
{
//save something in database
}
Got code samples by: http://forums.asp.net/t/1046550.aspx?How+do+you+raise+a+button+click+event+from+outside+the+button+
I want to call the OnClientClick method, but when I replace 'OnClick' with 'OnClientClick' this error appear:
System.NullReferenceException was unhandled by user code
Message=Object reference not set to an instance of an object.
Edit:
Variante 2:
I tried to rewrite my program like this:
aspx:
<script language="javascript">
function invokeButtonClick() {
document.getElementById("btnConfirm").click();
}
function Validate() {
return confirm('Save in db?');
}
</script>
<asp:Button runat="server" ID="btnConfirm" Text="Confirm" OnClick="btnConfirm_Click" OnClientClick="return Validate();" />
aspx:cs:
protected void Page_Load(object sender, EventArgs e)
{
ScriptManager.RegisterStartupScript(this, GetType(), "InvokeButton", "invokeButtonClick();", true);
}
protected void btnConfirm_Click(object sender, EventArgs e)
{
// save something in database
}
But then the page postback before my btnConfirm_Click function get called...
Thanks,
Michael
If OnClientClick has a return value of true then the codebehind OnClick event will fire. If OnClientClick is returned false then it won't.
If OnClientClick doesn't return anything the codebehind event will fire regardless. So you want..
<asp:Button runat="server" ID="btnConfirm" Text="Confirm" OnClick="btnConfirm_Click" />
<script language="javascript">
function Validate() {
return confirm('Save in db?');
}
</script>
Codebehind:
protected void Page_Load(object sender, EventArgs e)
{
// set OnClientClick
btnConfirm.OnClientClick = "return Validate();";
}
protected void btnConfirm_Click(object sender, EventArgs e)
{
//save something in database
}
UPDATE:
This post just came to my attention, don't know why I didn't include it in my original post but you can use 'OnClientClick' in the aspx tag:
<asp:Button runat="server" ID="btnConfirm" OnClientClick="return Validate();" Text="Confirm" OnClick="btnConfirm_Click" />
<script language="javascript">
function Validate() {
return confirm('Save in db?');
}
</script>
You need to Add both the events on the button click. OnClientClick handles the Client side script code and OnClick handles the server side event calling.
So add both events and there handlers. The NullReferenceException you are getting because of that.
<asp:Button runat="server" ID="btnConfirm" Text="Confirm" OnClick="btnConfirm_Click" OnClientClick="return Validate();" />
and in your javascript code can be
function Validate(){
if (valied())
return true;
return false;
}
100% code behind confirmation! Sometiems its so annyoing to Code behind or Server side confirmation in asp.net when you want to Delete or Update something, I got its solution after a long search as below:
<asp:Button ID="btnSave" runat="server" Text="Save" OnClientClick="return confirm('Are you sure to BLOCK this customer ?')" OnClick="btnSave_Click" />
Now whenever you will click this button it 'll ask to confirm, if you 'll select "NO" page will not post and if you select "Yes" then page will post and your button click event will be fire... like below
Code behind:
protected void Page_Load(object sender, EventArgs e)
{
}
// Before coming here,,,there will be confirmation
protected void btnSave_Click(object sender, EventArgs e)
{
GetCustomerInfo();
}
In my .aspx page I have a 'input' html tag, also an asp button.
<input id="Name" type="text" runat="server" clientidmode="Static" />
<asp:Button Width="100" type="submit" ID="sendOrder" runat="server" OnClick="SubmitForm" Text="Submit" />
On page load, I am filling the value in input tag from code behind, like this:
Name.Value= "X";
But now if I change value of this text box from browser, lets say "Y", and click on Submit button, then I get the old value, but not the new one.
protected void SubmitForm(object sender, EventArgs e)
{
var test= Name.Value; // here I get old value
}
How can I get the altered value?
Make sure you are only setting the value to "X" when its not a postback:
if (!Page.IsPostBack){
Name.Value= "X";
}
Otherwise when clicking the submit button, the Page_Load() event will change the value from "Y" back to "X".
You need to use !IsPostBack on Page_Load shown as below:
protected void Page_Load(object sender, EventArgs e)
{
//it's important to use this, otherwise textbox old value overrides again
if (!IsPostBack)
{
Name.Value= "X";
}
}
Suggestion:
We can use use <input></input> control in asp.net but best practice is to use <asp:TextBox></asp:TextBox> control instead.
Here is the sample example:
HTML
<asp:TextBox ID="Name" runat="server"></asp:TextBox>
<asp:Button Width="100" ID="sendOrder" runat="server" OnClick="SubmitForm"
Text="Submit" />
Code-behind:
protected void Page_Load(object sender, EventArgs e)
{
//it's important to use this, otherwise textbox old value overrides again
if (!IsPostBack)
{
Name.Text = "Some Value";
}
}
protected void SubmitForm(object sender, EventArgs e)
{
var test = Name.Text; //now get new value here..
}
Check for IsPostback in Page_Load so you don't overwrite the values that are submitted!
You don't need all the else portion, just do this
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
//code to execute here only when an action is taken by the user
//and not affected by PostBack
}
//these codes should be affected by PostBack
}
I am not able to visible my button on another button click event.
.aspx
<asp:Button ID="btnActivate" runat="server" SkinID="skinLoginButton"
Text="Activate" ToolTip="Activate" CausesValidation="true"
ValidationGroup="UserAuthentication" onclick="btnActivate_Click" />
<asp:Button ID="btnhomepage" Visible="false" runat="server"
Text="Goto Homepage" CssClass="cssLoginButton" onclick="btnhomepage_Click"/>
.cs
#region btnActivate_Click
protected void btnActivate_Click(object sender, EventArgs e)
{
this.btnhomepage.Visible = true;
}
#endregion
I use this.btnhomepage.Visible = true; in .cs file.
what's wrong in my code or declearation?
<asp:Button ID="btnhomepage" Visible="false" runat="server"
Text="Goto Homepage" CssClass="cssLoginButton" onclick="btnhomepage_Click"/>
when using visible attribute in the mark-up you are forcing your control to be visible=false and stay false forever. asp.net engine render asp.net controls into html control in asp.net page life cycle at Render stage. even you had changed the control property in any code behind event
Solution: Don't use makup attribute when setting control behaviour dynamicllay
page life cycle link:
http://msdn.microsoft.com/en-us/library/ms178472(v=vs.100).aspx
http://www.codeproject.com/Articles/73728/ASP-NET-Application-and-Page-Life-Cycle
Remove the visible property from the btnhomepage button and make it invisible from Page_Load
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
this.btnhomepage.Visible = false;
}
}
Try this
btnhomepage.Visible = true;
btnhomepage.Enabled = true;
btnhomepage.Style.Add("display", "block");
this seemed simple at first but I can't get it to work.
I have the following scenario:
<asp:ListView ID="CommentsListView" runat="server">
<LayoutTemplate>
<asp:PlaceHolder ID="itemPlaceholder" runat="server" />
</LayoutTemplate>
<ItemTemplate>
<UC:Comment runat="server" CommentItem="<%# CurrentComment %>" />
<br />
</ItemTemplate>
</asp:ListView>
<asp:TextBox ID="NewComment" runat="server" />
<asp:ImageButton ImageUrl="/images/ball.png" runat="server"
OnClick="SubmitComment" />
Code:
protected void Page_Load(object sender, EventArgs e)
{
RenderListView();
}
protected void RenderListView()
{
CommentsListView.DataSource = //Get the data source objects
CommentsListView.DataBind();
}
protected CommentObject CurrentComment
{
get { return (CommentObject)Page.GetDataItem(); }
}
protected void SubmitComment(object sender, ImageClickEventArgs e)
{
//code to submit comment
RenderListView();
}
basically, when I submit a comment, I want to see it in the ListView, but I don't. "MyControl" gets a null comment in the post-back, for all of the items (not just the new one).
Only after I refresh the page, I can see the new comment that I'v submitted. I can't however refresh the page every submit because this code is inside an UpdatePanel (the issue occurs without the UpdatePanel as well).
Any idea how to solve this?
I can't find any specifics on this, but I have a hunch the user control in your ItemTemplate is causing the issue. Can you remove it and see if it works?
I notice that you are calling RenderListView in both SubmitComment and PageLoad which I believe will cause it to fire twice when the button is clicked (with PageLoad firing first). It looks like the code you posted is simplified. Is it possible that there is something happening in the PageLoad which is sabotaging your SubmitComment steps?
I finally solved it, if anyone else may encounter this -
The solution was to avoid "<%#" and instead use the ItemDataBound event:
<asp:ListView ID="Comments" runat="server"
OnItemDataBound="CommentsItemDataBound">
and the method itself:
protected void CommentsItemDataBound(object sender, ListViewItemEventArgs e)
{
var commentItem = (Comment)(((ListViewDataItem)e.Item).DataItem);
var commentControl = (Comment)e.Item.FindControl("CommentControl");
commentControl.CommentItem = commentItem;
}
This way, the binding of each control works as expected.