AJAX.NET Subscribe to Reorderlist ItemCommand or DeleteCommand? - c#

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.

Related

trigger button inside a repeater control

I have a button inside a repeater control and I would like to disable it. I tried something like this...
if (Session["USER_ID"] == null)
{
//disable download button and
}
else
{
//enable download button
}
This is the button I want to enable and disable btnTEST
<asp:Repeater ID="Repeater1" runat="server"
OnItemCommand="Repeater1_ItemCommand">
<ItemTemplate>
<asp:Image ID="image" ImageUrl='<%# Eval("image_src")%>' runat="server" />
<asp:Button ID="btnTEST" runat="server" Text="Click Me!" CommandName="testme" Enabled="False" />
</ItemTemplate>
</asp:Repeater>
I am unable to use this:
btnTest.Enabled = True;
It doesn't work for some reason.
Since the button is inside the Repeater-control it will be dynamically generated as many times as there are items in the datasource databound. So you have to enable/disable the button on the repeater ItemDataBound-event as so:
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Button btn = (Button)e.Item.FindControl("btnTEST");
if (Session["USER_ID"] != null)
{
btn.Enabled = true;
}
}
}
Ps. No need to disable since the button is disabled by default.
Because the button is in a repeater. You need to disable the button in the itemdatabound. I suggest you to add something like this in the code behind:
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
Repeater1.ItemDataBound += (s, ev) =>
{
if (ev.Item.ItemType != ListItemType.AlternatingItem && ev.Item.ItemType != ListItemType.Item)
return;
var btnTest= ((System.Web.UI.WebControls.Button) ev.Item.FindControl("btnTEST"));
btnTest.Enabled = Session["USER_ID"] != null;
};
}

Repeater ItemCommand doesn't work

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

c# Why does my page / user control only support one event handler

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:

Handling control events from Repeater footer

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
}
}

The FormView fired event ModeChanging which wasn't handled.

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

Categories

Resources