<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.
Related
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.
In my ASP.NET page's Page_Load I'm trying to determine whether a certain button has been clicked and is attempting a postback:
if (Page.IsPostBack)
{
if (Request.Params.Get("__EVENTARGUMENT") == "doStuff")
doSomething();
}
doStuff is a JavaScript within the markup, but I don't want this alone to trigger the doSomething() method call, I also need to be able to ensure that the button the user has clicked is correct.
How can I identify and reference the button control from the code behind once the user clicks? I searched for and discovered this but when I try to implement it, the control returned is always null.
Or use the CommandName and command events.
<telerik:RadButton ID="RadButton1" runat="server" CommandName="first" CommandArgument="one" OnCommand="CommandHandler" />
<telerik:RadButton ID="RadButton2" runat="server" CommandName="second" CommandArgument="two" OnCommand="CommandHandler" />
<asp:Label ID="Label1" Text="" runat="server" />
<asp:Label ID="Label2" Text="" runat="server" />
protected void CommandHandler(object sender, CommandEventArgs e)
{
Label1.Text = e.CommandArgument.ToString();
Label2.Text = e.CommandName;
}
You haven't really explained what you're trying to do, but it sounds like you would have a much easier time if you added an OnClick event handler to the button instead of trying to figure it out in Page_Load. The event handler would only be executed when that button is clicked.
I've been banging my head against the wall for the last couple of days trying to get this scenario to work.
I have an <asp:UpdatePanel> on a page which has an external trigger and contains a Telerik RadListView:
<asp:UpdatePanel runat="server" ID="RationUpdatePanel" UpdateMode="Conditional" ChildrenAsTriggers="true">
<ContentTemplate>
<telerik:RadListView ID="RationListView runat="server"
DataSourceId="RationDataSource"
OnItemCanceling="ItemCancelingHandler"
OnItemEditing="ItemEditingHandler"
OnItemUpdating="ItemUpdateHandler">
<LayoutTemplate>
<table>
<thead>
<tr>...</tr>
</thead>
<tbody>
<tr id="itemPlaceHolder" runat="server"/>
</tbody>
</table>
</LayoutTemplate>
<ItemTemplate>
<tr>
<td>
<asp:LinkButton ID="EditButton" runat="server" CausesValidation="False" CommandName="Edit" Text="Edit" ToolTip="Edit" />
<asp:LinkButton ID="DeleteButton" runat="server" CausesValidation="False" CommandName="Delete" Text="Delete" ToolTip="Delete" />
</td>
<td>...</td>
</tr>
</ItemTemplate>
<EditItemTemplate>
<tr>
<td>
<asp:LinkButton ID="SaveButton" runat="server" CausesValidation="False" CommandName="Save" Text="Save" ToolTip="Save" />
<asp:LinkButton ID="CancelButton" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel" ToolTip="Update" />
</td>
<td>...</td>
</tr>
</EditItemTemplate>
</telerik:RadListView>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="UseRationButton" EventName="Click" />
</Triggers>
</UpdatePanel>
Code behind looks like this:
protected void RationListView_ItemEditing(object sender, RadListViewCommandEventArgs e)
{
((RadListViewDataItem)e.ListViewItem).Edit = true;
}
protected void RationListView_ItemCanceling(object sender, RadListViewCommandEventArgs e)
{
RationListView.ClearEditItems();
((RadListViewDataItem)e.ListViewItem).Edit = false;
}
Clicking on the EditButton or CancelButton produced the expected events and the ObjectDataSource routine to rebind the list is called, yet the update panel is not refreshed. Calling RationUpdatePanel.Update() from the event handlers does not change the situation. The code works properly outside of an update panel: the mode changes and the buttons from the other template are displayed.
In either scenario clicking on the external trigger causes the list to be displayed.
Update: I added a couple of javascript callbacks to the PageManager class in the browser for beginRequest and endRequest. These just display an alert when the event occurs.
The page contains several elements which basically look like this (details omitted):
<div>
<div>
<asp:DropDownList ID="RationDdl"/>
<asp:Button ID="UseRationButton"/>
</div>
<div>
<asp:DropDownList ID="AnimalGroupDdl"/>
<asp:UpdatePanel ID="AnimalWeightUpdatePanel"/>
<ContentTemplate>
<asp:DropDownList ID="AnimalWeightDdl"/>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</div>
<div>
<telerik:RadListBox>
</telerik:RadListBox>
</div>
<div>
<asp:UpdatePanel ID="FeedPreviewUpdatePanel">
<ContentTemplate>
<asp:Repeater>
</asp:Repeater>
</ContentPanel>
</asp:UpdatePanel>
</div>
<div>
<!-- this contains the update panel shown above -->
</div>
The UseFeedRationButton in the first div is an external trigger for the RationUpdatePanel. AnimalGroupDdl is an external trigger for AnimalWeightUpdatePanel. The handler for the SelectedIndexChanged event on AnimalGroupDdl populates AnimalWeightDdl with available weight classes for the
selected group. It has no interaction with RationUpdatePanel or FeedPreviewUpdatePanel.
Here is the markup for UseRationButton:
<asp:Button ID="UseRationButton" runat="server"
Text="Use This Ration"
OnClick="UseRationButton_Click"
CausesValidation="true"
ValidationGroup="UseRation"
UseSubmitBehavior="false">
</asp:Button>
And here is the code behind:
protected void UseRationButton_Click(object sender, EventArgs e)
{
string text = this.RationDdl.SelectedItem.Text;
this.RationNameLabel.Text = text;
this.RationDataSource.SelectParameters["RationId"].DefaultValue = this.RationDdl.SelectedValue;
}
There are two scenarios I have been testing:
1) Choose a ration from RationDdl and click UseRationButton
2) Choose an animal group and then execute scenario 1.
In scenario 1 an async postback is initiated by the client side asp.net code and the button event is handled server side. The RationListView is populated but the client side endRequest code is never executed (the alert is not displayed). Subsequent clicks on the controls in the RadListView trigger an async postback but the state of the list is not changed and the client side endRequest code is not executed.
In scenario 2 an async postback is initiated, the event is handled on the server side and AnimalWeightDdl is populated. The client side endRequest code is executed in this case (the alert IS displayed). Subsequent to this selecting a ration and clicking on the use ration button initiates a postback, the list view is populated and the endRequest code is executed. At his point clicking on buttons in the list view causes an async postback to be initiated, the state of the list is changed and the endRequest code is executed.
My working theory is that something is causing the state of the page request manager class on the client to become fubared, but I can't figure out what might be causing it.
Firstly , If you use telerik rad control , I suggest you to change your asp:button to telerik:RadButton .
Here is example
<telerik:RadButton ID="v" runat="server" Text="Use This Ration"
AutoPostBack="true"
ValidationGroup="UseRation" OnClick="UseRationButton_Click">
</telerik:RadButton>
protected void UseRationButton_Click(object sender, EventArgs e)
{
string text = this.RationDdl.SelectedItem.Text;
this.RationNameLabel.Text = text;
this.RationDataSource.SelectParameters["RationId"].DefaultValue = this.RationDdl.SelectedValue;
}
Note that , you should add AutoPostBack="true" into your radButton .
And this links , MSDN Reference1 and MSDN Reference2 will show details of using update Panel .
The research I did late Friday and Saturday morning provided the answer to this one.
I had some code attached to the Sys.WebForms.PageRequestManager endRequest event which updated a text box which contained a temporary title for a new ration. The code looked like this:
///
/// reregister the change handlers after reloading the animal weight ddl and create a
/// temporary name for the ration based on animal choice and weight.
///
function animalWeightDdlOnLoad() {
var ddl = document.getElementById("<%= AnimalWeightDdl.ClientID %>");
//
// add event handler in a browser agnostic fashion
//
if (ddl.addEventListener) {
ddl.addEventListener("change", animalWeightDdlOnChange);
}
else if (ddl.attachEvent) {
ddl.attachEvent("onchange", animalWeightDdlOnChange);
}
else {
ddl.onchange = animalWeightDdlOnChange;
}
animalWeightDdlOnChange();
}
animalWeightDdlOnChange() referenced the options list of the animal weight ddl:
///
/// create a temporary name for the ration
///
function animalWeightDdlOnChange() {
var ddl1 = document.getElementById("<%= AnimalGroupDdl.ClientID %>");
var ddl2 = document.getElementById("<%= AnimalWeightDdl.ClientID %>");
var text = document.getElementById("<%= RationNameText.ClientID %>");
text.value = ddl1.options[ddl1.selectedIndex].text + ' ' + ddl2.options[ddl2.selectedIndex].text + ' - Copy';
}
However if the list had not yet been populated the reference to options caused a fault which halted endRequest processing. Rather than having this code run on every async request I changed it to be a handler for the change event on the animal group ddl, like this:
function animalGroupDdlOnSelectedChanged() {
//
// add a handler for the endRequest event which will be triggered by
// fetching the contents of the weight ddl.
//
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(animalWeightDdlOnLoad);
}
Then in animalWeightDdlOnLoad() I added a line to remove the handler once the request had
run. This ensured that the change handler for the weight ddl would only run once the list had been populated. Problem solved. But just in case, I added a check on the length of the animal weight ddl's options list.
So even seemingly innocent javascript can mess up your async request handling in the browser.
I know there are a lot of related posts or articles about this, but then it seems that they're not helping my case. I've even compared with a working sample at this site, http://www.ezineasp.net/post/ASP-Net-LinkButton-Command-Event.aspx, I don't think there's much difference. I thought my code should be working but apparently it just won't. I'm so sorry if this looks like a duplicate, but it's my last resort to post here.
Here's my HTML:
<asp:ListView runat="server" ID="AppsList">
<LayoutTemplate>
<div>
<asp:PlaceHolder runat="server" ID="itemPlaceHolder" />
</div>
</LayoutTemplate>
<ItemTemplate>
<div class="applist">
<div class="app">
<asp:ImageButton ID="imgbtnApp" runat="server" ImageUrl='<%#Eval("Icon") %>' height="100" width="100" CommandName="Select" CommandArgument='<%# Eval("ID") %>' OnCommand="AppsList_ItemCommand" />
</div>
<div class="appname">
<asp:LinkButton ID="linkbtnAppName" runat="server" CommandName="Select" ForeColor="#333333" CommandArgument='<%# Eval("ID") %>' OnCommand="AppsList_ItemCommand" CssClass="linkbtnAppName"><%# Eval("AppName") %></asp:LinkButton>
</div>
</div>
</ItemTemplate>
<EmptyDataTemplate>
Sorry - Nothing found.
</EmptyDataTemplate>
</asp:ListView>
Code:
protected void AppsList_ItemCommand(object sender, CommandEventArgs e)
{
if (e.CommandName == "Select")
{
txtTest.Text = e.CommandArgument.ToString();
}
}
What I'm trying to achieve here is to capture the ID of the item in the ListView into the textbox when I click on either the Image Button or Link Button. Both will perform the same thing. I already got the Image button to work. When I click the Image, the ID, e.g 1 will appear in the textbox. But when I want to do the same thing with the Link Button, nothing will happen. The event is not being triggered in any way.
I've seen some posts talking about repeaters or AJAX to do the same thing, but I was just wondering why can't this code work. I would appreciate any pointer.
For my own experience, I have wrongly set ViewStateMode="Disabled" in page directive.
Because of this when post back occur upon clicking LinkButton inside the List View, existing data are vanished. So, will not reach to LinkButton's OnCommand event and never fire.
Once ViewStateMode attribute is removed, everything working well.
I noticed that your eventArgs should ListViewCommandEventArgs instead of CommandEventArgs and you should also bind the itemCommand event to your aspx page as shown below.
ASPX:
<asp:ListView runat="server" ID="AppsList" OnItemCommand="AppsList_OnItemCommand">
Code Behind:
protected void AppsList_OnItemCommand(object sender, ListViewCommandEventArgs e)
{
if (String.Equals(e.CommandName, "Select"))
{
}
}
UPDATE:
Also remove the OnCommand attribute from image button and Link button OnCommand="AppsList_ItemCommand" , and each CommandName should be different than other.
<div class="applist">
<div class="app">
<asp:ImageButton ID="imgbtnApp" runat="server" ImageUrl='<%#Eval("Icon") %>' height="100" width="100" CommandName="Select" CommandArgument='<%# Eval("ID") %>' />
</div>
<div class="appname">
<asp:LinkButton ID="linkbtnAppName" runat="server" CommandName="Select" ForeColor="#333333" CommandArgument='<%# Eval("ID") %>' CssClass="linkbtnAppName"><%# Eval("AppName") %></asp:LinkButton>
</div>
</div>
</ItemTemplate>
Well, in case someone is wondering why this is happening, this is what I've found out after spending a great deal of time ripping off my codes to see what's wrong with it. I doubt that someone will bump into this issue, but to heck with it, sharing is caring.
Apparently the search button I've placed in my master page is the reason behind this. I've assigned its ID as "submit", which is the thing that is stopping my linkbutton from working, which I have no idea why that is happening. The search function is merely a search engine allowing users to look for the keywords in the webpage. The linkbutton's OnCommmand event just won't trigger when the ID to the search button is assigned as "submit".
When I change it to "submit1", everything works as normal. I'm still a newbie in this asp.net thing, can anyone tell me why is this even affecting the linkbutton? Anyway, the OnCommand event is now working properly.
I had a similar issue with LinkButtons. I don't remember how I figured this out, but I must of got fustrated and started clicking the LinkButton a few times fairly quickly and I noticed that the Command event did fire once in awhile. Soon I figured it's if I clicked on the LinkButton before the page fully loads that the command would fire correctly. If the page fully loaded then it seems something was hijacking that command on the LinkButton.
Without success, from one of the other suggestions I searched for any button that might be using the name/id "submit". There wasn't one on my page.
With success, I tried using ASP.Net Buttons instead of LinkButtons. That seemed to fix my issue. I'm not entirely sure what caused this situation but Buttons seem to work in my case so I'll stick with that.
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 [:$]