"Update/Cancel" buttons don't appear in TemplateField Edit button - asp.net

When you create an edit button in every row of a Gridview using CommandField it displays update/cancel buttons after clicking, so you can accept/cancel changes.
However, I want an edit button that has tooltip text, and since CommandField doesn't have tooltip property, i used TemplateField. It worked with the delete button, but I'm having problems with the edit button:
<asp:GridView ID="GridView1" runat="server"
AllowPaging="True" AllowSorting="True"
DataMember="DefaultView"
DataSourceID="SqlDataSource1" AutoGenerateColumns="False"
DataKeyNames=FIELD,FIELD,FIELD" CellPadding="4" ForeColor="#333333" Width="90%"
Height="90%" Font-Size="Small">
<RowStyle BackColor="#EFF3FB" />
<Columns>
<asp:BoundField DataField="FIELD" HeaderText="FIELD" ReadOnly="True"
SortExpression="FIELD" />
<asp:BoundField DataField="FIELD" HeaderText="FIELD"
SortExpression="FIELD" />
<asp:BoundField DataField="FIELD" HeaderText="FIELD"
SortExpression="FIELD" />
<asp:BoundField DataField="FIELD" HeaderText="FIELD" ReadOnly="True"
SortExpression="FIELD" />
<asp:BoundField DataField="FIELD" HeaderText="FIELD" ReadOnly="True"
SortExpression="FIELD" />
<asp:BoundField DataField="FIELD" HeaderText="FIELD"
SortExpression="FIELD" />
<asp:CommandField ButtonType="Image" Visible="true" EditText="Edit" ShowEditButton="True" EditImageUrl="images/pencil1.png"></asp:CommandField>
<asp:TemplateField >
<ItemTemplate>
<asp:ImageButton ID="deleteButton" runat="server" CommandName="Delete" Text="Delete"
OnClientClick="return confirm('¿Are you sure?');" ToolTip="delete" ImageUrl="images/DeleteRed1.png" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#2461BF" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:DBUserInterfaceConnectionString %>"
SelectCommand="SELECT ... FROM ... INNER JOIN ... ON ..."
DeleteCommand="DELETE FROM ... WHERE ...=#param;"
UpdateCommand="UPDATE ... SET ... = #param, ... = #param2 WHERE ... = #param3 and ... = #param4 and ... = #param5;"
>
</asp:SqlDataSource>
As I said before, I replaced CommandField with:
<asp:TemplateField >
<ItemTemplate>
<asp:ImageButton ID="editButton" runat="server" CommandName="Edit" Text="Edit" ToolTip="Edit" ImageUrl="images/pincel1.png" />
</ItemTemplate>
</asp:TemplateField >
but "Update/Cancel" buttons don't appear, so I can't update/edit anything. Why does it happen?
Any Ideas to implement a succesful edit button?
NOTES:
Both buttons don't have vb code behind, for some reason delete button works just with DeleteCommand in the SqlDataSource, and if I try to delete the command, it prompts error because no DeleteCommand is specified.
UpdateCommand has no purpose, it can be deleted. I could use it for a update button instead of an edit button, but when i tried, it says #params are not known, that's why I decided to use edit button instead.

The <asp:TemplateField> is used when you want to set your own-defined i.e. User-Defined content for each item in the GridView control.
The <asp:CommandField> is used when you want to use pre-defined command buttons to perform select, edit, or delete operations. Check MSDN here.
So, when your are using your own user-defined way for edit button, you also need to specify your custom content way for Update & Cancel button inside <EditItemTemplate> as :
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton ID="editButton" runat="server" CommandName="Edit" Text="Edit"
ToolTip="Edit" ImageUrl="images/pincel1.png" />
</ItemTemplate>
<EditItemTemplate>
<asp:ImageButton ID="BtnUpdate" runat="server" CommandName="Update" Text="Update"
OnClick="BtnUpdate_Click" ImageUrl="images/Update.png"/>
<asp:ImageButton ID="BtnCancel" runat="server" CommandName="Cancel" Text="Cancel"
OnClick="BtnCancel_Click" ImageUrl="images/Cancel.png"/>
</EditItemTemplate>
</asp:TemplateField>
And just make sure, Only if you are again providing your Custom Implementation for Update & Cancel logic, you also define the onclick events for these two Update and Cancel buttons. Else remove the OnClick from markup of these buttons.
[ BtnUpdate_Click & BtnCancel_Click here.]

