Asp.net button onclick event is not firing serverside code - c#

This has been discussed too many times already, but none of those threads helped me.
Below is .aspx code.
<div id="Panel_login" runat="server" class="panel-default">
<asp:UpdatePanel ID="UpdatePanel_login" UpdateMode="Conditional" runat="server">
<ContentTemplate>
<asp:RequiredFieldValidator ID="txtb_loginEmailRequiredFieldValidator" ControlToValidate="txtb_loginEmail" runat="server" ErrorMessage="Email can't be Empty"></asp:RequiredFieldValidator>
<asp:TextBox ID="txtb_loginEmail" CssClass="form-control" placeholder="Your Email" runat="server" TextMode="Email" Width="400px"></asp:TextBox>
<asp:RequiredFieldValidator ID="txtb_loginPasswordRequiredFieldValidator" ControlToValidate="txtb_loginPassword" runat="server" ErrorMessage="Password can't be Empty"></asp:RequiredFieldValidator>
<asp:TextBox ID="txtb_loginPassword" CssClass="form-control" TextMode="Password" placeholder="Password" runat="server" Width="400px"></asp:TextBox>
<asp:Button ID="btn_userlogin" runat="server" OnClick="loginUser" Text="Login" />
</ContentTemplate>
</asp:UpdatePanel>
</div>
Below is my .cs file code.
protected void loginUser(object sender, EventArgs e)
{
Response.Redirect("www.google.com");
log.Debug("login_user is called");
}
On click of button, it should fire event, but it is not happening.
I have tried adding triggers to Update Panel for this button click, still didnt work.
It only gets fired when another button on same page is fired previously.
Can someone suggests what I am missing here.
Thanks for help.

I have seen the event never getting wired up. Try...
(1) In design mode delete the button, drag a new one back on the design surface, then rename your button. OR
(2) Go into design view, click the button, then properties, look at the Lightening Bolt Icon (event) remove that event, save, and add it back and save.

Related

Radio button not visible in ASP.NET page

ASP.NET radio button is not visible in my page. I am using it inside ASP.NET ListView. I tried putting the radio button inside ASP.NET update panel content template as well. Please tell me what has gone wrong?
<asp:UpdatePanel runat="server">
<ContentTemplate>
<asp:ListView ID="FraudCheckList_listView" runat="server">
<ItemTemplate>
<p class="fraud-step-title"><%#Eval("question")%></p>
<div class="row">
<asp:RadioButton
runat="server"
class="radio-inline col"
GroupName="govt"
Text="Low Risk"
></asp:RadioButton>
<asp:RadioButton
runat="server"
GroupName='govt'
Text="Medium Risk"
></asp:RadioButton>
<asp:RadioButton
runat="server"
GroupName='govt'
Text="High Risk"
></asp:RadioButton>
</div>
</ItemTemplate>
</asp:ListView>
</ContentTemplate>
</asp:UpdatePanel>
Since you haven't shared how your code looks like, i am just assuming certain things. Just see if you have done this or not.
void Button_Click(object sender, System.EventArgs e){RadioButton.Visible=false;}
If incase its true or something your radiobutton will not be visible. In your case it must be hidden. So, make the variable of visible as **false ** So you can just change your code to this and try it out.
This you have to do it in your main code. not in your html code.
Thank You!

Asp textbox clear on click

