I am dynamically adding buttons to a listview and using the ItemCommand event to handle the button click event using the CommandName property for the button. It works fine in IE, but when I try in Firefox 5, it is hitting the page load event but not the ItemCommand event. Is there a work-around for Firefox?
Thanks!
<asp:ListView ID="lvItems" runat="server" OnItemDataBound="lvItems_ItemDataBound"
DataSourceID="odsItems" OnItemCommand="lvItems_ItemCommand" DataKeyNames="ItemID"
OnDataBound="lvItems_DataBound" OnPagePropertiesChanging="lvItems_PagePropertiesChanging">
<LayoutTemplate>
<div id="itemPlaceholder" runat="server">
</div>
</LayoutTemplate>
<ItemTemplate>
<div>
<asp:Label ID="lbl" runat="server">
</asp:Label>
<asp:Button ID="btnAdd" runat="server" CommandName="Add" Text="Add" OnClientClick="this.disabled=true;" />
</div>
</ItemTemplate>
<EmptyDataTemplate>
No items found for the selected filters. Please try again.<br />
<br />
</EmptyDataTemplate>
</asp:ListView>
protected void lvItems_ItemCommand(object sender,ListViewCommandEventArgs e)
{
if (e.CommandName == "Add")
{
//code here;
}
}
You have to set UseSubmitBehaviour to false, because disabling a button on clientside will cancel the browsers submit. By the way, in IE it's exactly the same.
<asp:Button ID="btnAdd" runat="server" CommandName="Add" Text="Add"
UseSubmitBehavior="false" OnClientClick="this.disabled='true';" />
On this way ASP.NET will append the necessary client-script to postback at the end of your script:
__doPostBack('btnAdd','')
http://encosia.com/disable-a-button-control-during-postback/
OnclientClick and OnClick is not working at the same time?
Related
Will something like this work? If it does, how do I determine which button was clicked on post?
<asp:UpdatePanel ID="MainUpdatePanel" runat="server"><ContentTemplate><div class="home_c" id="home_c2">
<div class="home-tabs">
<asp:Button CssClass="home_tab" ClientIDMode="static" Text="Cures" ID="cures_btn" runat="server" />
<asp:Button CssClass="home_tab" ClientIDMode="static" Text="Conditions" ID="conditions_btn" runat="server" />
<asp:Button CssClass="home_tab" ClientIDMode="static" Text="Recent" ID="recent_btn" runat="server" />
<asp:Button CssClass="home_tab" ClientIDMode="static" Text="Uncured" ID="uncured_btn" runat="server" />
</div>
<div class="home-search">
</div>
</div>
</ContentTemplate></asp:UpdatePanel>
Yes it will work. Just add OnClick events for each and have your click events defined in your code behind to handle them. Simple as that. :)
E.g.
<asp:Button CssClass="home_tab" ClientIDMode="static" Text="Cures" ID="cures_btn" runat="server" OnClick="cures_btn_Click" />
And in your code behind
protected void cures_btn_Click(object sender, EventArgs e)
{
// Some code
}
Maybe I am not understanding how this works completely but what I have is a gridview custom template with an imagebutton and a modalpopup. Below my page I have my popup panel which is hidden. When I click my image button the panel displays then I click the "btnModalCancelAll" and it starts to step through my method which sets a page variable from 0 to 1 but then the problem comes in it does not continue to the original gridview image click event not sure why. Below is my template field and the popup panel.
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton ID="ImageButtonCancelResv" runat="server" Width="20" Height="20px" ImageUrl="~/Images/Delete.png" OnClick="ImageButtonCancelResv_Click" />
<asp:ModalPopupExtender ID="mpe" runat="server" TargetControlID="ImageButtonCancelResv" PopupControlID="pnlModalPanel" CancelControlID="btnModalCancel"></asp:ModalPopupExtender>
<br />
<asp:Label ID="Label1" runat="server" Text="Cancel"></asp:Label>
</ItemTemplate>
<asp:Panel ID="pnlModalPanel" CssClass="modalBackground" runat="server" Style="display:none">
<asp:UpdatePanel runat="server" ID="updatePanelPopUp">
<ContentTemplate>
<div id="modalWrap">
<asp:Label ID="lblAreyousure" runat="server" Text="Are you sure you want to do that?"></asp:Label>
<div id="divhr1" class="horizontalRule" runat="server"></div>
<div id="divCancelSummaryAll">
<asp:Label ID="lblEntireFamily" runat="server" Text="Cancel all family reservations from this event date."></asp:Label><br />
<asp:Label ID="lblSummaryDeleteAll" runat="server" Text=""></asp:Label><br />
<asp:Button ID="btnModalDeleteAll" runat="server" CssClass="modalButton" Text="Cancel all Family" OnClick="btnModalDeleteAll_Click" />
</div>
<div id="divCancelSummaryOne">
<asp:Label ID="Label2" runat="server" Text="Cancel selected child reservation from this event date."></asp:Label><br />
<asp:Label ID="lblSummaryDeleteOne" runat="server" Text=""></asp:Label><br />
<asp:Button ID="btnModalDeleteOne" runat="server" CssClass="modalButton" Text="Cancel this Child" OnClick="btnModalDeleteOne_Click" />
</div>
<br />
<asp:Button ID="btnModalCancel" runat="server" Text="Cancel" />
</div>
</ContentTemplate>
</asp:UpdatePanel>
</asp:Panel>
I suggest you create another button and use it as the TargetControlID for the modal popup.
you can hide this button by setting its display to 'none'. This will only be a dummy button so that your code compiles.
Now when you click the button "ImageButtonCancelResv" it will run your code which is in the method OnClick="ImageButtonCancelResv_Click"
and at the end of this method you can add a command to show the popup:
mpe.Show();
Example Code:
protected void ImageButtonCancelResv_Click(object sender, EventArgs e)
{
//Do something here when button is clicked
mpe.Show();
}
And then
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton ID="ImageButtonCancelResv" runat="server" Width="20" Height="20px" ImageUrl="~/Images/Delete.png" OnClick="ImageButtonCancelResv_Click" />
<div style="display: none">
<asp:Button ID="btnModalStatusHidden" runat="server" />
</div>
<asp:ModalPopupExtender ID="mpe" runat="server" TargetControlID="btnModalStatusHidden" PopupControlID="pnlModalPanel" CancelControlID="btnModalCancel"></asp:ModalPopupExtender>
<br />
<asp:Label ID="Label1" runat="server" Text="Cancel"></asp:Label>
</ItemTemplate>
In Internet Explorer if I hit enter in the TextBox P it submits the Form using the onclick event of LoginButton.
This is what I want to happen.
In Google Chrome if I hit enter it submits the form with the onclick of Button1.
This is not what I want to happen.
Button1 is actually not visible on the form under normal circumstances. It is made visible under certain circumstances to do a different task then login.
How can I force this, browser independently, to always use LoginButton onclick when someone presses enter?
<asp:TextBox ID="P" runat="server" TextMode="Password" Width="150"></asp:TextBox>
<asp:LinkButton CssClass="button" ID="LoginButton"
runat="server" CommandName="Login" Text="Log In" ValidationGroup="Login1"
onclick="LoginButton_Click" />
<asp:Button
ID="Button1" runat="server"
Text="Submit" onclick="Button1_Click" />
You set the forms default button:
<form id="Form1" defaultbutton="SubmitButton" runat="server">
The Following works for me.
<form id="form1" runat="server" defaultbutton="LoginButton">
<div>
<asp:TextBox ID="P" runat="server" TextMode="Password" Width="150"></asp:TextBox>
<asp:LinkButton CssClass="button" ID="LoginButton"
runat="server" CommandName="Login" Text="Log In" ValidationGroup="Login1"
OnClick="LoginButton_Click" />
<asp:Button ID="Button1" runat="server" Text="Submit" OnClick="Button1_Click" > </asp:button>
</div>
</form>
You'll probably want to set a default button in your Page_Load method, like this:
this.Form.DefaultButton = this.LoginButton.UniqueID;
I ended up using C# in my Page_Load(object sender, EventArgs e) method to set the DefaultButton. Found this on another stackoverflow post.
https://stackoverflow.com/a/2213237/2907463
this.Form.DefaultButton = Login1.FindControl("LoginButton").UniqueID;
I have a ModalPopupExtender with an UpdatePanel inside. The UpdatePanel has a Repeater with a list of LinkButtons.
<asp:Button ID="btnShow" runat="server" />
<ajaxToolkit:ModalPopupExtender ID="mpe" runat="server" TargetControlID="btnShow" PopupControlID="pnl" CancelControlID="btnCancel" />
<asp:Panel ID="pnl" runat="server">
<asp:UpdatePanel ID="up" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Repeater ID="rep" runat="server" onitemcommand="rep_ItemCommand">
<HeaderTemplate>
<ul>
</HeaderTemplate>
<ItemTemplate>
<li>
<asp:Label ID="lblAssignedTo" runat="server" Text='<%# Eval("AssignedTo") %>' />
<asp:LinkButton ID="lnkUnassign" runat="server" CommandName="Unassign" CommandArgument='<%# Eval("Id") %>' />
<ajaxToolkit:ConfirmButtonExtender ID="cbeUnassign" runat="server" TargetControlID="lnkUnassign" ConfirmText="Are you sure you want to unassign this item?" />
</li>
</ItemTemplate>
<FooterTemplate>
</ul>
</FooterTemplate>
</asp:Repeater>
</ContentTemplate>
</asp:UpdatePanel>
</asp:Panel>
protected void rep_ItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.CommandName == "Unassign")
{
//do something
up.Update();
}
}
When I click on a LinkButton, the UpdatePanel should update. It does this but it closes the ModalPopupExtender also.
Is there any way to update the UpdatePanel without hiding the ModalPopupExtender? I can just call ModalPopupExtender.Show(), but the page flickers.
Thanks.
Okay, just found out that there's something wrong with LinkButtons inside Repeaters. Used a Button control instead.
I have a button nested inside an update panel which has a CommandArgument tied to it. This calls a method which updates some label and text in an area not contained in the UpdatePanel. If I comment out the update panel the button works correctly so I know it is coming from the update panel. Anyone know how I can pass this through?
protected void Button_Command(object sender,
System.Web.UI.WebControls.CommandEventArgs e)
{
//update textboxes and labels here
}
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<div>
<asp:DataList ID="dListItems" runat="server" DataKeyField="PRODUCT_ID" RepeatColumns="4"
RepeatDirection="Horizontal" ShowFooter="False" ShowHeader="False" CellPadding="4">
<HeaderTemplate>
No Record Found....!
</HeaderTemplate>
<ItemTemplate>
<asp:Button ID="Button" runat="server" Text="Add to Cart"
CommandArgument='<%# Eval("Id") %>' CausesValidation="False"
CssClass="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only"
OnCommand="Button_Command"
/></span></span></p>
</ItemTemplate>
</asp:DataList>
</div>
</td>
</tr>
</table>
</div>
</ContentTemplate>
which updates some label and text in an area not contained in the
UpdatePanel
That's the problem. UpdatePanel will only update what is within and not what is outside. Try to put those control also the UpdatePanel and see them worked