Using OnClick event with PostBackUrl attribute of asp:Button - c#

I have an asp:Button control like this;
<asp:Button ID="btnPayCC" CssClass="paymentButton" runat="server" Text="ONAYLA"
OnClientClick="if(!doPostBack()) return false; frmMaster.target='_blank'"
PostBackUrl="http://www.google.com" OnClick="btnPayCC_Click" />
And doPostBack javascript function is;
function doPostBack()
{
__doPostBack('<%= btnPayCC.ClientID %>', '');
return true;
};
When I clicked the button, then it opens new page(google.com) as I wish,
But do not postback into button control's OnClick event.
What is the problem?
How can I solve it?

I had the same problem. I solved it by adding a Response.Redirect("url") at the end of my onClick event for the Button and removing the PostbackUrl attribute from the button so that is uses autopostback to the same url. IE:
protected void Button_Click(object sender, EventArgs e)
{
//do your button click events here
Response.Redirect("~/url.aspx");
}
and
<asp:Button ID="Button" runat="server" CausesValidation="false" OnClick="Button_Click">

Related

Avoid page refresh click on Button Asp.net c#

enter code here NET application, I have inserted a button that call a Javascript function (OnClick event) and a asp.net function (OnClick event)
The problem is that when I click the button, it refreshes the page.
How can I avoid the page refreshes click on asp button using javascript?
document.getElementById('pageurl').innerHTML = "tryfblike.aspx";
$('#<%= btnsave.ClientID %>').click();
$('#auth-loggedout').hide();
<asp:Button runat="server" ID="btnsave" OnClick="btnsave_Click();" Visible="true" style="display: none;" />
protected void btnsave_Click(object sender, EventArgs e)
{
objda.agentid = "2";
string currenttime = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
objda.datetime = currenttime;
ds = objda.tbl_log();
}
First to call JavaScript function, use OnClientClick.
To avoid page refresh, after your function call add return false;
For example:
<asp:Button ID="btnSubmit" runat="Server" OnClientClick="btnsave_Click(); return false;" />
in java script you can use return false.
if you want prevent whole page referesh use ajax.
FirstYou can call javascrip onclick and then simply add
return false;

In aspx how to trigger button click event when press "Enter" on a textbox while the button click event defined in source cs file

While, I want to trigger btnSearchSuiteGroup_Click event when pressing "enter" on txtSuiteGroupName which described in aspx, code below:
<asp:TextBox ID="txtSuiteGroupName" runat="server" clientidmode="Static" CssClass="DD" onkeypress="return searchKeyPress(event)"></asp:TextBox>
<asp:Button ID="btnSearchSuiteGroup" runat="server" Text="Search" CssClass="DD" Width="64px" onclick="btnSearchSuiteGroup_Click" />
<script type="text/javascript">
function searchKeyPress(e) {
// look for window.event in case event isn't passed in
if (typeof e == 'undefined' && window.event) { e = window.event; }
if (e.keyCode == 13) {
document.getElementById('<%=btnSearchSuiteGroup.ClientID%>').click();
}
}
</script>
While, the btnSearchSuiteGroup_Click is defined in the source cs file:
protected void btnSearchSuiteGroup_Click(object sender, EventArgs e)
{
this.LinqDataSource1.WhereParameters["SuiteGroupName"].DefaultValue = this.txtSuiteGroupName.Text;
this.GridView1.DataBind();
if (GridView1.Rows.Count == 0)
Response.Write("<script language='javascript'>window.alert('No record found!')</script>");
}
When I browse the website, the keypress on the textbox cannot initiate the button click event, anything wrong in the code?
If you use Panel you would not need to use any javascript function. You can specify default button Id for panel like below
<asp:Panel runat="server" DefaultButton="btnSearchSuiteGroup">
<asp:TextBox ID="txtSuiteGroupName" runat="server" clientidmode="Static" CssClass="DD">
</asp:TextBox>
<asp:Button ID="btnSearchSuiteGroup" runat="server" Text="Search" CssClass="DD" Width="64px" onclick="btnSearchSuiteGroup_Click" />
</asp:Button>
</asp:Panel>
OfCourse you can have Multiple panels on single page for assigning different default buttons for separate panels!
For More On Panel.DefaultButton Property
I suggest the following method:
1. Create a function 'ButtonClick' and put all the code in 'btnSearchSuiteGroup_Click' function into it.
2. Add 'onkeypress' event for the textbox 'txtSuiteGroupName' by looking at this thread
3. Whenever the above event is fired, see if the entered key is 'Enter'.
4. If the key is 'Enter' key, call the function 'ButtonClick'.

Autopostback prevents button click