I have some ASP textboxes I want to fill out.
<div class="form-group">
<asp:Label CssClass="form-control-label" ID="LFirstName" Text="First Name"
AssociatedControlID="TBFirstName" runat="server"></asp:Label>
<asp:TextBox CssClass="form-control" ID="TBFirstName"
runat="server" MaxLength="50"></asp:TextBox>
<asp:Label CssClass="form-control-label" ID="LLastName" Text="Last Name"
AssociatedControlID="TBLastName" runat="server"></asp:Label>
<asp:TextBox CssClass="form-control" ID="TBLastName"
runat="server" MaxLength="50"></asp:TextBox>
...
</div>
When a button is pushed...
<asp:Button ID="BTN_UserSubmit" runat="server"
Text="Submit" OnClick="Click_UserSubmit"/>
I have some magical things I want to do with them
protected void Click_UserSubmit(object sender, EventArgs e)
{
Log("TBFistName" + TBFirstName.Text);
Log("TBLastName" + TBLastName.Text);
...
Unfortunately, my log shows nothing but empty strings:
TBFistName
TBLastName
Why does this happen, and how can I keep the values long enough to do anything with them?
You are probably initialising/clearing the values in Page_Load.
Whenever you postback, either by a button click or some other means, the Page_Load method will be called before the click event.
So you need to put any initialisation logic inside:
if (!Page.IsPostback)
{
...
}

asp.net Disable Button after Click while maintaining CausesValidation and an OnClick-Method

.aspx
<asp:TextBox ID="txtInvite" Width="170px" Height="20px" runat="server"
Font-Size="Small" ></asp:TextBox>
<AjaxToolkit:TextBoxWatermarkExtender ID="tbexInvite"
runat="server" SkinID="skinTextBoxWatermarkExtender"
TargetControlID="txtInvite" WatermarkText="Email"></AjaxToolkit:TextBoxWatermarkExtender>
<asp:RequiredFieldValidator ID="rfvInvite" runat="server"
Display="None" ValidationGroup="Inivitation" SetFocusOnError="true"
ControlToValidate="txtInvite" ErrorMessage="Enter Email."></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="regExpInvite" runat="server"
Display="None" ValidationGroup="Inivitation" SetFocusOnError="true"
ValidationExpression="\s*\w+([-+.]\w+)*#\w+([-.]\w+)*\.\w+([-.]\w+)*\s*"
ControlToValidate="txtInvite" ErrorMessage="Invalid Email Format."></asp:RegularExpressionValidator>
<div class="ButtonLogin" style="margin-top:-27px;margin-right:143px;_margin-right:105px;">
<asp:UpdatePanel ID="UpdatePanel1" runat="server" >
<ContentTemplate>
<asp:Button ID="btnInvite" runat="server" CssClass="cssLoginButton blue" Text="Invite" ToolTip="Invite" ValidationGroup="Inivitation" CausesValidation="true" onclick="btnInvite_Click" />
</ContentTemplate>
</asp:UpdatePanel>
</div>
<asp:UpdateProgress ID="UpdateProgress1" runat="server" AssociatedUpdatePanelID="UpdatePanel1" DisplayAfter="0">
<ProgressTemplate>
<asp:Image ID="Image1" ImageUrl="~/App_Themes/Lifetrons/images/progressbar.gif" AlternateText="Processing" runat="server" />
</ProgressTemplate>
</asp:UpdateProgress>
Page_load Code
.CS
this.btnInvite.Attributes.Add("onclick", "this.disabled=true;" + Page.ClientScript.GetPostBackEventReference(this.btnInvite, "").ToString());
I want to disable button after clicking on it with maintaining validation and Onlick method. I refer to this question my problem is same like this question but I haven't solve my problem. How can I solve?
There is no need to mess around with your code-behind Page_Load event. In your button control, just add the following snippet:
OnClientClick="if(Page_ClientValidate('Inivitation')){this.disabled=true;}"
So that your button looks like
<asp:Button ID="btnInvite" runat="server" CssClass="cssLoginButton blue" Text="Invite"
ToolTip="Invite" ValidationGroup="Inivitation" CausesValidation="true"
onclick="btnInvite_Click" OnClientClick="if(Page_ClientValidate('Inivitation')){this.disabled=true;}" />
You should be able to disable the button using the Button_Click method. Make your first line something like this:
Button.Enabled = False;
There is a problem with this method though. If the page loads too quickly or the code freezes it will not disable the button properly. Just remember to re-enable the button at the end of the method if the validation fails or times out.
Another method you may try is to hide it using CSS. I am a big fan of this because it always works and it is very simple. You need a technology like jQuery where you can add classes on the fly. You can use the AddClass and RemoveClass API in jQuery to add/remove CSS classes at runtime. Take a look here. http://jqueryui.com/addClass/
you can use Like this
<asp:Button ID="btnInvite" runat="server" CssClass="cssLoginButton blue" Text="Invite" ToolTip="Invite" ValidationGroup="Inivitation" CausesValidation="true" UseSubmitBehavior = False"onclick="btnInvite_Click" />

C# codebehind not getting hit when button clicked

<asp:UpdatePanel runat="server" ID="userRemovalUpdatePanel">
<ContentTemplate>
<p><label>Remove: </label>
<asp:DropDownList runat="server" ID="removeUserList" /></p>
<br />
<asp:Button runat="server" ID="removeUserBtn" Text="Remove User"
onclick="removeUserBtn_Click" CssClass="buttons" />
<p><label for="deleteStatus">Delete status: </label></p><br />
<asp:Label runat="server" ID="deleteStatusLbl" Text="" Font-Size="Medium" Width="100" ForeColor="Red" />
</ContentTemplate>
</asp:UpdatePanel>
I put a break point on the below code behind and it never gets hit. The code behind for this is:
protected void removeUserBtn_Click(object sender, EventArgs e)
{
string userToDelete = removeUserList.SelectedValue;
Business.User deleteUser = new Business.User();
deleteStatusLbl.Text = deleteUser.DeleteUser(userToDelete);
fillUserDropDown();
}
It's not: onclick="removeUserBtn_Click"
It is: OnClick="removeUserBtn_Click"
Do you have any validators on the page? Also place the Application_Error event in your Global.asax and try to log any errors that take place.
Validators can prevent the postback and behave strangely with update panels sometimes.
Do you have any code running in Page_Load()?
Code here runs before any event handlers are fired, so if something was interrupting your page loading here, say redirecting to another page etc. the click handler would never be fired.
The page lifecycle is described here.
Page_PreRender() is an alternative place to put code that you want to run after the event handlers fire.

Conditional ConfirmButtonExtender

I am trying to bypass the ConfirmButtonExtender depending on the value of another field in the page.
Basically, when a user click on my "Cancel" button, I normally display a modalpopup using the confirmbuttonextender and the modalpopupextender to display a dialog box confirming that they wish to cancel any changes they have made and return to the prior screen. If they click Yes, the button's onclick event fires which calls some code in the codebehind and redirects the user to another page. If they click no, it just returns to the same page, with no changes.
However, in some situations, I know that my user is unable to perform any edits (they aren't allowed to) and for those users, I don't want to display the "Are you sure you want to leave you will loose any changes" dialog box.
I've set a hidden checkbox field named "cbAllowEdit" to indicate whether the user is allowed to edit the fields or not.
I was trying to use the technique found at link text to get this working but it just doesn't even seem to be firing the button's onclientclick event at all.
Here is my code.
ASPX & Javascript
<asp:CheckBox ID="cbAllowEdit" runat="server" Checked="true" />
<asp:Button ID="btnCancel" runat="server" CausesValidation="false"
OnClick="btnCancel_Click" Text="Cancel" OnClientClick="disableSubmit();return false;" />
<ajaxToolKit:ConfirmButtonExtender ID="ConfirmButtonExtenderbtnCancel"
runat="server" DisplayModalPopupID="ModalPopupExtenderbtnCancel"
TargetControlID="btnCancel" BehaviorID="ConfirmButtonExtenderbtnCancel" />
<ajaxToolKit:ModalPopupExtender ID="ModalPopupExtenderbtnCancel" runat="server"
BackgroundCssClass="modalBackground" CancelControlID="btnCancelCancel"
OkControlID="btnConfirmCancel" PopupControlID="ConfirmCancelPanel"
TargetControlID="btnCancel" />
<asp:Panel ID="ConfirmCancelPanel" runat="server" CssClass="modalWindow"
Height="200" Width="450">
<p class="confirmMessage">
Are you sure you want to navigate away from this record?
</p>
<div align="center">
<p class="feedbackError">If you have made any changes to the record since the last time
you saved, they will be lost.</p>
<asp:Button ID="btnConfirmCancel" runat="server" Text="Yes" Width="75" />
<asp:Button ID="btnCancelCancel" runat="server" Text="No" Width="75" />
</div>
</asp:Panel>
<script type="text/javascript">
function disableSubmit() {
if (document.getElementById('<%= cbAllowEdit.ClientID %>').checked) {
return checkSubmit();
}
else {
return true;
}
}
function checkSubmit() {
var confirmButton = $find('ConfirmButtonExtenderbtnCancel');
confirmButton._displayConfirmDialog();
}
</script>
Code behind:
/// <summary>
/// Runs when the btnCancel button is clicked.
/// </summary>
protected void btnCancel_Click(object sender, EventArgs e)
{
Page.Response.Redirect("~/Searches/LookupCode/Default.aspx");
}
any ideas on what I am doing wrong?
As Chris said, you don't have to have a ConfirmButtonExtender and ModalPopupExtender (although this looks like a way round the fact that the JavaScript confirm() function is limited to OK and Cancel buttons in its' dialog - noted as a technique for the future :-) )
If you know in your code-behind that the user isn't going to be doing any editing (as evidenced by your cbAllowEdit control), then you should be able to just disable the ConfirmButtonExtender:
ConfirmButtonExtenderbtnCancel.Enabled = false;
Then clicking the Cancel button should take you into your code-behind function without confirmation at all.
EDIT: The ConfirmPopupExtender shouldn't be pointing to the btnCancel. Instead, it should be pointing to a hidden button (this is what the example in the link does).
<asp:Button ID="btnCancel" runat="server" CausesValidation="false"
OnClick="btnCancel_Click" Text="Cancel" OnClientClick="disableSubmit();return false;" />
<!-- This is the Hidden Buttons that you need to add and reference in the ConfirmButtonExtender -->
<asp:Button ID="HiddenButtonConfirm" runat="server" Text="" style="display:none;" />
<asp:Button ID="HiddenButtonModal" runat="server" Text="" style="display:none;" />
<ajaxToolKit:ConfirmButtonExtender ID="ConfirmButtonExtenderbtnCancel"
runat="server" DisplayModalPopupID="ModalPopupExtenderbtnCancel"
TargetControlID="HiddenButtonConfirm" BehaviorID="ConfirmButtonExtenderbtnCancel" />
<!-- You'll also want the ModalPopup to point to a seperate hidden button. -->
<ajaxToolKit:ModalPopupExtender ID="ModalPopupExtenderbtnCancel" runat="server"
BackgroundCssClass="modalBackground" CancelControlID="btnCancelCancel"
OkControlID="btnConfirmCancel" PopupControlID="ConfirmCancelPanel"
TargetControlID="HiddenButtonModal" />
This javascript might work:
var confirmbtn = $find('ConfirmButtonExtenderID');
confirmbtn.enabled =false;
//ConfirmButtonExtenderID needs to be the name of your ConfirmButtonExtender ID [:$]

Categories

Resources