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>
Related
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.
<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.
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.
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>
I have a web page with checkbox list to choose multiple crystal reports to view. I want to open these reports in new tabs or windows.
I tried this :
This is the show report button:
<asp:Button ID="Button2" runat="server" Text="Show Report" OnClick="ButtonShowReport_Click"
CssClass="button" OnClientClick="aspnetForm.target ='_blank';"/>
And this is my gridview control:
<asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333"
RowStyle-HorizontalAlign="Center" AutoGenerateColumns="False"
DataKeyNames="AccountID">
<RowStyle BackColor="#>
<Columns>
<asp:TemplateField HeaderText="Choose Client's Accounts">
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server"/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField Visible="false">
<ItemTemplate>
<asp:Label id="lblAccountId" runat ="server" text='<%# Eval("AccountID")%>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="AccountID" HeaderText="AccountID" Visible="false"/>
<asp:BoundField DataField="AccountName" HeaderText="Account Name" />
<asp:BoundField DataField="Name" HeaderText="Client Name" />
</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>
My c# code looks like:
protected void ButtonShowReport_Click(object sender, EventArgs e)
{
string accountID;
int Rows = GridView1.Rows.Count;
for (int i = 0; i < Rows; i++)
{
//CheckBoxField cb = ((CheckBoxField)gvASH.Rows[i].Cells[1]).;
CheckBox cb = (CheckBox)GridView1.Rows[i].Cells[0].FindControl("CheckBox1");
if (cb.Checked == true)
{
accountID = ((Label)GridView1.Rows[i].FindControl("lblAccountID")).Text;//GridView1.DataKeys[i].Value.ToString();//GridView1.Rows[i].Cells[1].Text;
//Button2.Enabled = true;
openReport(accountID);
}
}
}
protected void openReport(string accountID)
{
//some code for setting parameters of the crystal report
//and for creating report key
Response.Redirect(string.Format("~/Report.aspx?ReportKey={0}",
ReportKey.ToString()),false);
}
So if the user checked 2 reports and clicked on the ShowReport button, it'll do the loop but opens only one new tab with the last report chosen ...
Any Suggestions please ??
Add this to the control which triggers the redirect. I know this works for Buttons/LinkButtons, not sure if it will work on a checkbox though.
OnClientClick="aspnetForm.target ='_blank';"
One method you might consider:
Instead of doing the Redirect to each report inside the code behind - dynamically generate some JavaScript that has uses window.open for each report. The javascript would look similar to this:
<script type="text/javascript">
window.open("<report1URL>", "_blank");
window.open("<report2URL>", "_blank");
...
window.open("<reportNURL>", "_blank");
</script>
Then, instead of doing a Response.Redirect, you should be able to do a Respone.Write() to get the browser to execute the code.
string myJavaScript= <dynamicallyGenerateJavascript>;
Response.Write(myJavaScript);