I have a GridView with a BoundField column and quite a few item templates like the following:
<asp:GridView ID="gvUsers" runat="server" AutoGenerateColumns="False" OnRowCommand="gvUsers_RowCommand"
OnRowDataBound="gvUsers_RowDataBound" DataKeyNames="UserId" OnRowEditing="gvUsers_OnRowEditing"
OnRowUpdating="gvUsers_OnRowUpdating" OnRowUpdated="gvUsers_OnRowUpdated"
DataSourceID="DataSource1" Width="807px" Height="105px"
AllowPaging="True" >
<Columns>
<asp:BoundField DataField="UserName" HeaderText="User Name"
SortExpression="UserName" />
<asp:TemplateField HeaderText="Approver">
<ItemTemplate>
<asp:CheckBox ID="cbApprover" runat="server" Enabled="false" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Auditor">
<ItemTemplate>
<asp:CheckBox ID="cbAuditor" runat="server" Enabled="false" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="btnEdit" runat="server"
CommandArgument='<%# Eval("UserName") %>' CommandName="Edit" Text="Edit" />
<asp:Label ID="lblPipe1" runat="server" Text=" | " />
<asp:LinkButton ID="btnUpdate" runat="server"
CommandArgument='<%# Eval("UserName") %>' CommandName="Update" Text="Update" />
<asp:Label ID="lblPipe" runat="server" Text=" | " />
<asp:LinkButton ID="btnDelete" runat="server"
CommandArgument='<%# Eval("UserName") %>' CommandName="Remove"
OnClientClick="return confirm('Are you sure you want to delete this user?');"
Text="Delete" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
While handling the Edit link button I need to use the value in the BoundField, UserName. Unfortunately during the OnRowEditing handler, all strings are empty "". This is also true in the ensuing OnRowDataBound handler for the row in question where e.Row.RowState == DataControlRowState.Edit. Is there a way to obtain this value after the user clicks on the Edit link, and commences the OnRowEditing event?
I arrived at a solution that was a bit more complicated than I was hoping for. I'll show code later but I did the following:
Created a HiddenField to hold the
value of the cell I wanted to edit in
its view state.
In the OnRowDataBound handler, I assigned the value to the
HiddenField.
The ItemTemplate, as seen above, sends the UserName as an
argument. I store this value in
the HiddenField.
The OnRowEditing is fired after the Command handler. That is where I
read the HiddenField.
If anyone has a simpler solution I would love hear about it.
Could it be that you're using late binding in TemplatedItems without EditItemTemplates?
Related
I have one GridView and I have a CSS class called class="icon-p". I want to use this class in my GridView column "Modify", except using ButtonType="Image" and ImageUrl..`
Here is my GridView code:
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False"
CssClass="table" DataKeyNames="id_s" DataSourceID="SqlDataSource1">
<Columns>
..
<asp:CommandField CancelText="cancel" DeleteText="delete" EditText="Update" HeaderText="Modify" ShowEditButton="True" ShowHeader="True" ButtonType="Image"/>
</Columns>
</asp:GridView>
Right click on your GridView in design view then go to edit columns.
Find the commandfield and click the blue hyperlink at the bottom that says "Convert to Template".
Exit back out and go to your markup and you will find that the individual controls are there and you can get at them to set your CssClass etc. The only thing that makes it an update button is the CommandName="Update" attribute.
This article here details the conversion steps that I was trying to explain:
http://peterkellner.net/2009/09/06/detailsview-gridview-aspnet-disable-new-edit-delete-buttons/
You will then be left with something like this:
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" Visible='<%# GetShowEditButton() %>'
CommandName="Edit" Text="Edit"></asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" Visible='<%# GetShowInsertButton() %>'
CommandName="New" Text="New"></asp:LinkButton>
<asp:LinkButton ID="LinkButton3" runat="server" CausesValidation="False"
CommandName="Delete" Text="Delete" Visible='<%# GetShowDeleteButton() %>' ></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
You can then set the CssClass like this for example:
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False"
Visible='<%# GetShowEditButton() %>' CommandName="Edit" Text="Edit"
CssClass="icon-p"></asp:LinkButton>
In my code pasted below the grid has an option to delete the row on click of remove button. Before hitting server side code i want to confirmation from the user to delete the record.
But always server side code is hit rather than the confirm popup showing first.
<asp:GridView ID="grdDelegateList" runat="server" CssClass="gridviewBorder" AutoGenerateColumns="False"
CellPadding="1" Style="margin-left: 0px" BackColor="White" Font-Names="Calibri"
BorderWidth="1px" Width="100%" AllowPaging="True" GridLines="Horizontal" RowHoverBackColor="#666666"
RowHoverForeColor="White" SelectedRowStyle-BackColor="#333333" SelectedRowStyle-ForeColor="White"
PageSize="10" OnPageIndexChanging="grdDelegateList_PageIndexChanging" OnRowCommand="grdDelegateList_RowCommand"
OnRowDataBound="grdDelegateList_RowDataBound" OnRowDeleting="grdDelegateList_RowDeleting">
<Columns>
<asp:BoundField HeaderText="Employee ID" DataField="DelegateID" ItemStyle-HorizontalAlign="Center" HeaderStyle-HorizontalAlign="Center" />
<asp:TemplateField HeaderText="Full Name" ItemStyle-HorizontalAlign="Center" HeaderStyle-HorizontalAlign="Center">
<ItemTemplate>
<p>
<%#DataBinder.Eval(Container.DataItem, "Owner.FirstName")%>
<%#DataBinder.Eval(Container.DataItem, "Owner.LastName")%>
</p>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Remove" ItemStyle-HorizontalAlign="Center" HeaderStyle-HorizontalAlign="Center">
<ItemTemplate>
<span style="cursor: pointer">
<asp:LinkButton ID="ImgRemove" runat="server" CommandName="Delete" CommandArgument='<%# Eval("ID") %>'
Text="Remove" OnClientClick="return confirm(Are you sure you want to remove this Delegate);">
</asp:LinkButton></span>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
What i want to do is show a confirm box if user presses the on button server side code (ie: row command ) event should be hit, other wise on cancel it should do nothing. but its not working.
Your code for the confirm() function is not valid.
confirm() expects a string variable (see window.confirm) yet in your case you are passing in an invalid object and would receive the error
Uncaught SyntaxError: Unexpected identifier
Update your code to be;
<asp:LinkButton
ID="ImgRemove"
runat="server"
CommandName="Delete"
CommandArgument='<%# Eval("ID") %>'
Text="Remove" OnClientClick="return confirm('Are you sure you want to remove this Delegate');">
My suggestion would be to add single quotes to the OnClientClick
OnClientClick="return confirm('Are you sure you want to remove this Delegate');
you can also try the following code:
OnClientClick="if (!confirm('Are you sure you want to remove this Delegate')) return false;"
Try below snippet.
<asp:LinkButton ID="ImgRemove" runat="server" CommandName="Delete" CommandArgument='<%# Eval("ID") %>' Text="Remove" OnClientClick="if (!confirm('Are you sure you want to remove this Delegate?'));">
OnClientClick="if (!confirm('Are you sure you want to remove this Delegate?'));
will show the alert with Yes/No options. If you click on No then nothing will happen. If you click on Yes then corresponding code will execute
Look, it´s simple. I have a GridView which is populated with data from my database.
What I want is put a button in each cell that contains a specific datum.
Pls, look the image below. It describes exactly what I want
In VB.NET, pls! =)
Example Image
Thanks very much!
You are going to need to define the button control inside of the <Columns> section of your GridView markup, like this:
<asp:gridview id="CustomersGridView" runat="server">
<columns>
<asp:boundfield datafield="DateColumn" headertext="Date"/>
<asp:TemplateField>
<HeaderTemplate>
Positive
</HeaderTemplate>
<ItemTemplate>
<asp:Label id="LabelPositive" runat="server" Text='<%# Eval("PositiveColumn")%>' />
<br />
<asp:Button id="ButtonPositive" runat="server" Text="Show" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
Negative
</HeaderTemplate>
<ItemTemplate>
<asp:Label id="LabelNegative" runat="server" Text='<%# Eval("NegativeColumn")%>' />
<br />
<asp:Button id="ButtonNegative" runat="server" Text="Show" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
Neutral
</HeaderTemplate>
<ItemTemplate>
<asp:Label id="LabelNeutral" runat="server" Text='<%# Eval("NeutralColumn")%>' />
<br />
<asp:Button id="ButtonNeutral" runat="server" Text="Show" />
</ItemTemplate>
</asp:TemplateField>
<asp:boundfield datafield="NoCommentsColumn" headertext="No Comments"/>
<asp:boundfield datafield="TotalColumn" headertext="Total"/>
</columns>
</asp:gridview>
Note: The datafield and Eval() calls are bound to made up names like NeutralColumn and NoCommentsColumn, substitute those names with your real database field names.
You need to add while data binding the grid. Look for an event call and handle your code there.
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowdatabound.aspx
I have a delete button in my gridview, but I want this button not to work depending on the result of an sql query.
An example
I have a gridview with "ship containers", and I want to remove one of the container from this list, but I want to display a message "to be able to delete this container, please remove it from the products", so that if a ship container is in use, I need to prevent it from being deleted.
Here is the way you can do this:
<asp:GridView ID="EntityGridView" runat="server" DataKeyNames="DocumentId" AutoGenerateColumns="False"
AllowPaging="True" AllowSorting="False" SkinID="GridViewSmall" OnRowCommand="EntityGridView_RowCommand"
OnPageIndexChanged="EntityGridView_PageIndexChanged">
<Columns>
<asp:TemplateField ItemStyle-CssClass="TemplateFieldThreeColumns">
<ItemTemplate>
<asp:ImageButton ID="ImageButton1" ImageAlign="Top" runat="server" ImageUrl='<% #ResolveImageUrl(Eval("Extension").ToString()) %>'
ToolTip='<%# Eval("Extension").ToString() %>' CommandName="Select" CommandArgument='<%# Eval("DocumentId") %>' />
<asp:ImageButton ID="btnDelete" runat="server" ToolTip="<% $resources:AppResource,Delete %>"
SkinID="DeletePage" OnClientClick="<%# GetDeleteConfirmation(Resources.AppResource.ConfirmDocumentDelete) %>"
CommandName="CustomDelete" CommandArgument='<%# Eval("DocumentId") %>' Visible='<% #AllowDocDelete(Container.DataItem) %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Title" HeaderText="<% $resources:AppResource,Title %>" />
<asp:BoundField DataField="Author" HeaderText="<% $resources:AppResource,Author %>" />
<asp:BoundField DataField="FileName" HeaderText="<% $resources:AppResource,FileName %>" />
<asp:BoundField DataField="Created" HeaderText="<% $resources:AppResource,Created %>" />
</Columns>
<EmptyDataTemplate>
<asp:Label ID="EmptyLabel" runat="server" Text='<%# Resources.AppResource.NoContentToDisplay %>' CssClass="NoDataLabel"></asp:Label>
</EmptyDataTemplate>
</asp:GridView>
Take notice on AllowDocDelete function that disables delete button. This function should be declared inslide your page class, something like this:
public bool AllowDocDelete(object item)
{
bool result = false;
//TODO: check your condition
return result;
}
Item object represents binded entity.
I have a GridView that I use to show my users the result of a search. I want to allow them to choose which columns are shown on the GridView when performing their search. Simple enough, yes? I wanted to try doing this using just databinding, no events. Unfortunately, my code fails to update the GridView using checkboxes bound to the column's Visible property. The state of the chechboxes changes, but the Visible property of the columns does not.
Snippet of Search.aspx:
<myControl:FacultyGridView ID="FacultyGridView1" runat="server" />
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" Text='<%# Eval("HeaderText") %>' Checked='<%# Bind("Visible") %>' AutoPostBack=true/></ItemTemplate>
</asp:Repeater>
Code-behind snippet in Search.aspx.cs:
protected void Page_Load(object sender, EventArgs e)
{
Repeater1.DataSource = FacultyGridView1.GridView.Columns;
Repeater1.DataBind();
}
To be clear, the GridView is exposed as a public property of a user control named FacultyGridView. Relevant snippet of FacultyGridView.ascx:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
AllowPaging="True" AllowSorting="True" PageSize="25">
<PagerSettings Mode="NumericFirstLast" Position="TopAndBottom" />
<Columns>
<asp:BoundField DataField="Name" HeaderText="Name" ReadOnly="True" SortExpression="Name" />
<asp:TemplateField HeaderText="University" SortExpression="UniversityID">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("University.Name") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Division">
<ItemTemplate>
<asp:Repeater ID="Repeater1" runat="server" DataSource='<%# Eval("DivisionMemberships") %>'>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("Division.Name") %>'></asp:Label>
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Title" HeaderText="Title" ReadOnly="True" SortExpression="Title" />
<asp:TemplateField HeaderText="Research Type">
<ItemTemplate>
<asp:Repeater ID="Repeater1" runat="server" DataSource='<%# Eval("ResearchTypeMappings") %>'>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("ResearchType.Name") %>'></asp:Label>
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Expertise" HeaderText="Expertise" ReadOnly="True" SortExpression="Expertise" />
<asp:HyperLinkField DataNavigateUrlFields="Website" DataTextField="Website" HeaderText="Website"
SortExpression="Website" />
<asp:BoundField DataField="Phone" HeaderText="Phone" ReadOnly="True" SortExpression="Phone" />
<asp:TemplateField HeaderText="Email Address" SortExpression="EmailAddress">
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# Eval("EmailAddress", "mailto:{0}") %>'
Text='<%# Eval("EmailAddress") %>'></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Finally, I should mention that the GridView is bound by a Button on the page, but I am not getting updates to the Visible property whether I play with the checkboxes before or after databinding. Furthermore, I have not seen my desired behavior when binding the repeater only on the first Page_Load() using if(!IsPostBack), nor by not using Checkbox.AutoPostback true or false. Any clue as to what I'm doing wrong? I expect it to be something simple, but I'm a bit green here.
As a note: I know how to do this easily with events, but I want to do it with databinding as a learning exercise.
Probably because every time the grid is bound to the data, the column & settings are recreated (with-out your changes).