I think since you've converted it to a TemplateField, all of the automatically-functioning stuff (like Update/Cancel buttons) has been disabled. I'm betting you'll need to add an <EditItemTemplate> with the Update and Cancel buttons, and hook them to the relevant commands using CommandName.

Related

ASP.net Gridview Row Deletion

I am using Visual Studio 2015 and Entity Framework 6. I have a gridview displaying orders from a database. However, I need a user to be able to click a row and be taken to another page for editing that row after a dialog box confirmation.
This is what I have:
<asp:GridView ID="gridOrders" runat="server" Height="184px" Width="1359px" AutoGenerateColumns="false"
AllowSorting="true" >
<HeaderStyle Font-Bold="true" Font-Size="16pt" BackColor="#cc0000" ForeColor="Black" />
<RowStyle Font-Size="12pt" BackColor="#afadad" ForeColor="White"/>
<AlternatingRowStyle BackColor="#afadad" ForeColor="White" />
<Columns>
<asp:CommandField HeaderText="" SelectText="CANCEL ORDER" ShowSelectButton="true" ControlStyle-ForeColor="White" />
<asp:BoundField HeaderText="First Name" DataField="FirstName" SortExpression="FirstName" />
How do I make the row selection to another page happen with a dialog that asks user if they are sure?
Change your aspx page with the below code
<asp:GridView ID="gridOrders" runat="server" Height="184px" Width="1359px" AutoGenerateColumns="False"
AllowSorting="True">
<HeaderStyle Font-Bold="true" Font-Size="16pt" BackColor="#cc0000" ForeColor="Black" />
<RowStyle Font-Size="12pt" BackColor="#afadad" ForeColor="White" />
<AlternatingRowStyle BackColor="#afadad" ForeColor="White" />
<Columns>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="lnkCancelOrder" runat="server" OnClientClick="return confirm('Are you sure to redirect?')" OnClick="lnkCancelOrder_Click" CausesValidation="False" CommandArgument='<%#Eval("OrderID") %>' CommandName="Select" Text="CANCEL ORDER"></asp:LinkButton>
</ItemTemplate>
<ControlStyle ForeColor="White" />
</asp:TemplateField>
<asp:BoundField HeaderText="First Name" DataField="FirstName" SortExpression="FirstName" />
</Columns>
</asp:GridView>
Write the c# code as follows
You can redirect to another page and pass the orderID as QueryString, and retrieve the complete order information by orderID and show that in an edit mode form
protected void lnkCancelOrder_Click(object sender, EventArgs e)
{
LinkButton lnk = sender as LinkButton;
string orderID = lnk.CommandArgument;
Response.Redirect("AnotherPage.aspx?orderId="+orderID);
}
Write under the <asp:TemplateField> of Gridview as
<asp:LinkButton ID="anchrTag" runat="server" PostBackUrl="Your edit page url" OnClientClick="return confirm('Are u sure to leave this page and want to go for edit?');">Edit</asp:LinkButton>

Send email using gridview checked employees to them in asp.net vb

