Gridview buttonfield works LinkButton doesn't - asp.net

I've been fighting this problem for many hours now and could really use some help :-)
This is the grid
<asp:GridView ID="annonceView" runat="server" AutoGenerateColumns="False" DataKeyNames="Id" DataSourceID="dataSourceAnnoncer">
<Columns>
<asp:BoundField DataField="Productname" HeaderText="Productname" />
<asp:buttonfield buttontype="Link" commandname="Delete" text="Delete"/>
<asp:TemplateField HeaderText="Administration">
<ItemTemplate>
<asp:LinkButton ID="lnkBtnDelete" runat="server" Text="Delete" CausesValidation="False" CommandName="Delete" OnClientClick="return confirm('Delete?')" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:LinqDataSource ID="dataSourceAnnoncer" runat="server" ContextTypeName="Namespcae.TheContext"
EnableDelete="True" TableName="Annoncer">
</asp:LinqDataSource>
Clicking the buttonfield deletes the record just fine. Clicking the LinkButton doesn't work. I get a postback and the grid is shown as empty and no record is deleted. Seems like an empty databinding.
I have tried to create a custom OnClick, OnCommand event for the LinkButton, but neither are fired. The OnRowCommand isn't fired either.
I don't manually DataBind in the codebehind.

The problem wasn't with Asp.net but with Sitecore (A CMS), the simple solution is described here, http://www.cassidy.dk/blog/sitecore/2009/01/typesthatshouldnotbeexpanded.html.
I feel like Sitecore has stolen several hours of my life, well the problem is solved, so I'm happy :-)

Your code looks fine and should work. Make sure that you aren't using the lnkBtnDelete Id somewhere else. Do you have both the buttonField and the TemplateField present at the same time? What happens if you remove the buttonfield?

Related

Enable a LinkButton nested inside a disabled GridView

I have a Gridview that has Enabled="False" (it contains many controls and in this circumstance i need them all to be disabled, easiest way is to set Enabled="False" on the Gridview).
The issue is I want to enable one LinkButton on each row within the disabled Gridview. I've tried finding the LinkButton on RowDataBound and enabling it there but it's still disabled. Can this be done? Can you have an enabled Button within a disabled GridView?
Simplified example of the code (real one is thousands of lines long)
<asp:GridView runat="server" enabled="false" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton runat="server" id="button-to-be-enabled" text="press me"/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

Dynamic Data GridView Pager missing DynamicHyperlink Edit parameters

Edit: Feb 7, 2012 - Turns out the key parameter disappears from the GridView Edit link after a Sort as well, so it doesn't appear to be the Pager after all, but the problem persists... any ideas very welcome.
I have a Asp.Net Dynamic Data app. It uses the standard GridViewPager.aspx in Custom List.aspx which is marked up like this:
<asp:GridView ID="GridView1" runat="server" DataSourceID="GridDataSource" EnablePersistedSelection="True" CssClass="DDGridView" HeaderStyle-CssClass="th" RowStyle-CssClass="td" CellPadding="6" AllowSorting="True" AllowPaging="True" PageSize="3"
OnRowDataBound="GridView_OnRowDataBound" OnRowDeleting="GridView_OnRowDeleting">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:LoginView ID="LoginView1" runat="server">
<RoleGroups>
<asp:RoleGroup Roles="admin">
<ContentTemplate>
<asp:DynamicHyperLink ID="EditLink" runat="server" Action="Edit" Text="edit" />
<asp:LinkButton ID="LinkButton1" runat="server" CommandName="Delete" Text="delete" OnClientClick='return confirm("Are you sure you want to delete this item?");' />
</ContentTemplate>
</asp:RoleGroup>
</RoleGroups>
</asp:LoginView>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<PagerStyle CssClass="DDFooter" />
<PagerTemplate>
<asp:GridViewPager runat="server" />
</PagerTemplate>
<EmptyDataTemplate>
There are currently no items in this table.
</EmptyDataTemplate>
</asp:GridView>
On page 1 of the List all is well. The Edit Link rendered includes ?key=xxxxx and the Edit form opens to the correct record.
Once we page off of Page 1 however, the Edit Link rendered on each row has no 'key' parameter and the Edit form always opens to the first row in the database.
I am at a loss to explain this or even where to look. There is no custom code attached to any event associated with the Edit DynamicHyperlink or the GridviewPager.
Has anyone experienced this or have any suggestions as to what might be the issue?
Found the answer here:
LinkButton CommandArgument is empty when it is inside LoginView in a GridView
Turns out good 'ol Microsoft forgot to wire LoginView to fire row-level databind events inside a GridView. No databind, no link parameters!
In deference to a positive attitude, I'll make no comment on the level of organization it takes to allow that out the door....

