I'm having trouble in a Repeater in webform where OnItemCommand event is not working. He should be fired when I click the Linkbutton.
Codigo aspx:
<asp:Repeater ID="repeaterImagens" runat="server"
OnItemCommand="repeaterImagens_ItemCommand"
OnItemDataBound="repeaterImagens_ItemDataBound">
<ItemTemplate>
...
<asp:LinkButton ID="lbExcluir" runat="server"
CommandName="excluir"
CommandArgument="<%# ((String)Container.DataItem) %>"
OnClientClick="if (!confirm('Confirma a exclusão desta imagem?'));">
</asp:LinkButton>
</ItemTemplate>
</asp:Repeater>
Code behind C#
protected void repeaterImagens_ItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.CommandName.Equals("excluir"))
{
ExcluirArquivo(e.CommandArgument.ToString());
}
}
Tested in debug mode, clicking the Linkbutton nothing happens, not even to call the ItemCommand event
Better way is to handle linkbutton client confirmation in ItemDataBound event:
protected void Repeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
LinkButton lb = e.Item.FindControl("lbExcluir") as LinkButton;
if (lb != null) {
lb.OnClientClick = "return confirm('Confirma a exclusão desta imagem?')";
}
}
Related
I have 2 events that I want to fire from a user control back to it's parent.
"UserSelected" and "NoUsersSelected"
One fires when a user is selected and the other fires when all the users have been removed.
My problem is "UserSelected" works, but "NoUsersSelected" fails... In the code snippets below, I have commented the line that fails.
UPDATE:
When I comment all code relating to the first event handler, the 2nd one starts working.
Here is the code on my child (user) control:
public event EventHandler UserSelected;
public event EventHandler NoUsersSelected;
protected virtual void onUserSelected(System.EventArgs e)
{
if (UserSelected != null) UserSelected(this, e);
}
protected virtual void onNoUsersSelected(System.EventArgs e)
{
**###** PROBLEM ! - NoUsersSelected is always null so doesn't fire event **###**
if (NoUsersSelected != null) NoUsersSelected(this, e);
}
And to raise the events, I use buttons inside a GridView TemplateField:
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btnSelect" CommandArgument='<%# Eval("UserID") %>' CommandName="SelectUser" ToolTip="Select User" runat="server" UseSubmitBehavior="false" CssClass="GridBtn_Select" CausesValidation="false" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btnDelete" CommandArgument='<%# Eval("UserID") %>' CommandName="DeleteUser" ToolTip="Delete User" runat="server" OnClientClick="javascript:if(!confirm('Are you sure?')) return false;" UseSubmitBehavior="false" CssClass="GridBtn_Delete" CausesValidation="false" />
</ItemTemplate>
</asp:TemplateField>
And here is the code behind for these buttons:
if (e.CommandName == "SelectUser")
{onUserSelected(System.EventArgs.Empty);}
and...
if (e.CommandName == "DeleteUser")
{ //this is where I remove the user from the grid...
if (gridUsers.Rows.Count < 1)
{onNoUsersSelected(System.EventArgs.Empty);}
.
On the parent page that hosts the user control:
protected void Page_Load(object sender, EventArgs e)
{
//Bind to the event on child control
GridAdvertiserUsers.UserSelected += UserSelected_EventHandler;
GridAdvertiserUsers.NoUsersSelected += NoUsersLinked_EventHandler;
}
and the functions that the events call:
void UserSelected_EventHandler(object sender, EventArgs e)
{ //do stuff here }
void NoUsersLinked_EventHandler(object sender, EventArgs e)
{ //Do other stuff }
UPDATE:
When onUserSelected is called, both Events seem to have their methods wired up correctly
But when onNoUserSelected is called, they are both null:
I have a DataList and inside it I have a DropDownList:
<asp:DataList ID="dlconfigureItem" runat="server">
<ItemTemplate>
<asp:DropDownList CssClass="config-select" ID="ddlitem runat="server"></asp:DropDownList>
</ItemTemplate>
</asp:DataList>
How can I get selectedindexchanged event of DropDownList on the server side? I tried this:
public void ddlitem_selectedindexchanged (object sender, EventArgs e)
{
}
but it is not working.
You have defined the server side method:
public void ddlitem_selectedindexchanged (object sender, EventArgs e)
{
}
but you have not told client side that there is an event for you, so in html code tell it like:
onselectedindexchanged="ddlitem_selectedindexchanged"
and also set AutoPostBack property to true.
From the SelectedIndexChanged event the easiest is to cast the sender to the DropDownList
var ddl = (DropDownList)sender;
The sender is always the control that is the source of the event.
For the sake of completeness, from ItemDataBound of the DataList:
protected void dlconfigureItem_ItemDataBound(object sender, DataListItemEventArgs e)
{
DropDownList ddlitem = e.Item.FindControl("ddlitem") as DropDownList;
if (ddlitem != null)
{
// ...
}
}
Edit: Have you forgotten to register the event?
<asp:DropDownList CssClass="config-select"
ID="ddlitem"
OnSelectedIndexChanged="ddlitem_selectedindexchanged"
runat="server">
</asp:DropDownList>
Note that you should not bind your DataList to it's DataSource on postbacks, otherwise events are not triggered. So check for the IsPostBack property of the page.
For example in page_load:
if(!IsPostBack)BindDataList();
Register the event and set AutoPostBack="true"
<asp:DropDownList CssClass="config-select"
ID="ddlitem"
AutoPostBack="true"
OnSelectedIndexChanged="ddlitem_selectedindexchanged"
runat="server">
</asp:DropDownList>
event (on selected index change you can get the selected value)
protected void ddlCategory_SelectedIndexChanged(object sender, EventArgs e)
{
var ddlList = (DropDownList)sender;
string selectedValue = ((DropDownList)ddlList.NamingContainer.FindControl("ddlitem")).SelectedValue;
}
Not sure if you can't get the selected item on the server or you can't find the way to handle the event. In case your problem is with the event handling, try this
<asp:DataList ID="dlconfigureItem" runat="server">
<ItemTemplate>
<asp:DropDownList CssClass="config-select" ID="ddlitem"
OnSelectedIndexChanged="ddlitem_selectedindexchanged"
AutoPostBack="true" runat="server"></asp:DropDownList>
</ItemTemplate>
</asp:DataList>
Assuming I have the following repeater.
<asp:Repeater ID="MyRepeater" runat="server" onitemdatabound="MyRepeater_ItemDataBound">
<FooterTemplate>
</table>
<asp:Button ID="btnPrevious" runat="server" Text="<" />
<asp:Label ID="lblCurrentPage" runat="server" Text="<%# PagingStatus() %>" />
<asp:Button ID="btnNext" runat="server" Text=">" />
</FooterTemplate>
</asp:Repeater>
How can I handle the click events from btnPrevious and btnNext?
I have tried the following:
protected void MyRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
Button btnPrevious = (Button)e.Item.FindControl("btnPrevious");
Button btnNext = (Button)e.Item.FindControl("btnNext");
if (btnPrevious != null)
btnPrevious.Click += btnPrevious_Click;
if (btnNext != null)
btnNext.Click += btnNext_Click;
}
But this has failed (The event is never raised)..
You can use them in the same way you would use a normal button event handler eg:
Html:
<asp:Button ID="btnNext" runat="server" CommandArgument="<%=Id%>" onclick="Button_OnClick" Text=">" />
Code:
protected void Button_OnClick(object sender, EventArgs e)
{
Button button = sender as Button;
if(button != null)
{
string commandArg = button.CommandArgument;
//Do Work
}
}
The you can use the command argument to find out which button was clicked.
Hope this helps.
I would suggest using the ItemCommand event of the repeater. You still have to add the commands to your buttons though. Like this:
<asp:Button ID="btnPrevious" runat="server" Text="<" CommandName="Previous"/>
protected void MyRepeater_ItemCommand(object source, RepeaterCommandEventArgs e)
{
if(e.CommandName.ToLower().Equals("previous")) {
//go back
}
else
{
//go forward
}
}
Ok, so I'm struggling with using asp:formview.
I've got the formview up and running and I've added the 'Edit' button.
<asp:FormView runat="server" id="fwHotelDetails" DataKeyNames="id" OnDataBound="fwHotelDetails_DataBound" OnModeChanging="fwHotelDetails_ModeChanging" >
<ItemTemplate>
// (..) some code here which outputs some data
<asp:Repeater runat="server" id="repScore">
<ItemTemplate>
<span class="item"> Some output here</span>
<asp:LinkButton ID="EditButton" runat="server" CausesValidation="False" CommandName="Edit" Text="Edit" />
</ItemTemplate>
</asp:Repeater>
<EditItemTemplate>
Test test, anything??
</EditItemTemplate>
</ItemTemplate>
</asp:FormView>
I've tried thefollowing solutions in the code behind - none of them works:
protected void fwHotelDetails_ItemCommand(object sender, FormViewModeEventArgs e)
{
if (e.CommandName.Equals("Edit"))
{
fwHotelDetails.ChangeMode(e.NewMode);
}
}
and this:
protected void fwHotelDetails_ModeChanging(object sender, System.Web.UI.WebControls.DetailsViewModeEventArgs e)
{
fwHotelDetails.ChangeMode((FormViewMode)e.NewMode);
}
Clicking the Edit button only gives me the following error message:
The FormView 'fwHotelDetails' fired event ModeChanging which wasn't handled
What more needs to be done?
This page is a great reference for FormView controller: http://authors.aspalliance.com/aspxtreme/sys/web/ui/webcontrols/FormViewClass.aspx
Update: I've updated code to refelct Phaedrus suggestion.
Current status is that even after clicking Edit button, the content from ItemTemplate is loaded.
You have to specify which method handles the ModeChanging event. This event is raised when a FormView control attempts to switch between edit, insert, and read-only mode, but before the mode actually changes.
<asp:FormView OnModeChanging="fwHotelDetails_ModeChanging" />
The second parameter of your method signature is 'DetailsViewModeEventArgs' it should be 'FormViewModeEventArgs'.
void fwHotelDetails_ModeChanging(Object sender, FormViewModeEventArgs e)
{
}
Just Simply write code in formview's Item_Command
protected void formview_ItemCommand(object sender, FormViewCommandEventArgs e)
{
if (e.CommandName == "Edit")
{
formview.DefaultMode = FormViewMode.Edit;
formview.DataBind();
}
if (e.CommandName == "Cancel")
{
formview.DefaultMode = FormViewMode.ReadOnly;
formview.DataBind();
}
}
I would like to subscribe to the ItemCommand event of a Reorderlist I have on my page. The front end looks like this...
<cc1:ReorderList id="ReorderList1" runat="server" CssClass="Sortables" Width="400" OnItemReorder="ReorderList1_ItemReorder" OnItemCommand="ReorderList1_ItemCommand">
...
<asp:ImageButton ID="btnDelete" runat="server" ImageUrl="delete.jpg" CommandName="delete" CssClass="playClip" />
...
</cc1:ReorderList>
in the back-end I have this on Page_Load
ReorderList1.ItemCommand += new EventHandler<AjaxControlToolkit.ReorderListCommandEventArgs>(ReorderList1_ItemCommand);
and this function defined
protected void ReorderList1_ItemCommand(object sender, AjaxControlToolkit.ReorderListCommandEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
if (e.CommandName == "delete")
{
//do something here that deletes the list item
}
}
}
Despite my best efforts though, I can't seem to get this event to fire off. How do you properly subscribe to this events in a ReorderList control?
this works:
<cc2:ReorderList ID="rlEvents" runat="server" AllowReorder="True" CssClass="reorderList"
DataKeyField="EventId" DataSourceID="odsEvents" PostBackOnReorder="False"
SortOrderField="EventOrder" OnDeleteCommand="rlEvents_DeleteCommand">
...
<asp:ImageButton ID="btnDeleteEvent" runat="server" CommandName="Delete" CommandArgument='<%# Eval("EventId") %>' ImageUrl="~/images/delete.gif" />
...
</cc2:ReorderList>
code behind:
protected void rlEvents_DeleteCommand(object sender, AjaxControlToolkit.ReorderListCommandEventArgs e)
{
// delete the item
// this will give you the DataKeyField for the current record -> int.Parse(e.CommandArgument.ToString());
//rebind the ReorderList
}
Since your ImageButton's CommandName="delete" you should be hooking up to the DeleteCommand event instead of ItemCommand.