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();
}
}
Related
I'am trying to hide/show the edit/save button in a ASP.NET Template.
so i want the edit button too show when no row is selected, and then hide it on click and then make the save button visable instead.
How do I access and update the attribute?
The solution ive tried just gives me "Null"
what i have:
<ItemTemplate>
<asp:ImageButton ID="ImageButtonEdit" runat="server" CommandName="Edit" ImageUrl="loginstyling/images/Edit.png"/>
<asp:ImageButton ID="ImageButtonUpdate" runat="server" CommandName="Update" ImageUrl="loginstyling/images/Save.png" OnClick="ImageButtonUpdate_Click" Visible="true"/>
<asp:ImageButton ID="ImageButtonDelete" runat="server" CommandName="Delete" ImageUrl="loginstyling/images/Remove.png" visible="false" />
</ItemTemplate>
what ive tried behind:
protected void ImageButtonUpdate_Click(object sender, ImageClickEventArgs e)
{
ImageButton test = (ImageButton)GridView1.FindControl("ImageButtonUpdate");
test.Attributes.Add("Visible", "False");
}
Try if this works:
test.Visible = false;
Try using RowDataBoundEvent for GridView, if it works:
protected void gridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
ImageButton test = (ImageButton)e.Row.FindControl("ImageButtonUpdate");
test.Visible = false;
}
}
Are you sure that the .FindControl("ImageButtonUpdate");returns a valid object?
Since it returns null check this other post FindControl() return null , it seems to be the same problem you have encountered.
As topic , I have a item template than consist of a dropdownlist , when user attempt to click it , warning should come out to warn User whether to continue or not. after I click OK , nothing happens , it is not going back to the postback
My gridview code for item template is like the following :
<asp:TemplateField HeaderText="Status">
<ItemTemplate>
<asp:DropDownList ID="cboStatus" runat="server" AutoPostBack="True">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
My Code behind will set the JavaScript to the attribute
DropDownList cboStatus= (DropDownList)e.Row.FindControl("cboStatus");
cboStatus.Attributes.Add("onChange", "Confirmation();");
JavaScript :
function Confirmation() {
if (confirm('Are you sure you want to do this?')) {
__doPostback(this, 'Select${0}');
}
}
I want my postback to call this function
protected void cboStatus_Click(object sender, EventArgs e)
{
//Some Code
}
Change your code a bit like below. This has been tested locally to be working fine. The below code will fire up the server event for selection changed if Ok from the confirmation window is clicked otherwise not.
Your gridview template definition
<asp:TemplateField HeaderText="Status">
<ItemTemplate>
<asp:DropDownList ID="cboStatus" runat="server" AutoPostBack="true" onchange="return Confirmation ();" OnSelectedIndexChanged="cboStatus_SelectedIndexChanged">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
JavaScript function for the onchange event
function Confirmation() {
if (confirm('Are you sure you want to do this?')) {
__doPostBack('__Page', '');
}
return false;
}
And finally your server event for SelectedIndexChanged
protected void cboStatus_SelectedIndexChanged(object sender, EventArgs e)
{
// Some code
}
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 created my gridview with checkboxes inside of it with this code.
<asp:GridView ID="GridView1" runat="server" Width="366px" autogeneratecolumn="false">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="SelectAllCheckBox" runat="server" AutoPostBack="true" oncheckedchanged="SelectAllCheckBox_OnCheckedChanged" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="EachCheckBox" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
I tried check/uncheck it.
enter link description here
protected void SelectAllCheckBox_OnCheckedChanged(object sender, EventArgs e)
{
String test = "test";
test = "newtest";
GridView1.DataSource = null;
GridView1.DataBind();
}
But it doesn't trigger any event.
enter link description here
I'm trying to find where my code is missing and searched so far but still can't.
Thank you for your help!
You must use OnItemCreated or OnItemDataBound and link your checkbox with your delegate
void Item_Created(Object sender, DataGridItemEventArgs e)
{
CheckBox cbx = (CheckBox)e.Item.FindControl("SelectAllCheckBox");
cbx.CheckedChanged += SelectAllCheckBox_OnCheckedChanged;
}
The code looks fine and works for me.
I suspect you might be binding the GridView on every postback.
When you click the CheckBox with the event attached it causes the page to refresh. If you bind the CheckBox on Page_Load (or any method that occurs on every trip to the server) it will bind the grid every time you click the CheckBox. In this case it will never get as far as firing your event.
If so, try checking for a postback before binding your GridView.
For example:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Gridview1.DataSource = myDataSource;
GridView1.DataBind();
}
}
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
}
}