Asp.net 2.0 Gridview edit

i have used grid view to display data. Now i need to edit the row. I have kept edit link button in template field of grid-view. Now when i click the edit button, i need to retrieve the data for particular row into the server controls, so that user can enter the data into it.
How can i do that?
let me know if any info required..
thanks!
UPDATED
See http://img18.imageshack.us/i/editform.jpg/
Now, when i click edit from below grid, the data in grid should come up in above form.
There is a different template available in Gridview and you have to use it properly. For example, if you want to edit something, the editTemplate is available for that.. look at the following sample:
<asp:GridView runat="server" ID="grd">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Label runat="server" ID="lbl"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID="Textbox1"></asp:TextBox>
<asp:HiddenField runat="server" ID="hdf" />
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox runat="server" ID="Textbox1"></asp:TextBox>
<asp:HiddenField runat="server" ID="hdf" />
</InsertItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
have a look on these article
http://www.asp.net/data-access/tutorials/using-templatefields-in-the-gridview-control-cs
http://programming.top54u.com/post/ASP-Net-GridView-Edit-ItemTemplate-Mode.aspx
This is such a general question that you should really review how the GridView works in the first place.
Try reviewing the following example from MSDN regarding GridView editting;
http://msdn.microsoft.com/en-us/library/ms972948.aspx

Can we avoid postback in gridView...when user click a button in same?

I have a gridview ...in my dotnet 2.0 - web application.
gridview has 2 columns...first column is ID..and second is a "link button"..as shown below.
On click of this link, I have to display a popup..and I'm using ajax ModalPopupExtender.
I am able to see the popup.
But the issue is...when I click on this link button the postback happens...and the gridview.datasource is NULL. So I have to fetch the data from database and bind the gridview again.
Please help me to find a way to avoid this.
<asp:GridView ID="AvailableGridView" >
<Columns>
<asp:BoundField ItemStyle-CssClass="ItemStyleCss" ItemStyle-HorizontalAlign="Center"
HeaderStyle-CssClass="GridHeaderRow" HeaderText="ID" DataField="ClaimCodeGroupIdText"
/>
<asp:TemplateField HeaderText="View" ShowHeader="False"> <ItemTemplate>
<asp:LinkButton CssClass="localNavigation" ID="ViewCCGLinkButton"
Text="View" CommandName="View" CommandArgument='<%# Eval("ClaimCodeGroupId") %>' runat="server"></asp:LinkButton>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" />
<HeaderStyle CssClass="GridHeaderRow" />
</asp:TemplateField>
</Columns>
<asp:GridView />
You can set ajax toolkit modal popup to load client side and not cause a postback.
But if you have to get data from the server side, i think you have following options
Use ajax to load server data
Save the selected Id in the hidden text box and on postback read Id thru hidden textbox
Use jQuery BlockUI and ajax to load server data (personal fav.)
Pick your options according to ur need and requirement.

ASP.NET how best to add a delete confirmation on a gridview with EntityDataSource

I have a .NET4 web application using the Entity Framework
In one of my pages I have a gridview bound to an entity data source. Within the Gridview definition I have
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
and the EntityDataSource is defined
<asp:EntityDataSource ID="eds_timesheets" runat="server" ConnectionString="name=TIME_ENTRY_DB"
DefaultContainerName="TIME_ENTRY_DB" EnableDelete="True" EnableFlattening="False"
EnableUpdate="True" EntitySetName="TIMESHEETs" Include="USER, ACTIVITY, PROJECT"
EntityTypeFilter="TIMESHEET">
</asp:EntityDataSource>
Everything works as expected, however I now want to put a confirmation of delete in place in case of accidental pressing.
I have tried placing code on the row command of the gridview wwhich would register a javascript alert window however it appears that at this point the EntityDataSource has already carried out its delete.
There is no OnClientClick for a gridview command field to place a small javascript snippet.
Has anyone encoutnered and subsequently solved this issue? Is it easier to have a link button and handle the delete of the Entity data source myself?
Use a TemplateField, and then put a Button in it with the CommandName="Delete". Then you can use the OnClientClick property to call your javascript confirmation.
Something like the following (can use Button, ImageButton, or LinkButton):
<asp:TemplateField>
<ItemTemplate>
<asp:Button id="DeleteButton" runat="server" text="Delete"
CommandName="Delete" OnClientClick="return confirm('Are you sure?');" ></asp:Button>
</ItemTemplate>
</asp:TemplateField>
Of course, you would not want to show the delete button in the CommandField.

Resources