Template Button GridView - asp.net

I'm using a GridView in my web application and converted the default delete/edit buttons to template fields. I did this because I can't simply delete users from the database with the default SQL delete query that was generated.
I want to use Membership.DeleteUser() however I don't know how to get the username from the GridView that corresponds to the LinkButton that the user presses.
How would I go about doing this?

You can pass value in CommandArgument and get it in the code behind.
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton runat="server" ID="ibtnDelete"
Text="Delete"
CommandName="Delete"
CommandArgument='<%# Eval("UserName") %>'
</ItemTemplate>
</asp:TemplateField>
You can also use DataKeyNames="UserName" in your gridview and get it in code behind.

Related

How to enable a checkbox in Gridview only when it's in the Edit mode?

This is how I have my checkbox column currently:
<asp:TemplateField HeaderText="Hide/Show" ItemStyle-HorizontalAlign="Center" SortExpression="Hide_Show">
<ItemTemplate>
<asp:CheckBox ID="HideShowChk" runat="server" Checked='<%# Bind("Hide_Show") %>' />
</ItemTemplate>
</asp:TemplateField>
I have no code in my code behind because it's not necessary. Basically, a user can check or uncheck it, but it won't be reflected in the database unless I actually go into Edit mode, check or uncheck it, and then hit update so that the changes ARE reflected in the database.
Is there a way to have it so that the checkbox is "disabled" or anything of that sort while it's not in edit mode?
You have to use edititemtemplate for enabling it in the edit mode
<edititemtemplate>
<asp:CheckBox ID="HideShowChk" runat="server" Checked='<%# Bind("Hide_Show") %>' />
</edititemtemplate>
If you still want to use the check box inside the ItemTemplate make it disable using CSS or jQuery.

how to add checked event to checkbox in databound datagrid in asp.net?

I have datagridview with multiple columns in my asp.net website. And I am displaying a backend sql stored procedure output into this grid using page OnLoad event. First column in the grid contains a checkbox. I have added this checkbox through ItemTemplate, so that all rows will have a checkbox for selecting the row. I want user able to select the checkbox and based on this selection I would like to perform a DB operation.
currently i am using like below, but couldn't able to trigger the event.
<asp:GridView ID="resultGridView" runat="server" >
<Columns>
<asp:TemplateField HeaderText="Processed">
<ItemTemplate>
<asp:CheckBox ID="CheckBoxProcess" runat="server" OnCheckedChanged="resultgrid_CellContentClick"
Checked="false" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
on my code behind, I have method resultgrid_CellContentClick() for checkbox selection change event. But this code, never executed when select checkbox on/off.
You didn't set AutoPostBack="true" in your checkbox, that's why your checkbox event handler did not work. Just set it...
<asp:CheckBox ID="CheckBoxProcess" AutoPostBack="true" runat="server"
OnCheckedChanged="resultgrid_CellContentClick" Checked="false" />

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.

How to show insert or cancel buttons instead of links with Detailsview?

How to show insert or cancel buttons instead of links with Detailsview?
You can use the CommandField inside your <fields> tag
<Fields>
<asp:CommandField ButtonType="Button" />
</Fields>
Came across this question while looking for other information on the DetailsView. An alternative approach to that above is to change the commandfield into a template field (from the Edit Fields dialog box for the details view, select the command field entry in the list of selected fields, then click on the "Convert this field into a template field" link). Then, back in your source view, you will see something like this:
<asp:TemplateField HeaderText="Actions">
<InsertItemTemplate>
<asp:ImageButton ID="btnInsertAccept" runat="server" CausesValidation="True" CommandName="Insert" AlternateText="Insert" ImageUrl="images/16x16.check.gif" ToolTip="Click to insert this new division." ValidationGroup="ValidationInsert" />
<asp:ImageButton ID="btnInsertCancel" runat="server" CausesValidation="False" CommandName="Cancel" AlternateText="Cancel" ImageUrl="images/cancel.png" ToolTip="Click to cancel inserting this new division." />
</InsertItemTemplate>
</asp:TemplateField>
Now, there will also be ItemTemplate and EditItemTemplate entries, but you get the idea. Ive changed the default asp:LinkButton controls to ImageButtons, but you could also use normal buttons, whatever fits your UI design. The important part is the CommandName entry on each control - be it insert, Update, Delete, Cancel - it is that command name that causes the appropriate operation to take place.
Hope that give you additional help.

ASP.NET Setting a DetailsView BoundField value from javascript

I'd like to know if it is possible to set a field of a DetailsView BoundField (in edit mode) from javascript? I can't set an id for the boundfield, so obviously can't use document.getElementById() to get to it, but I was wondering if there's another way to do it?
You could try to create an EditItemTemplate, place a TextBox inside and give it an ID of your choice.
Like this:
<Fields>
<asp:TemplateField HeaderText="Column1">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
</Fields>
If there is stuff to be bound on the TextBox's Text property you could use Bind() method.
Good Luck.

Resources