jQuery Submit Heisenbug - c#

I have a heisenbug - it works when I put a Firebug breakpoint on it, but it doesn't work without the breakpoint.
The LoginSubmit function is called from two places in my code. In one instance, the btnLoginSubmit_Click event is fired successfully, but in the other instance, the event doesn't fire unless I put a breakpoint on the $submitButton.click(); line.
function LoginSubmit() {
$("#diagnostics").text("");
var $submitButton = $("#<%=btnLoginSubmit.ClientID %>");
$submitButton.click(); // <-- This is the line of code that is not executing.
$("#diagnostics").text("LoginSubmit failed to submit postback. ");
}
<asp:Button ID="btnLoginSubmit" runat="server" Text="Submit" CssClass="hiddenButton"
OnClick="btnLoginSubmit_Click" OnClientClick="return true;" Visible="true" />
Code behind page:
protected void btnLoginSubmit_Click(object sender, EventArgs e)
{
....
}

Consider registering a PostBackEventHandler instead of firing the onclick of the button
public partial class ScheduleEdit : System.Web.UI.Page,
System.Web.UI.IPostBackEventHandler
{
protected void Page_Init(object sender, EventArgs e)
{
var postBack = Page.ClientScript.GetPostBackEventReference(this, null);
var script = "function postback() { " + postBack + "; }";
Page.ClientScript.RegisterStartupScript(GetType(), this.UniqueID, script, true);
}
// this is called via the client side 'postback' script
public void RaisePostBackEvent(string eventArgument)
{
DoStuff();
}
}
// client side script:
$("#DP").datepicker({
onSelect: function (dateText, inst) { postback(); },
});

The issue involved a problem in jQuery involving dialogue boxes. When I closed the dialogue box before issuing the submit, it worked.

Related

Tekerik radwindow open and close issue

With the click event of a link button calling a server side method from Form1
Form1 aspx.cs page code:-
protected void lnkTakeAction_OnCommand(object sender, CommandEventArgs e)
{
if (e.CommandName == "duplicate")
{
//Some operations conditional
//If condition true
//call javascript method to call a javascript function to open a rad window
ScriptManager.RegisterStartupScript(this, typeof(System.Web.UI.Page), "ABC "ABCPopUp" + ID + "','Tag');", true);
}
}
Form1 aspx javascript code:-
function ABCPopUp(ID, Tag) {
var oWindow = window.radopen('XYZPage.aspx?ID=' + scoreMethodId + '&Tag=' + mode, 'rdWindowMapping');
oWindow.SetSize(600, 500);
oWindow.Center();
var titleBar = oWindow.GetTitlebar();
$telerik.$(titleBar).parent().hide();
return false;
}
Issue facing - The XYZPage is not opening.The same functionality works if I call the function ABCPopUp from javascript.But when I call from server side the form is not opening.Please give expert advice
Most likely, the script runs too early. You have two options:
use AJAX for the grid and do not include the RadWindow in the response
OR, use the Sys.Application.Load event to call your function: http://www.telerik.com/help/aspnet-ajax/window-troubleshooting-opening-from-server.html.

Call jquery button click event from code behind

I am using the below code to display a div on button click event. Is there any way to call this button click event from C# code behind.
$(document).on("click", '#AddNew', function (e) {
e.stopPropagation();
if ($NewEntry.is(":hidden")) {
$NewEntry.slideDown("slow", "easeOutBounce");
$NewEntry.slideDown("slow", "easeOutBounce");
$filter.slideUp("slow");
return false;
} else {
$NewEntry.slideUp("slow");
return false;
}
});
use this code :
protected void myButton(object sender, EventArgs e)
{
ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "tmp", "<script type='text/javascript'>myFunction(params...);</script>", false);
}
more read : Call jQuery function from ASP.NET code behind C#

How to enable a disabled button in code behind in asp.net

I have disabled a button in javascript. On page load I need to enable it again.
This is my code in script.
$(document).ready(
function () {
document.getElementById("<%=btnsubmit.ClientID%>").disabled = true;
});
In code behind,I am enabling it
protected void Page_Load(object sender, EventArgs e)
{
btnsubmit.Enabled = true;
}
but its not enabled.
Can anyone please help?
The server side event Page_Load always run first..So you are enabling the button first and then disabling it in the clientside $(document).ready..
I don't know what is your goal.But you can solve the problem by..
protected void Page_Load(object sender, EventArgs e)
{
btnsubmit.Enabled = false;
}
Javascript..
$(document).ready(
function () {
document.getElementById("<%=btnsubmit.ClientID%>").disabled = false;
});
Refer this link for extra reading...
Try with btnsubmit.disabled = false;
like
protected void Page_Load(object sender, EventArgs e)
{
btnsubmit.disabled = false;
}
Page load runs when the page is rendered on the server - First.
After the page gets rendered on the server it gets to the client and then the js starts running.
Your page load assignment gets overridden on the clients end.
You can do both on the server(enable\disable).
delete this:
$(document).ready(
function () {
document.getElementById("<%=btnsubmit.ClientID%>").disabled = true;
});
And use this:
protected void Page_Load(object sender, EventArgs e)
{
if(blah blah blah)
btnsubmit.Enabled = true;
else
btnsubmit.Enabled = false;
}
try this out , using jQuery
$(document).ready(function(){
$('#buttonID/.buttonClass').removeAttr('disabled');
});
and through javascript use
document.getElementById("buttonid").setAttribute("disabled", false);
You can use asp button in front end. And choose enable false to disabled it.
<asp:Button id="btnName" runat="server" Text="Add" Enabled="false"/>
Then you can enable it in server side.
btnName.Enabled = true;

Button click event error

Having just added a new button in my web application, I get an error when clicking on it, and I'm wondering if this is related to misplaced code. I will describe what/where I did, briefly. Thanks very much.
In ascx file:
<asp:Button ID="btn_rezerv" runat="server" Text="Reserve film" OnClick="btn_rezerv_Click"/>
In the ascx.cs file:
namespace CinProj.UserControls
{
public partial class FilmsList : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
PopulateControls();
}
private void PopulateControls()
{
string categId = Request.QueryString["CategID"];
string filmId = Request.QueryString["FilmID"];
....
if (categId != null)
{
.....
}
if (filmId != null)
{
......
Button btn_rezerv = (Button)item.FindControl("btn_rezerv");
}
}
protected void btn_rezerv_Click(object sender, EventArgs e)
{
string fid = Request.QueryString["FilmID"];
ShoppingCartAccess.AddItem(fid);
}
}
}
"Server Error in '/' Application.
Invalid postback or callback argument. Event validation is enabled using 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. "
Another problem could be because your PopulateControls method should probably only be called when during the Page Load when it's not a PostBack. I can't tell from above, but to me it looks like it only needs done on Load. Try wrapping that call with this:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
PopulateControls();
}
}
It's likely the result of making some sort of client change that the server doesn't know about. Many times this is the result of changing values in a dropdown in JavaScript, for example.
To fix, you could:
Do away with using JavaScript for said modification
Use an UpdatePanel and add your control to it. If the client needs to make a change, trigger the UpdatePanel's update in order for the control's viewstate to update.

Button.OnClientClick question

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");
}

Categories