I have an asp.net page and on it there are a number of controls, including:
A textbox with autopostback = true and the server side textchanged event implemented
A button with the server side click event implemented
The textbox performs the postback as soon as the user leaves the control (ie, focus is lost). The problem is that if the user happens to change the value and then press the button without leaving the textbox, then on button click the textbox will perform the postback but the button click will be lost.
How can i force both the textbox and the button events to fire consecutively in such cases?
Thanks
Try:
ASPX:
<asp:TextBox ID="TextBox1" clientidmode="Static" runat="server" onkeypress="return EnterEvent(event)"></asp:TextBox>
<asp:Button ID="Button1" runat="server" style="display:none" Text="Button" />
JS:
function EnterEvent(e) {
if (e.keyCode == 13) {
__doPostBack('<%=Button1.UniqueId%>', "");
}
}
CS:
protected void Button1_Click1(object sender, EventArgs e)
{
}
You can delay postback from textbox and cancel it in case of button click. To do this add code below to Page_PreRender method:
protected void Page_PreRender(object sender, EventArgs e)
{
Button1.OnClientClick = string.Format("if(window.{0}Timeout){{ clearTimeout(window.{0}Timeout); }}",
TextBox1.ClientID);
TextBox1.Attributes["onChange"] = string.Format("javascript:window.{0}Timeout = setTimeout(\"{1}\", 500); return;",
TextBox1.ClientID,
ClientScript.GetPostBackEventReference(TextBox1, ""));
}

How to validate a form in ASP.NET using an image button, before raising the onClick event?

This may be a silly question but i'm a beginer here..
I created a form in asp.net and it's all fields are validated using validation controls on clicking an image button.It is working properly.After that i add an event to the button.This is the code of that button.
<asp:ImageButton ID="Submit" runat="server" OnClick="submit_Clicked" ValidationGroup="AddSchoolValidationGroup" CausesValidation="true" ImageUrl="~/Styles/images/submit-btn.png" OnClientClick="submitClicked" />
The purpose of that event is for submitting all the values in the form to database.Here my problem is,After adding the onClick event to button, validation has not taken place.And i want these onClick event to raise only when the form is validated successfully.Can any one guide me ..Thanks in Advance.
make sure that you are using the same validation group for all the validation controls.
in addition you can use OnClientClick event to call validation.
<asp:ImageButton ID="Submit" runat="server" OnClick="submit_Clicked" ValidationGroup="AddSchoolValidationGroup" CausesValidation="true" ImageUrl="~/Styles/images/submit-btn.png" OnClientClick="return Page_ClientValidate('AddSchoolValidationGroup');" />
You need to validate the page before submitting. Please add attribute for button.
protected void Page_Load(object sender, EventArgs e)
{
Submit.Attributes.Add("onclick", "javascript:if(Page_ClientValidate('AddSchoolValidationGroup')){return true;}else{return false;}");
}
Since you have a method "submitClicked" in onclient click, you can call the same inside Page_ClientValidate and remove the existing "OnClientClick" property.
protected void Page_Load(object sender, EventArgs e)
{
Submit.Attributes.Add("onclick", "javascript:if(Page_ClientValidate('AddSchoolValidationGroup')){submitClicked(); return true;}else{return false;}");
}
You can use javascript function
function validateForm() {
if (document.getElementById('txtName').value == '')
{
ValidatorEnable(document.getElementById("rfvName"), true) // rfvName is id of validator
return false; }
}
and call that function on client click of image button
<asp:ImageButton ID="Submit" runat="server" OnClientClick="validateForm();" OnClick="submit_Clicked" ValidationGroup="AddSchoolValidationGroup" CausesValidation="true" ImageUrl="~/Styles/images/submit-btn.png" />
Replace OnClientClick="submitClicked" with OnClientClick="return submitClicked();"
function submitClicked()
{
if (document.getElementById('txtname') != '') {
return true;
}
else {
alert('Please Enter Name');
return false;
}
}
So until all the validations are validated, the Server side event will not be called.

JavaScript form submit doesn't fire asp server-side click function of the submit button

I used an event listener to prevent the action of my asp:button (submit) because I wanted to validate my form with a javascript function before it actually gets submitted.
formInstance.addEventListener('submit', function (event) {
event.preventDefault();
}, false);
Here is the button:
<asp:Button ID="btnCreateForm" CssClass="submit-form button" runat="server"
Text="Save Form" OnClick="btnCreateForm_Click"
OnClientClick="Sharpforms.checkFormEntry()" />
The fired javascript:
checkFormEntry: function () {
var formName = document.getElementById("txtFormName");
if (formName.value.trim() == "") {
alert("Please fill in a valid form name!");
return false;
}
else {
//formInstance.submit();
return true;
}
}
Apparently the javascript submit() does submit the form because the page is being reloaded but I recognized that it doesn't enter my ASP side btnCreateForm_Click function any more:
protected void btnCreateForm_Click(object sender, EventArgs e)
{
System.Diagnostics.Debug.WriteLine("Test if the button has been clicked.");
}
When commenting my form event listener as well as the submit() and try to submit it natively he enters the click function without problems. But then I have no possibility to check my form on the client side. What am I missing?
You have to add return to the OnClientClick event. If the function returns true, the postback occurs. If the function returns false, the postback gets cancelled.
<asp:Button ID="btnCreateForm" CssClass="submit-form button" runat="server"
Text="Save Form" OnClick="btnCreateForm_Click"
OnClientClick="return Sharpforms.checkFormEntry()" />
Also, doing it in this way you can remove the event listener.

Categories

Resources