I have a button within the ItemTemplate of my ListView:
<asp:ListView ID="notificiationsList" runat="server">
<ItemTemplate>
<button type="submit" commandargument='<%# Eval("offerID") %>' onclick="Accept_Click" runat="server" >Accept</button>
</ItemTemplate>
</ListView>
Then I have a breakpoint in my code:
protected void Accept_Click(object sender, EventArgs e)
{
.... // breakpoint here
}
However when I debug the page does nothing and it doesn't reach the breakpoint for some reason?
Does anyone understand what I am doing wrong?
I'm not entirely sure how you are binding your ListView. I created the following code with a few tweaks to what you have above.
<asp:ListView ID="lvNotification" runat="server">
<ItemTemplate>
<asp:LinkButton ID="lbAccept" runat="server" OnClick="Accept_Click" CommandArgument="test" Text="Accept" />
</ItemTemplate>
</asp:ListView>
Binding the ListView:
List<string> tL = new List<string>(){ "this", "and", "that"};
lvNotification.DataSource = tL;
lvNotification.DataBind();
And I reused your click code:
protected void Accept_Click(object sender, EventArgs e)
{
// breakpoint here
}
I was able to hit the breakpoint without issue.
I had the same issue and was a statement in the pageload that causes the ListView to be binded again.
This causes the initial event lost.
Check the Page_Load just to be sure :)
HTH,
Milton
Related
I'm developing a ASP.NET website with a C# backend. I'm having a problem with how to set an onclick event for buttons that are nested inside of both a loginview and a repeater. The code works fine for displaying all of the other data (anonymous view displays only an error message) but right now the buttons just redirect to the same page and remove the repeater and all contents, whereas they're supposed to run a specific delete function. The repeater, as it is right now, uses an alternatingitem template. If I remove the buttons from the nested controls, they work. I've tried this with buttons, linkbuttons, and imagebuttons. I'd rather use the latter, if possible. Is it possible to assign an Onclick to these buttons if they're nested like this? If not, what approach should I use?
<asp:LoginView ID="LoginLinksView" runat="server" EnableViewState="false">
<AnonymousTemplate>
<asp:Label ID="errorlabel" runat="server"></asp:Label>
</AnonymousTemplate>
<LoggedInTemplate>
<asp:Repeater id="Repeater" runat="server" >
<HeaderTemplate>
<table cellspacing="0" cellpadding="0">
<thead></thead>
</HeaderTemplate>
<ItemTemplate>
<tr class="Repeaterrow">
<!--Additional code here-->
<asp:ImageButton ID="delbutton" runat="server" ImageUrl=
"~/Images/delete.png" Onclick="DeleteOnClick"/>
</tr>
</ItemTemplate>
<AlternatingItemTemplate>
<tr class="Repeaterrow">
<!--Additional code here-->
<asp:ImageButton ID="delbutton" runat="server" ImageUrl=
"~/Images/delete.png" Onclick="DeleteOnClick"/>
</tr>
</AlternatingItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
</LoggedInTemplate>
</asp:LoginView>
Here are the problems with your approach
1- The button issues postback as it should. But you need to put some CommandArgument with to identify "key" or which row you are processing it for.
2- Re Bind your Repeater with source. Below is the sample code for you.
protected void Page_Load(object sender, EventArgs e)
{
BindRepeater();
}
private void BindRepeater()
{
List<int> items = new List<int>();
for (int i = 0; i < 10; i++)
{
items.Add(i);
}
Repeater.DataSource = items;
Repeater.DataBind();
}
protected void DeleteOnClick(object sender, EventArgs e)
{
ImageButton delbutton = (sender as ImageButton);
//1- call your method with passing in delbutton.CommandArgument - it will give you key/ whatever you like
//2- Rebind the Repeater here and that will bind controls again...
BindRepeater();
}
protected void Repeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
ImageButton delbutton = (sender as RepeaterItem).FindControl("delbutton") as ImageButton;
if (delbutton != null)
{
delbutton.CommandArgument = (sender as RepeaterItem).ItemIndex.ToString();
}
}
and ASPX Repeater definition would change to
Thanks,
Riz
I´m having some wierd issus with my asp:Linkbutton functionality in my asp:ListView.
Here is my code:
<asp:ListView ID="lvData" runat="server" OnItemCommand="lvData_ItemCommand" OnItemDataBound="lvData_ItemDataBound">
<LayoutTemplate>... </LayoutTemplate>
<ItemTemplate>
...
<td>
<asp:LinkButton ID="ItemLink" runat="server" CommandName="View" Text='<%# Eval("NameOfBatch")%>'></asp:LinkButton>
</td>
...
my code-behind is like this:
protected void lvData_ItemCommand(object sender, ListViewCommandEventArgs e)
{
string smu = "";
}
if I put a breakpoint on string smu it never goes there.
The only thing that happens is that my table dissapears and nothing else.
Do you have any ideas ?
Set the CausesValidation property to false on the controls if you have validation on the form and don't want them to trigger the validation.
I have a server method that I want to run from a link inside the LayoutTemplate of my ListView. I've found that it doesn't fire unless I move the link outside the LayoutTemplate. Is there any other way to run a Serverside event from an anchor or LinkButton inside of a LayoutTemplate? I thought about trying a link with a CommandName and capturing that inside of OnItemCommand but I don't know if that works inside of the LayoutTemplate.
My anchor inside the LayoutTemplate:
<a runat="server" id="proceedCheckout" OnServerClick="startCheckout">
The server method:
public void startCheckout(object sender, EventArgs e)
{....
I attempted to replicate your issue but with no luck. It seems to work for me. I have included my test mark-up and code so that you can see if you are missing something:
Markup
<asp:ListView ID="listView1" runat="server">
<LayoutTemplate>
<div>
<asp:LinkButton ID="linkButton1" runat="server" OnClick="LinkButton_Click" Text="Link Button"></asp:LinkButton>
</div>
<div runat="server" id="itemPlaceholder" />
</LayoutTemplate>
<ItemTemplate>
<div><%# Container.DataItem %></div>
</ItemTemplate>
</asp:ListView>
Code
protected void Page_Load(object sender, EventArgs e)
{
List<string> data = new List<string>();
data.Add("Item 1");
data.Add("Item 2");
data.Add("Item 3");
listView1.DataSource = data;
listView1.DataBind();
}
protected void LinkButton_Click(object sender, EventArgs e)
{
//...
}
Hope this helps.
I want to fire the Delete Command in Datalist Control .. but it is not firing .. Help please ..
this is my code :
protected void DeleteCommand(object source, DataListCommandEventArgs e)
{
Label2.Text = "hello";
}
and This is my html code :
<asp:DataList ID="DLImages" runat="server" BorderStyle="None"
DataKeyField="fId" RepeatColumns="4"
RepeatDirection="Horizontal" ShowFooter="False" ShowHeader="False"
OnDeleteCommand="DeleteCommand"
onitemdatabound="DLImages_ItemDataBound">
<ItemTemplate>
<asp:ImageButton ID="IBDelete" runat="server" BorderStyle="None" CommandName="Delete" ImageUrl="~/Dashboard/Images/dldelete.png" />
</ItemTemplate>
</asp:DataList>
..
Your code looks like OK to me. It should fire the DeleteCommand.
But the problem is that I am sure you are binding the Datalist in your page_load event, but not under If(!IsPostBack) condition. What happens when you hit Delete button is your page_load event fires before your DeleteCommand and it rebinds the DataList and your event is lost
Your page_load event code should look like...
If(!IsPostBack)
{
DataList binding code goes here......
...........................
}
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
// Bind the DataList here....
}
this seemed simple at first but I can't get it to work.
I have the following scenario:
<asp:ListView ID="CommentsListView" runat="server">
<LayoutTemplate>
<asp:PlaceHolder ID="itemPlaceholder" runat="server" />
</LayoutTemplate>
<ItemTemplate>
<UC:Comment runat="server" CommentItem="<%# CurrentComment %>" />
<br />
</ItemTemplate>
</asp:ListView>
<asp:TextBox ID="NewComment" runat="server" />
<asp:ImageButton ImageUrl="/images/ball.png" runat="server"
OnClick="SubmitComment" />
Code:
protected void Page_Load(object sender, EventArgs e)
{
RenderListView();
}
protected void RenderListView()
{
CommentsListView.DataSource = //Get the data source objects
CommentsListView.DataBind();
}
protected CommentObject CurrentComment
{
get { return (CommentObject)Page.GetDataItem(); }
}
protected void SubmitComment(object sender, ImageClickEventArgs e)
{
//code to submit comment
RenderListView();
}
basically, when I submit a comment, I want to see it in the ListView, but I don't. "MyControl" gets a null comment in the post-back, for all of the items (not just the new one).
Only after I refresh the page, I can see the new comment that I'v submitted. I can't however refresh the page every submit because this code is inside an UpdatePanel (the issue occurs without the UpdatePanel as well).
Any idea how to solve this?
I can't find any specifics on this, but I have a hunch the user control in your ItemTemplate is causing the issue. Can you remove it and see if it works?
I notice that you are calling RenderListView in both SubmitComment and PageLoad which I believe will cause it to fire twice when the button is clicked (with PageLoad firing first). It looks like the code you posted is simplified. Is it possible that there is something happening in the PageLoad which is sabotaging your SubmitComment steps?
I finally solved it, if anyone else may encounter this -
The solution was to avoid "<%#" and instead use the ItemDataBound event:
<asp:ListView ID="Comments" runat="server"
OnItemDataBound="CommentsItemDataBound">
and the method itself:
protected void CommentsItemDataBound(object sender, ListViewItemEventArgs e)
{
var commentItem = (Comment)(((ListViewDataItem)e.Item).DataItem);
var commentControl = (Comment)e.Item.FindControl("CommentControl");
commentControl.CommentItem = commentItem;
}
This way, the binding of each control works as expected.