I am having problem with my repeater's OnItemCommand event.
When I click the Link Button, its not firing.
Am I missing any environment variable
ASPX code
<table>
<!-- repResearchers begin, 0=display name, 1=url -->
<asp:Repeater ID="repExtResearchers" Runat="server" OnItemCommand="deleteResearcher">
<ItemTemplate>
<tr>
<td>
<a href="<%# ((System.String[])Container.DataItem)[1] %>">
<%# ((System.String[])Container.DataItem)[0] %></a>
</td>
<td>
<asp:LinkButton ID="lbDelete" runat="server" CommandName="del"
CommandArgument = "<%# ((System.String[])Container.DataItem)[1]%>"
OnClientClick="if (!confirm('Are you sure do you want to delelte it?')) return false;">Delete</asp:LinkButton>
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
</table>
CS
protected void deleteResearcher(object sender, RepeaterCommandEventArgs e)
{
string a;
lblError.Text = e.CommandArgument.ToString();
lblError.Visible = true;
}
Make sure you dont rebind the repeater at every postback.
If (Page.IsPostBack)
return;
repExtResearchers.DataSource = ...
repExtResearchers.DataBind();
Hope that helps.
I'm sure - as this is an EXTREMELY old question - that this has been answered already, but for people who may be running into what I was running into...
If you're using any of the Ajax Controls, they all require a validation group. I had a really long page that I was trying to shorten by doing this, so I wasn't noticing that the ajax controls from the Ajax Control Toolkit were throwing errors and not validating. I set the LinkButton's validation group to something that was nowhere anywhere and it started firing.
Hopefully, that helps someone out.
It won't fix your ptoblem but change
OnClientClick="if (!confirm('Are you sure do you want to delelte it?')) return false;"
to
OnClientClick="return confirm('Are you sure do you want to delelte it?')"
Your code is using a double negative to confirm a positive.
I had this issue using OnCommand in a LinkButton and I had an empty href="". When I removed the extra attribute, it posted back.
Related
I'm trying to find a linkbutton inside on my aspx page.
I don't want to find the originator, like the specific sender, I just want to find the overall linkbutton in the itemtemplate and make them all invisible.
Here is the ASPX code where the linkbutton is
<ItemTemplate>
<table>
<tr>
<td>
<asp:TextBox ID="DisplayOP" runat="server" Text="test" Visible="false"></asp:TextBox>
</td>
</tr>
<tr>
<td><asp:Linkbutton ID="ItemEmneLabel" runat="server" Text='<%# Eval("Emne") %>' OnClick="ItemShowComments" CommandArgument='<%# Eval("OpslagsID") %>' Visible="true" /></td>
<--- Code Omitted --->
In my code behind .cs file I try to find it like this:
var lnkb = (LinkButton)ListView1.FindControl("ItemEmneLabel");
lnkb.Visible = false;
and this:
var lnkb = (LinkButton)this.ListView1.FindControl("ItemEmneLabel");
lnkb.Visible = false;
However whatever I try I get an error saying:
{"Object reference not set to an instance of an object."}
What am I doing wrong here?
it's impossible to find a control declared in an ItemTemplate like this.
You can only get the control with sender in ItemShowComments method.
Okay, so I found the solution to my problem.
Now I wanted to disable the visibility of the linkbuttons, when the listview was showing specific data. I kept looking for the answer in code behind, but found out that I could solve it in aspx.
What I did was add a statement to visibility like this:
Visible='<%# !Eval("Emne").Equals("") %>'
The exclamationmark (!) in front of Eval means, if it is NOT equal to "". This means that if "Emne" is not-empty, then the statement is true, thus visible is true. I could do it the other way around by coding it like this:
Visible='<%# Eval("Emne").Equals("") %>'
I feel like this is a very simple solution that I am overlooking somehow. Basically I have a web page (using asp.net webforms) with a datalist. Inside my datalist, and inside the datalist items I have a div that I want the user to be able to click to cause a page redirect. I would like to do this without use of an anchor or javascript if possible. My datalist:
<asp:DataList ID="DataList1" ..etc >
<HeaderTemplate>
..etc
</HeaderTemplate>
<ItemTemplate>
<table >
<tr >
<td>..etc</td>
<td>..etc</td>
<td ><div id="clr_div" runat = "server"></div></td> <-- this is the div to be clicked
</tr>
</table>
</ItemTemplate>
And then in my code behind
protected void on_item_databound(object sender, DataListItemEventArgs e)
{
System.Web.UI.HtmlControls.HtmlGenericControl div = (System.Web.UI.HtmlControls.HtmlGenericControl)e.Item.FindControl("clr_div");
div.Attributes.Add("onclick", "**What goes here??**");
}
I have tried to put in the 'what goes here' section:
window.location.href = \"default.aspx\" **doesn`t work, the quotes don`t render properly in html
window.location.href = default.aspx **doesn`t work
return Response.Redirect(default.aspx); **doesn`t work
Any help is welcome. Thanks
The answers listed should help you out once you fix your div issues. I have experienced event firing issues in non-fixed divs when using static positioning, so you might want to try specifying position:relative on your containing div in question if you have fixed divs elsewhere.
Difference between static and relative positioning
Also, TMTOWTDI. If you have an empty div, you could just also turn it into a button for an insignificant markup cost
as explained here
Just add an ItemCommand event to your datalist
<asp:DataList ID="DataList1" onitemcommand="on_item_Command" ..etc..
and change this
<div id="clr_div" runat = "server"></div>
to this
<asp:Button id="clr_div" CommandName="nav-to-page" runat="server" CommandArgument='<%# Eval("field to eval if this came from a datasource") %>' />
and in code behind
protected void on_item_Command(object source, DataListCommandEventArgs e)
{
if (e.CommandName == "nav-to-page")
{
string s = (string)e.CommandArgument;
Response.Redirect("NewPage.aspx?" + s);
}
}
I have two buttons inside of the repeater and I'm trying to access them from code behind, but they are not working. I receive the following error:
Invalid postback or callback argument. Event validation is enabled using in configuration or <%# Page EnableEventValidation="true" %> in a page. For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them. If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation.
I don't understand what does the error mean nor where is coming from. Here is my code:
<asp:Repeater ID="Repeater1" runat="server"
onitemdatabound="Repeater1_ItemDataBound1"
onitemcommand="Repeater1_ItemCommand" >
<ItemTemplate>
<table>
<tr>
<td><asp:Image ID="Image1" runat="server" /></td>
<td><asp:Label ID="empnamelbl" runat="server" Text='<%# Eval("fullname") %>'></asp:Label></td>
<td><asp:DropDownList ID="DropDownList1" runat="server">
</asp:DropDownList></td>
<td> <asp:Button ID="Button1" runat="server" Text="Present" CommandName="test1" CommandArgument='<%# Eval("ID") %>' /> </td>
<td><asp:Button ID="Button2" runat="server" Text="Absent" CommandName="test2" CommandArgument='<%# Eval("ID") %>' /> </td>
</tr>
</table>
</ItemTemplate>
</asp:Repeater>
Code behind:
protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.CommandName == "test1")
{
Response.Write("hi");
}
}
I was able to solve it by using, UseSubmitBehavior="false", property in the buttons.
Here is it:
<asp:Button ID="Button2" UseSubmitBehavior="false" runat="server" Text="Absent" CommandName="test2" CommandArgument='<%# Eval("ID") %>' />
Try setting EnableViewState="false" for the repeater. Definitely there will be some drawbacks but this has worked for me.
Else below are some more approaches you can try on:
If you are setting your Repeater's DataSource in Page_Load event.
Try wrapping your Page_Load code inside if(!IsPostBack) .
protected void Page_Load( object sender , eventArgs e)
{
if(!IsPostBack)
{
// bind repeater , dropdownlist items ... here
}
}
Also from your error message it seems that enableEventValidation=“true” setting is present either in your Page as:
<%# Page EnableEventValidation="true" ... %>
OR in your web.config :
<system.web>
<pages enableEventValidation="true"/>
</system.web>
Some suggestions:
Disable eventvalidation. Easy to do but less secure .THIS IS NOT RECOMMENDED. Also read here an extremely good article on this issue: http://odetocode.com/blogs/scott/archive/2006/03/22/asp-net-event-validation-and-invalid-callback-or-postback-argument-again.aspx
If any client side method is used to set changes in Controls, It's better to use the
postback and add or remove the items server-side. Example: if a list control includes options numbered 1, 2, or 3 when the page is rendered, and if a postback request is received specifying option number 4, ASP.NET raises an exception.
This MSDN reference. is good too.
I hope this helps.
due to presence of Image that run at server .. try to use img tag instead
I know there are a lot of related posts or articles about this, but then it seems that they're not helping my case. I've even compared with a working sample at this site, http://www.ezineasp.net/post/ASP-Net-LinkButton-Command-Event.aspx, I don't think there's much difference. I thought my code should be working but apparently it just won't. I'm so sorry if this looks like a duplicate, but it's my last resort to post here.
Here's my HTML:
<asp:ListView runat="server" ID="AppsList">
<LayoutTemplate>
<div>
<asp:PlaceHolder runat="server" ID="itemPlaceHolder" />
</div>
</LayoutTemplate>
<ItemTemplate>
<div class="applist">
<div class="app">
<asp:ImageButton ID="imgbtnApp" runat="server" ImageUrl='<%#Eval("Icon") %>' height="100" width="100" CommandName="Select" CommandArgument='<%# Eval("ID") %>' OnCommand="AppsList_ItemCommand" />
</div>
<div class="appname">
<asp:LinkButton ID="linkbtnAppName" runat="server" CommandName="Select" ForeColor="#333333" CommandArgument='<%# Eval("ID") %>' OnCommand="AppsList_ItemCommand" CssClass="linkbtnAppName"><%# Eval("AppName") %></asp:LinkButton>
</div>
</div>
</ItemTemplate>
<EmptyDataTemplate>
Sorry - Nothing found.
</EmptyDataTemplate>
</asp:ListView>
Code:
protected void AppsList_ItemCommand(object sender, CommandEventArgs e)
{
if (e.CommandName == "Select")
{
txtTest.Text = e.CommandArgument.ToString();
}
}
What I'm trying to achieve here is to capture the ID of the item in the ListView into the textbox when I click on either the Image Button or Link Button. Both will perform the same thing. I already got the Image button to work. When I click the Image, the ID, e.g 1 will appear in the textbox. But when I want to do the same thing with the Link Button, nothing will happen. The event is not being triggered in any way.
I've seen some posts talking about repeaters or AJAX to do the same thing, but I was just wondering why can't this code work. I would appreciate any pointer.
For my own experience, I have wrongly set ViewStateMode="Disabled" in page directive.
Because of this when post back occur upon clicking LinkButton inside the List View, existing data are vanished. So, will not reach to LinkButton's OnCommand event and never fire.
Once ViewStateMode attribute is removed, everything working well.
I noticed that your eventArgs should ListViewCommandEventArgs instead of CommandEventArgs and you should also bind the itemCommand event to your aspx page as shown below.
ASPX:
<asp:ListView runat="server" ID="AppsList" OnItemCommand="AppsList_OnItemCommand">
Code Behind:
protected void AppsList_OnItemCommand(object sender, ListViewCommandEventArgs e)
{
if (String.Equals(e.CommandName, "Select"))
{
}
}
UPDATE:
Also remove the OnCommand attribute from image button and Link button OnCommand="AppsList_ItemCommand" , and each CommandName should be different than other.
<div class="applist">
<div class="app">
<asp:ImageButton ID="imgbtnApp" runat="server" ImageUrl='<%#Eval("Icon") %>' height="100" width="100" CommandName="Select" CommandArgument='<%# Eval("ID") %>' />
</div>
<div class="appname">
<asp:LinkButton ID="linkbtnAppName" runat="server" CommandName="Select" ForeColor="#333333" CommandArgument='<%# Eval("ID") %>' CssClass="linkbtnAppName"><%# Eval("AppName") %></asp:LinkButton>
</div>
</div>
</ItemTemplate>
Well, in case someone is wondering why this is happening, this is what I've found out after spending a great deal of time ripping off my codes to see what's wrong with it. I doubt that someone will bump into this issue, but to heck with it, sharing is caring.
Apparently the search button I've placed in my master page is the reason behind this. I've assigned its ID as "submit", which is the thing that is stopping my linkbutton from working, which I have no idea why that is happening. The search function is merely a search engine allowing users to look for the keywords in the webpage. The linkbutton's OnCommmand event just won't trigger when the ID to the search button is assigned as "submit".
When I change it to "submit1", everything works as normal. I'm still a newbie in this asp.net thing, can anyone tell me why is this even affecting the linkbutton? Anyway, the OnCommand event is now working properly.
I had a similar issue with LinkButtons. I don't remember how I figured this out, but I must of got fustrated and started clicking the LinkButton a few times fairly quickly and I noticed that the Command event did fire once in awhile. Soon I figured it's if I clicked on the LinkButton before the page fully loads that the command would fire correctly. If the page fully loaded then it seems something was hijacking that command on the LinkButton.
Without success, from one of the other suggestions I searched for any button that might be using the name/id "submit". There wasn't one on my page.
With success, I tried using ASP.Net Buttons instead of LinkButtons. That seemed to fix my issue. I'm not entirely sure what caused this situation but Buttons seem to work in my case so I'll stick with that.
This is a weird issue. I have a List view with 2 Link buttons. "Edit" and "Delete"
Iam able to attach an event handler for the first linkbutton(Update). Code in the event handler executed fine. But If I try to attach a event handler for the second link button(Delete) , I get an error.
My Item Template Looks like this.
<ItemTemplate>
<tr>
<td>
<asp:Label ID="MessageLabel" runat="server" Text='<%# Eval("Item") %>' />
</td>
<td>
<asp:Label ID="URLLabel" runat="server" Text='<%# Eval("URL") %>' />
</td>
<td>
<asp:LinkButton ID="EditLinkButton" runat="server" OnClick="EditLinkButtonClicked"
CommandArgument='<%# Eval("ItemID") %>'> Edit</asp:LinkButton>
</td>
<td>
<asp:LinkButton ID="DeleteLinkButton" runat="server" OnClick="DeleteLinkButtonClicked"
CommandArgument='<%# Eval("ItemID") %>'>Delete</asp:LinkButton>
</td>
</tr>
</ItemTemplate>
The Event handler declared in the codebehind file are
public void EditLinkButtonClicked(object sender, EventArgs e)
{
-----
}
public void DeleteLinkButtonClicked(object sender, EventArgs e)
{
-----
}
[Exactly same]
First Item works absolutely fine. But If I attach the second handler, I get the following error
Am I missing some thing ?
Note - There is no error if I try to attach the 1st event handler to the second link button.[ie EditLinkButtonClicked to DeleteLinkButtonClicked ]
Issue occurs only when I try to attach DeleteLinkButtonClicked to DeleteLinkButton
Any help? Thanks in Advance
Try to disable EnableEventValidation for the Page directive and check it.
<%# Page EnableEventValidation="false" %>
If it help then you should to know that:
This feature reduces the risk of
unauthorized or malicious postback
requests and callbacks. It is strongly
recommended that you do not disable
event validation.
regarding to http://msdn.microsoft.com/en-us/library/system.web.ui.page.enableeventvalidation.aspx
and try to avoid this in the way that was suggested here.
It'd be three things:
You need to clean, rebuild solution, and reset IIS or close ASP.NET development server (maybe some cache is preventing you to execute the most recent version of the code-behind class).
Typo. Double-check that...!
Event handler hasn't the right signature.
Anyway, why don't you use the "Command" event?
You can do OnCommand="Item_Command" and the event handler will have CommandEventArgs which provides you the CommandName and CommandArgument, so you can switch the command name and invoke the logic for editing or deleting respectively.
Read more here:
http://msdn.microsoft.com/es-es/library/system.web.ui.webcontrols.commandeventargs_members.aspx
The same problem occurs in my code. I resolved by using CausesValidation="false"
<asp:LinkButton ID="YourID" runat="server" **CausesValidation="false"** OnClick="Category_btn_Click1" />