I have a couple of gridviews in my website. Here's the code:
changeSlides.aspx:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="ID" DataSourceID="AccessDataSource1">
<Columns>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
<asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False"
ReadOnly="True" SortExpression="ID" />
<asp:ImageField DataImageUrlField="picPath"
DataImageUrlFormatString="PlaceImages/{0}" HeaderText="Picture" ControlStyle-CssClass="editPhotoGridFormat">
<ControlStyle CssClass="editPhotoGridFormat"></ControlStyle>
</asp:ImageField>
<asp:BoundField DataField="PicPath" HeaderText="Filename" />
<asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />
<asp:BoundField DataField="City" HeaderText="City" SortExpression="City" />
<asp:BoundField DataField="Country" HeaderText="Country"
SortExpression="Country" />
</Columns>
</asp:GridView>
EditBlogPost.aspx:
<asp:GridView ID="GridView1" runat="server" AllowSorting="True"
AutoGenerateEditButton="True"
DataSourceID="AccessDataSource1"
AutoGenerateColumns="False" DataKeyNames="ID"
AlternatingRowStyle-BackColor="Gray"
AlternatingRowStyle-CssClass="editGridFormat" RowStyle-CssClass="editGridFormat"
RowStyle-VerticalAlign="Top"
onselectedindexchanged="GridView1_SelectedIndexChanged">
<AlternatingRowStyle BackColor="Gray" CssClass="editGridFormat"></AlternatingRowStyle>
<Columns>
<asp:CommandField ShowSelectButton="True" ShowDeleteButton="True" />
<asp:BoundField DataField="ID" HeaderText="ID" />
<asp:BoundField DataField="BlogTitle" HeaderText="BlogTitle"
SortExpression="BlogTitle" />
<asp:ImageField DataImageUrlField="Image" HeaderText="Image"
DataImageUrlFormatString="~/PlaceImages/{0}" ControlStyle-CssClass="editPhotoGridFormat"
AlternateText="Either a wrong file type or a misspelled file name has occurred"
NullDisplayText="No picture on file" >
<ControlStyle CssClass="editPhotoGridFormat"></ControlStyle>
</asp:ImageField>
<asp:BoundField DataField="Caption"
HeaderText="Caption for Picture on Homepage" />
<asp:TemplateField headertext="Text for the homepage">
<EditItemTemplate>
<asp:TextBox id="PicTextBox" runat="server" text='<%# Bind("PicText")%>' textmode="MultiLine" height="300px" width="300px" />
</EditItemTemplate>
<ItemTemplate>
<%# Eval("PicText")%>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="TravelDate" HeaderText="Date of Travel"
SortExpression="TravelDate" DataFormatString="{0:MMMMMMMMM, dd, yyyy }" HtmlEncode="false" />
<asp:TemplateField headertext="Top of the Blog Post">
<EditItemTemplate>
<asp:TextBox id="BeginTextBox" runat="server" text='<%# Bind("BeginText")%>' textmode="MultiLine" height="300px" width="300px" />
</EditItemTemplate>
<ItemTemplate>
<%# Eval("BeginText")%>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="City" HeaderText="City" />
<asp:BoundField DataField="Country" HeaderText="Country"
SortExpression="Country" />
<asp:TemplateField headertext="Text at the End of the Post">
<EditItemTemplate>
<asp:TextBox id="EndTextBox" runat="server" text='<%# Bind("EndText")%>' textmode="MultiLine" height="300px" width="300px" />
</EditItemTemplate>
<ItemTemplate>
<%# Eval("EndText")%>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<RowStyle VerticalAlign="Top" CssClass="editGridFormat"></RowStyle>
</asp:GridView>
EditPeoplePhotos.aspx:
<asp:GridView ID="GridView1" runat="server" AllowSorting="True"
DataSourceID="AccessDataSource1" Width="493px">
<Columns>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True"
ShowSelectButton="True" />
<asp:ImageField DataImageUrlField="picPath"
DataImageUrlFormatString="photos/PeoplePhotos/{0}" HeaderText="Picture" ControlStyle-CssClass="editPhotoGridFormat">
</asp:ImageField>
</Columns>
</asp:GridView>
My update statements work, but my delete statements don't work in two of the three gridviews. My corresponding delete statements are all virtually the same: "DELETE FROM (Table Name) WHERE ID=?" but I get different error codes, which are as follows:
For changeSlides.aspx, it says "Entry with same key already exists"
For EditBlogPosts.aspx, it work fine. It deletes the blog.
For EditPeoplePhotos.aspx, it says "No value given for one or more required parameters"
Each of those pages use a separate, unrelated table in my database, and each query is only referring to one table, so there are no foreign keys used.
What is going on?
Related
For each row in GridView i have a HyperLink, and i need to generate different ID´s for each of them. Using Eval doesn´t work properly:
<asp:GridView
ID="ResultTableGvw"
runat="server"
AutoGenerateColumns="false"
OnRowDataBound="ResultTableGvw_RowDataBound">
<Columns>
<asp:BoundField ReadOnly="true" HeaderText="ID" DataField="id" />
<asp:BoundField ReadOnly="true" HeaderText="accountId" DataField="externalId" />
<asp:BoundField ReadOnly="true" HeaderText="tydeId" DataField="typeId" />
<asp:BoundField ReadOnly="true" HeaderText="Estado" DataField="statusId" />
<asp:BoundField ReadOnly="true" HeaderText="Timestamp do Pedido" DataField="timestampRequest" />
<asp:TemplateField HeaderText="Operação" ItemStyle-Wrap="false">
<ItemTemplate>
<asp:HyperLink
ID='<%# DataBinder.Eval( Container.DataItem, "Id", "OperationManagementHl{0}") %>'
runat="server"
Text="">
</asp:HyperLink>
</ItemTemplate>
<ItemStyle Wrap="False"></ItemStyle>
</asp:TemplateField>
</Columns>
</asp:GridView>
Any suggestion to solve this problem?
The error is : The ID property of a control can only be set using the ID attribute in the tag and a simple value. Example: <asp:Button runat="server" id="Button1" />
Try with this pattern and it works for me,
<asp:Label ID="lblNames" runat="server" Text='<%# Eval("Names") %>' Height ="60px" Width ="90%" CssClass="LabelDesign" />
I have a dropdown in which I have two options,
a. Selected
b. Not Selected.
I want whenever the user selects any option, the search should filter on that basis. I have a column of Selection in gridview which has both option Selected and Not Selected. The values are coming from the table.
Please see the HTML of dropdown and button for your reference:
<div style="float: left;">
<asp:DropDownList ID="ddlSort" runat="server" AutoPostBack="false" OnSelectedIndexChanged="ddlSort_SelectedIndexChanged">
<asp:ListItem Text="--Select--" Value="1"></asp:ListItem>
<asp:ListItem Text="Selected" Value="2"></asp:ListItem>
<asp:ListItem Text="Not Selected" Value="3"></asp:ListItem>
</asp:DropDownList>
<asp:Button ID="btnSortSelection" runat="server" Text="Sort Selection" OnClick="btnSortSelection_Click" />
</div>
I tried with the below mentioned code but the page refreshes and the dropdown inside the gridview loses its value. So it did not returns my search result.:-
protected void ddlSort_SelectedIndexChanged(object sender, EventArgs e) {
SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["DefaultSQLConnectionString"].ConnectionString); //create connection
SqlCommand cmd = new SqlCommand("Select * from Career.Applicant where Selection =#Selected", conn);
cmd.Parameters.AddWithValue("#selection", ddlSort.SelectedValue);
}
I want to stop the dropdownlist values to get postback and also it should work when sorted
Please see the code with Updated Panel:-
<asp:UpdatePanel runat="server" ID="updateapplicants">
<ContentTemplate>
<div style="border: 1px solid #A8A8A8; width: 920px;">
<asp:GridView ID="gv_Applicants" runat="server" AutoGenerateColumns="false" AllowPaging="true" Width="920"
PageSize="5" OnPageIndexChanging="gv_Applicants_PageIndexChanging" OnRowCommand="gv_Applicants_RowCommand"
EmptyDataText="No Applicants Found."
AllowSorting="true"
OnSorting="gv_Applicants_Sorting"
OnRowDataBound="gv_Applicants_RowDataBound" RowStyle-CssClass="a12" AlternatingRowStyle-CssClass="a22" ForeColor="#333333" GridLines="None" CssClass="table_box" HeaderStyle-Height="35px" DataKeyNames="JobId">
<AlternatingRowStyle BackColor="#F0F0F0" />
<Columns>
<asp:BoundField DataField="FirstName" HeaderText="First Name" HeaderStyle-Width="84" />
<asp:BoundField DataField="LastName" HeaderText="Last Name" HeaderStyle-Width="106" />
<asp:BoundField DataField="ContactNumber" HeaderText="Contact" HeaderStyle-Width="98" />
<asp:BoundField DataField="Email" HeaderText="Email" HeaderStyle-Width="150" />
<asp:TemplateField HeaderText="Position" SortExpression="Position" HeaderStyle-Width="107">
<ItemTemplate>
<%# Eval("Job.Position") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Location" SortExpression="Location" HeaderStyle-Width="100">
<ItemTemplate>
<%# Eval("Job.Location") %>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="AppliedDate" DataFormatString="{0:MMMM dd, yyyy}" HeaderText="Date of Application" ReadOnly="true" HeaderStyle-Width="121" />
<asp:TemplateField HeaderText="Action" HeaderStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:LinkButton ID='lnkView' CommandName='v' Text='View' runat='server' CommandArgument='<%# Eval("ApplicantId") %>'></asp:LinkButton>
|
<asp:LinkButton ID='lnkdel' CommandName='d' Text='Delete' runat='server' CommandArgument='<%# Eval("ApplicantId") %>' OnClientClick="return confirm('Are you sure to delete?');"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Selection">
<ItemTemplate>
<asp:DropDownList ID="ddlSelection" runat="server" EnableViewState="true" AutoPostBack="true">
<asp:ListItem Text="None" Value="1"></asp:ListItem>
<asp:ListItem Text="Selected" Value="2"></asp:ListItem>
<asp:ListItem Text="Not Selected" Value="3"></asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="JobId" HeaderText="Job ID" Visible="false" ReadOnly="true" />
</Columns>
<EditRowStyle BackColor="#2461BF" />
<FooterStyle BackColor="#D8DADA" Font-Bold="True" />
<HeaderStyle BackColor="#D8DADA" Font-Bold="True" />
<PagerStyle BackColor="#D8DADA" HorizontalAlign="Center" />
<RowStyle BackColor="white" BorderStyle="Solid" BorderColor="#a8a8a8" BorderWidth="1px" Height="35" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
</asp:GridView>
</div>
<br />
<br />
<div id="divDetails" runat="server" class="force-overflow">
<div class="scrollbar" id="style-1">
<div style="border-left: 1px solid #A8A8A8; border-right: 1px solid #A8A8A8;">
<asp:DetailsView ID="dv_Applicants" HeaderStyle-Font-Bold="true" HeaderStyle-BackColor="#F7E1E1" runat="server" AutoGenerateRows="false" AllowPaging="false"
HeaderText="Applicant Details" OnDataBound="dv_Applicants_DataBound" Width="912px" BackColor="#F0F0F0" BorderStyle="Solid" BorderWidth="1" BorderColor="#F0F0F0"
RowStyle-Height="30" Font-Size="10">
<Fields>
<asp:TemplateField HeaderText="Position" HeaderStyle-CssClass="c_width">
<ItemTemplate>
<%#Eval("Job.Position") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Location" HeaderStyle-CssClass="c_width">
<ItemTemplate>
<%#Eval("Job.Location") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Business Unit" HeaderStyle-CssClass="c_width">
<ItemTemplate>
<%#Eval("Job.BusinessUnit") %>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="FirstName" HeaderText="First Name" HeaderStyle-Width="190" HeaderStyle-Font-Bold="true" />
<asp:BoundField DataField="LastName" HeaderText="Last Name" HeaderStyle-Width="190" HeaderStyle-Font-Bold="true" />
<asp:BoundField DataField="ContactNumber" HeaderText="Contact" HeaderStyle-Width="190" HeaderStyle-Font-Bold="true" />
<asp:BoundField DataField="Email" HeaderText="Email" HeaderStyle-Width="190" HeaderStyle-Font-Bold="true" />
<asp:BoundField DataField="PAN" HeaderText="PAN" HeaderStyle-Width="190" HeaderStyle-Font-Bold="true" />
<asp:BoundField DataField="DOB" DataFormatString="{0:MMMM dd, yyyy}" HeaderText="Date of Birth" HeaderStyle-Width="190" HeaderStyle-Font-Bold="true" />
<asp:BoundField DataField="Graduation" HeaderText="Graduation" HeaderStyle-Width="190" HeaderStyle-Font-Bold="true" />
<asp:BoundField DataField="GradOther" HeaderText="Other" HeaderStyle-Width="190" HeaderStyle-Font-Bold="true" />
<asp:BoundField DataField="GradDate" DataFormatString="{0:MMMM dd, yyyy}" HeaderText="Graduation Date" HeaderStyle-Width="190" HeaderStyle-Font-Bold="true" />
<asp:BoundField DataField="PostGraduation" HeaderText="Post Graduation" HeaderStyle-Width="190" HeaderStyle-Font-Bold="true" />
<asp:BoundField DataField="PGOther" HeaderText="Other" HeaderStyle-Width="190" HeaderStyle-Font-Bold="true" />
<asp:BoundField DataField="PGDate" DataFormatString="{0:MMMM dd, yyyy}" HeaderText="Post Graduation date" HeaderStyle-Width="190" HeaderStyle-Font-Bold="true" />
<asp:BoundField DataField="AnyOtherQual" HeaderText="Any Other Qualification" HeaderStyle-Width="190" HeaderStyle-Font-Bold="true" />
<asp:BoundField DataField="CurrentOrg" HeaderText="Organization" HeaderStyle-Width="190" HeaderStyle-Font-Bold="true" />
<asp:BoundField DataField="CurrentDesignation" HeaderText="Designation" HeaderStyle-Width="190" HeaderStyle-Font-Bold="true" />
<asp:BoundField DataField="CurrentFunctionalDesig" HeaderText="Current Functional Designation" HeaderStyle-Width="190" HeaderStyle-Font-Bold="true" />
<asp:BoundField DataField="CurrentCTC" DataFormatString="{0} lakhs per annum" HeaderText="CTC" HeaderStyle-Width="190" HeaderStyle-Font-Bold="true" />
<asp:BoundField DataField="YrsinCurrentRole" HeaderText="Current Experience" HeaderStyle-Width="190" HeaderStyle-Font-Bold="true" />
<asp:BoundField DataField="TotalExp" HeaderText="Total Experience" HeaderStyle-Width="190" HeaderStyle-Font-Bold="true" />
<asp:BoundField DataField="KeySkills" HeaderText="Skills" HeaderStyle-Width="190" HeaderStyle-Font-Bold="true" />
<asp:BoundField DataField="City" HeaderText="City" HeaderStyle-Width="190" HeaderStyle-Font-Bold="true" />
<asp:BoundField DataField="State" HeaderText="State" HeaderStyle-Width="190" HeaderStyle-Font-Bold="true" />
<asp:BoundField DataField="Country" HeaderText="Country" HeaderStyle-Width="190" HeaderStyle-Font-Bold="true" />
<asp:TemplateField HeaderText="Willing to Relocate" ControlStyle-CssClass="c_width" HeaderStyle-CssClass="c_width">
<ItemTemplate>
<%# Convert.ToBoolean(Eval("WillingtoRelocate")) == true ? "Yes" : "No" %>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="LocationPreference" HeaderText="Location Preference" HeaderStyle-Width="190" HeaderStyle-Font-Bold="true" />
<asp:BoundField DataField="AppliedDate" DataFormatString="{0:MMMM dd, yyyy}" HeaderText="Date of Application" ReadOnly="true" HeaderStyle-Font-Bold="true" />
<asp:HyperLinkField DataNavigateUrlFields="CVFilePath" Text="View Resume" ControlStyle-ForeColor="White" ControlStyle-CssClass="bg" Target="_blank" />
</Fields>
</asp:DetailsView>
</div>
</div>
</div>
</ContentTemplate>
</asp:UpdatePanel>
I have the following GridView:
<asp:GridView ID="gvDispMsg" ClientIDMode="Static" ShowHeaderWhenEmpty="false" AlternatingRowStyle-BackColor="#EBE9E9" AutoGenerateColumns="false" EmptyDataText="There is no data to display" runat="server" AllowPaging="false" AllowSorting="true">
<Columns>
<asp:BoundField HeaderText="Delete Message" HeaderStyle-Width="12%" />
<asp:BoundField HeaderText="ID" HeaderStyle-Width="12%" DataField="ID" />
<asp:BoundField HeaderText="Date" HeaderStyle-Width="13%" DataField="Created" />
<asp:BoundField HeaderText="Message" HeaderStyle-Width="45%" DataField="Message" />
<asp:BoundField HeaderText="Active" HeaderStyle-Width="10%" DataField="Active" />
<asp:BoundField HeaderText="Created By" HeaderStyle-Width="20%" DataField="CreatedBy" />
</Columns>
</asp:GridView>
It is populated from a SQL table which is executed from code-behind. The only column which is not populated is the Delete Message column.
How can I add a button for each row in the Delete Message column which takes the argument for the ID datafield?
Add
<asp:GridView AutoGenerateDeleteButton="true" DataKeyNames="ID" OnRowDeleted="NewEvent"
and handle the NewEvent in CodeBehind
or
add a <asp:ButtonField ButtonType="Button" CommandName="delete" Text="Delete" HeaderText="Delete"/> instead of a BoundField
or
add
<asp:TemplateField HeaderText="Delete">
<ItemTemplate>
<asp:Button ID="lbiDelete" runat="server" CommandName="Delete" CommandArgument='<%# Bind("ID") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:Button ID="lbiDelete" runat="server" CommandName="Delete" CommandArgument='<%# Bind("ID") %>' />
</EditItemTemplate>
<FooterTemplate>
<asp:Button ID="lbiDelete" runat="server" CommandName="Delete" CommandArgument='<%# Bind("ID") %>' />
</FooterTemplate>
</asp:TemplateField>
where you have most abilities to configure this column in every state
I want to show sum of values on header text in grid view.
Code:
.aspx
<asp:GridView ID="gvTrHty" runat="server" AutoGenerateColumns="false" Width="100%"
Style="border: 0px solid #cdcdcd; background-color: #cdcdcd;" border="0" CellSpacing="1"
CellPadding="3" OnSorting="gvTrHty_Sorting" AllowSorting="true">
<Columns>
<asp:TemplateField HeaderText="S.No" ItemStyle-VerticalAlign="Middle" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<%#Container.DataItemIndex+1 %>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField HeaderText="Employee" DataField="UserFullName" SortExpression="UserFullName" />
<asp:BoundField HeaderText="Course" DataField="CourseName" SortExpression="CourseName" />
<asp:BoundField HeaderText="Duration(Hrs)" DataField="Duration" SortExpression="Duration" />
<asp:BoundField HeaderText="Start Date" DataField="StartDate" DataFormatString="{0:dd-MMM-yyyy}"
SortExpression="StartDate" ItemStyle-HorizontalAlign="Center" ItemStyle-VerticalAlign="Middle" />
<asp:BoundField HeaderText="End Date" DataField="EndDate" DataFormatString="{0:dd-MMM-yyyy}"
SortExpression="EndDate" ItemStyle-HorizontalAlign="Center" ItemStyle-VerticalAlign="Middle" />
<asp:BoundField HeaderText="Result" DataField="RESULT" ItemStyle-HorizontalAlign="Center"
SortExpression="RESULT" ItemStyle-VerticalAlign="Middle" />
<asp:BoundField HeaderText="Year" DataField="Year" ItemStyle-HorizontalAlign="Center"
SortExpression="Year" ItemStyle-VerticalAlign="Middle" />
<asp:BoundField HeaderText="Category" DataField="Category" ItemStyle-HorizontalAlign="Center"
SortExpression="Category" ItemStyle-VerticalAlign="Middle" />
<asp:BoundField HeaderText="Sub Category" DataField="SubCategory" SortExpression="SubCategory" />
</Columns>
<HeaderStyle CssClass="header-link grid-Header DataGridFixedHeader gvHead " />
<RowStyle CssClass="gd_odd_txt gvRow" />
</asp:GridView>
In the below image format I want to get sum of Duration
Any ideas? Thanks in advance
like in title + how to handle button click which button is in GridView Footer also?
file .aspx seems like this
<asp:GridView ID="GridView1" runat="server"
AutoGenerateColumns="False"
CellPadding="4" DataKeyNames="id" EnableModelValidation="True"
ForeColor="#333333" GridLines="None"
onrowcancelingedit="GridView1_RowCancelingEdit1"
onrowediting="GridView1_RowEditing1"
onrowupdating="GridView1_RowUpdating1" AllowPaging="True"
onrowdeleting="GridView1_RowDeleting"
onpageindexchanging="GridView1_PageIndexChanging" Height="322px"
ShowFooter="True" onselectedindexchanged="GridView1_SelectedIndexChanged" >
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:CommandField ShowEditButton="True" />
<asp:BoundField DataField="id" HeaderText="ID" Visible="False" />
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:TemplateField>
<FooterTemplate>
<asp:TextBox ID="txtName" runat="server" />
</FooterTemplate>
</asp:TemplateField>
<asp:BoundField DataField="LastName" HeaderText="LastName" />
<asp:TemplateField>
<FooterTemplate>
<asp:TextBox ID="txtLastName" runat="server" />
</FooterTemplate>
</asp:TemplateField>
<asp:BoundField DataField="DriveLic" HeaderText="DriveLicense" />
<asp:TemplateField>
<FooterTemplate>
<asp:TextBox ID="txtDriveLicense" runat="server" />
</FooterTemplate>
</asp:TemplateField>
<asp:BoundField DataField="country" HeaderText="Country" />
<asp:TemplateField>
<FooterTemplate>
<asp:TextBox ID="txtWoj" runat="server" />
</FooterTemplate>
</asp:TemplateField>
<asp:CommandField ShowDeleteButton="True" />
<asp:TemplateField>
<FooterTemplate>
<asp:Button ID="ButtonOK" Text="Confirm" runat="server" />
</FooterTemplate>
</asp:TemplateField>
</Columns>
Inside your GridView_RowCommand Event you can access the footer control by GridView1.FooterRow.FindControl method.
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName.Equals("Insert", StringComparison.OrdinalIgnoreCase))
{
TextBox txtSomeNewValue = ((TextBox)GridView1.FooterRow.FindControl("txtSomeNewValue"));
string theTextValue = txtSomeNewValue.Text;
}
}
Update: wrapped the code in an if block that checks if the commandname is what you were expecting. This event is also used for delete, edit, etc, so you might end up running the code for an event you didnt intend if you dont wrap it in this.