Good day
I have grid view with employee data displaying in it.
<asp:TabContainer ID="tcmanagers" runat="server" ActiveTabIndex="0"
Width="100%">
<asp:TabPanel ID="TabAdmins" runat="server" HeaderText="Admins" TabIndex="3">
<ContentTemplate>
<asp:Label ID="Label4" runat="server" Font-Bold="False" Font-Italic="False" CssClass="style3"
ForeColor="Blue" Width="290px">Select Admins to send:</asp:Label>
<asp:Button ID="Sendadmins" runat="server" Text="Send Email" Width="201px"/>
<br />
<br />
<asp:GridView ID="GridView3" runat="server" AutoGenerateColumns="False" DataKeyNames="Employee_ID"
DataSourceID="SqlDataSource3" Width="633px" CssClass="grd">
<Columns>
<asp:BoundField DataField="Employee_ID" HeaderText="Employee_ID" ReadOnly="True"
SortExpression="Employee_ID" Visible="False" />
<asp:BoundField DataField="Employee_Number" HeaderText="Employee Number" SortExpression="Employee_Number">
<ItemStyle Width="70px" Wrap="False" />
</asp:BoundField>
<asp:BoundField DataField="Employee_Surname" HeaderText="Surname" SortExpression="Employee_Surname">
<ItemStyle Wrap="False" />
</asp:BoundField>
<asp:BoundField DataField="Employee_Name" HeaderText="Name" SortExpression="Employee_Name">
<ItemStyle Wrap="False" />
</asp:BoundField>
<asp:BoundField DataField="Employee_Job_Title" HeaderText="Job Title" SortExpression="Employee_Job_Title">
<ItemStyle Wrap="False" />
</asp:BoundField>
<asp:BoundField DataField="Employee_Dept" HeaderText="Department" SortExpression="Employee_Dept">
<ItemStyle Wrap="False" />
</asp:BoundField>
<asp:BoundField DataField="Employee_EMail" HeaderText="Email Address" SortExpression="Employee_EMail">
<ItemStyle Wrap="False" />
</asp:BoundField>
<asp:TemplateField HeaderText="Check All"><HeaderTemplate>
<asp:CheckBox ID="chkAlladmin" runat="server" AutoPostBack="True" OnCheckedChanged="chkAllAdmin_CheckedChanged" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="chkAdmin" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns><RowStyle Font-Bold="False" Font-Italic="False" Font-Names="Arial" Font-Size="10pt" />
<EditRowStyle Font-Names="Arial" Font-Size="10pt" /><HeaderStyle Font-Bold="False" Font-Italic="False" Font-Names="Arial" Font-Size="10pt"
Font-Underline="False" CssClass="grdhead" HorizontalAlign="Center" />
<AlternatingRowStyle CssClass="grdalt" />
<SelectedRowStyle BackColor="Fuchsia" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:ApplicationServices %>"
ProviderName="<%$ ConnectionStrings:ApplicationServices.ProviderName %>" SelectCommand="SELECT dbo.lms_Employee.Employee_ID,dbo.lms_Employee.Employee_EMail,dbo.lms_Employee.Employee_Number, dbo.lms_Employee.Employee_Surname, dbo.lms_Employee.Employee_Name, dbo.lms_Employee.Employee_Job_Title,dbo.lms_Employee.Employee_Dept FROM dbo.aspnet_Membership INNER JOIN dbo.aspnet_Users ON dbo.aspnet_Membership.UserId = dbo.aspnet_Users.UserId INNER JOIN dbo.aspnet_UsersInRoles ON dbo.aspnet_Users.UserId = dbo.aspnet_UsersInRoles.UserId INNER JOIN dbo.lms_Employee ON dbo.aspnet_Users.UserName = dbo.lms_Employee.Employee_UserName WHERE (dbo.lms_Employee.Employee_Active = 'True') AND (dbo.aspnet_UsersInRoles.RoleId = '9ec9b26d-64a1-4736-8e82-fe6de7c7caac') and Employee_Number <> '100'">
</asp:SqlDataSource>
</ContentTemplate>
</asp:TabPanel>
</asp:TabContainer>
Now I can select all data in it or one at a time with code below.
Protected Sub chkAllAdmin_CheckedChanged(sender As Object, e As EventArgs)
For Each gr As GridViewRow In GridView3.Rows
Dim cb As CheckBox = DirectCast(gr.FindControl("chkAdmin"), CheckBox)
If DirectCast(sender, CheckBox).Checked Then
cb.Checked = True
Else
cb.Checked = False
End If
Next
End Sub
What I want to do is send a email to all the selected employees. I have an iis server and sql server. So my database is on sql server and my web application is on iis server.
I have no idea how to approached this any help will be appreciated.
Without coding it for you, here's the strategy I use for this type of task:
Convert the BoundField items to TemplateFields. When you do this, you'll likely have controls with meaningful names inside the ItemTemplate for each field, like "lblEmployeeEmail"
On your button-click, loop through the items in the GridView and for each row, use the FindControl() method to find the CheckBox (and then check if it's checked) and use the FindControl() also to get any other fields you might need. (The email label, etc.)
Here's the generic code pattern to follow (lifted from learneveryday.net):
foreach (GridViewRow gvrow in GridView1.Rows)
{
CheckBox CheckBox1 = (CheckBox)gvrow.FindControl("CheckBox1");
if (CheckBox1.Checked)
{
// we write this code for find a label value
Label lblLookFor = (Label)gvrow.FindControl("lblLookFor");
string lookfor = lblLookFor.Text;
}
}
Of course, this only covers how to get the employees that are checked and how to retrieve their email address from the GridViewRows. I'm assuming you already know how to send emails, but if not, see the MSDN documentation.

how to add a tooltip and confirm message box to asp CommandField(image button)

<asp:GridView ID="GridView1" runat="server"
AllowPaging="True" AllowSorting="True"
DataMember="DefaultView"
DataSourceID="SqlDataSource1"...
>
<RowStyle BackColor="#EFF3FB" />
<Columns>
<asp:BoundField ...
<asp:CommandField ButtonType="Image" DeleteText="Delete" ShowDeleteButton="True" DeleteImageUrl="images/deletered1.png"></asp:CommandField>
</Columns>
<FooterStyle BackColor="#507CD1" .../>
</asp:GridView>
I have a grid view with a an <asp:CommandField> in every row but I can't find a prperty to set a tooltip text("delete") or confirm message box (something like "Are you sure?" Yes No)
Try converting your CommandField to a TemplateField, like this:
<asp:TemplateField HeaderText="Delete">
<ItemTemplate>
<asp:Button ID="deleteButton" runat="server" CommandName="Delete" Text="Delete"
OnClientClick="return confirm('Are you sure you want to delete this user?');" />
</ItemTemplate>
</asp:TemplateField>
Then you just need to handle your Command in code-behind.

Add more columns to GridView

I have a GridView, which is bound to a database table. This GridView shows the some books (price, author information, etc).
I wants to add a column "Add to Cart" to this GridView at the end of each book. I added one column with text "Add to cart". But, on debug, it shows an error as "Add to cart is not present in db."
So please tell me how to append this column to the GridView.
Markup
<asp:GridView ID="GridView1" runat="server" BorderStyle="Double"
BorderWidth="3px" CellPadding="4" GridLines="Horizontal" Height="260px"
Width="661px">
<FooterStyle BackColor="White" ForeColor="#333333" />
<HeaderStyle BackColor="#336666" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#336666" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="White" ForeColor="#333333" />
<SelectedRowStyle BackColor="#339966" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F7F7F7" />
<SortedAscendingHeaderStyle BackColor="#487575" />
<SortedDescendingCellStyle BackColor="#E5E5E5" />
<SortedDescendingHeaderStyle BackColor="#275353" />
<Columns>
<asp:ButtonField Text="Add To Cart">
<FooterStyle HorizontalAlign="Right" />
<HeaderStyle HorizontalAlign="Right" />
<ItemStyle HorizontalAlign="Left" />
</asp:ButtonField>
</Columns>
</asp:GridView>
Code
public partial class WebForm9 : System.Web.UI.Page<p>
{
buybl obj = new buybl();
protected void Page_Load(object sender, EventArgs e)
{
obj.dept = "IT";
GridView1.DataSource = obj.select();
GridView1.DataBind();
}
}
Without your markup, it's hard to say. But, it sounds like you may have modelled the "Add To Cart" column after your columns that are bound to the database. You probably have your databound columns set up as BoundFields like this:
<Columns>
<asp:BoundField DataField="Title" HeaderText="Book Title" />
<asp:BoundField DataField="Author" HeaderText="Book Author" />
<asp:BoundField DataField="Price" HeaderText="Book Price" />
</Columns>
Your "Add To Cart" column will not be databound. If you just want to add a column that has a static button, you can add a CommandField to your GridView, like this:
<Columns>
<asp:BoundField DataField="Title" HeaderText="Book Title" />
<asp:BoundField DataField="Author" HeaderText="Book Author" />
<asp:BoundField DataField="Price" HeaderText="Book Price" />
<asp:CommandField SelectText="Add To Cart" ShowSelectButton="True" />
</Columns>
Or you can just add AutoGenerateSelectButton="True" to your GridView markup. Then you can handle the adding of items to your cart in the RowCommand or SelectedIndexChanged events that are fired when you click the Select button
you can add column like this in gridview
<asp:TemplateField >
<HeaderTemplate >
<asp:Button ID="ButtonName" runat="server" Text="Add to Cart" > </asp:Button>
</HeaderTemplate>
</asp:TemplateField>
Use the TemplateField column type -- unlimited customization:
http://msdn.microsoft.com/en-us/library/bb288032.aspx
<asp:TemplateField HeaderText="FirstName" SortExpression="FirstName">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<% #Bind("FirstName") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<% #Bind("FirstName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>

asp.net one more updatepanel is not working

I added 3 updatepanel on a aspx page.Two updatepanel includes gridview.And i make gridview paging.two buttons fill this gridviews when clicked.I set both of them triggers and conditional state.Third updatepanel includes textbox.And i set updatemode conditional,triggers third button.
That my question,when i click firstly third button i select checked datafield from gridview,run succesfully.
But when i click after one clicked,third updatepanel and gridview paging is not working.
<asp:UpdatePanel ID="UpdatePanelWorkFLow" runat="server">
<%--<Triggers>
<asp:PostBackTrigger ControlID="btnGenerate" />
</Triggers>--%>
<ContentTemplate>
<asp:GridView ID="GridWorkFlow" runat="server" AllowPaging="True" AutoGenerateColumns="False"
EnableSortingAndPagingCallbacks="false" CellPadding="4" ForeColor="#333333" GridLines="None"
OnPageIndexChanging="GridWorkFlow_PageIndexChanging">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:TemplateField ShowHeader="true" HeaderText="Seç">
<ItemTemplate>
<asp:CheckBox ID="checkWorkFlow" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField Visible="False">
<ItemTemplate>
<asp:Label ID="labelWorkFlowId" runat="server" Text='<%# Eval("WorkFlowId")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="WorkFlow Adi">
<ItemTemplate>
<asp:Label ID="labelWorkFlowName" runat="server" Text='<%# Bind("Name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Birincil Entity Adi">
<ItemTemplate>
<asp:Label ID="labelPrimaryEntity" runat="server" Text='<%# Bind("PrimaryEntity") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField Visible="true" HeaderText="Durum Açiklamasi">
<ItemTemplate>
<asp:Label ID="labelWorkFlowStaus" runat="server" Text='<%# GetStatusCodeText(DataBinder.Eval(Container.DataItem,"StatusCode"))%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btnWorkFLow" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
my gridview paging code
WorkFlowDataBind();
GridWorkFlow.PageIndex = e.NewPageIndex;
GridWorkFlow.DataBind();
my third button code
string userQueryId = GetUserQueryId();
string userqueryName = GetUserQueryName();
string workFlowId = GetWorkFlowId();
string primaryEntityName = GetWorkFlowPrimaryEntity();
GetWorkFlowName();
my one and second button when clicked load gridview
Datatable dtUserQuery=GetDataTable();
GridUserQuery.DataSource = dtUserQuery;
GridUserQuery.DataBind()
How can i fix.
Logic is .. If you have one or more update panel in the same page then properties of all update panel should be same such as if update panel data are dependency on each other
Update Mode : Always
EnableViewState : true(if you are using it)
Or you can explicitly update the Update panel when mode is conditional
Let us say you have two update panel like up1 and up2.
You have placed the label in the up1 and the other form or button placed in the up2.
Now on the click event in the up2, label placed in the up1 should be updated without loading the full page. Set the following stuffs
Set EnableParitialRendering ='True'(Script Manager)
Set Update Mode :Conditional(up1)
Set Update Mode : Always (up2). All set up done here. On click of a button in the up2, do your work and call up1.Update(); That's all.

Resources