From some reason the RequiredFieldValidation doesn't work with asp:DetailsView:
<asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False"
CellPadding="4" DataKeyNames="Id" DataSourceID="EntityDataSource1"
DefaultMode="Insert" ForeColor="#333333" GridLines="None"
Height="50px" Width="125px" OnItemInserted="DetailsView1_ItemInserted">
<AlternatingRowStyle BackColor="White" />
<CommandRowStyle BackColor="#D1DDF1" Font-Bold="True" />
<EditRowStyle BackColor="#2461BF" />
<FieldHeaderStyle BackColor="#DEE8F5" Font-Bold="True" />
<Fields>
<asp:BoundField DataField="Id" InsertVisible="false" HeaderText="Id" ReadOnly="True" SortExpression="Id" />
<asp:BoundField DataField="First_Name" HeaderText="Name" SortExpression="First_Name" />
<asp:BoundField DataField="LastName" InsertVisible="false" HeaderText="LastName" SortExpression="LastName" />
<asp:BoundField DataField="Gender" HeaderText="Gender" SortExpression="Gender" />
<asp:TemplateField HeaderText="Salary" SortExpression="Salary">
<EditItemTemplate>
<asp:TextBox ID="salaryTB" runat="server" Text='<%# Bind("Salary") %>' ValidationGroup="DetailsViewValidation"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvSalary" runat="server" ControlToValidate="salaryTB"
InitialValue="0" Display="Dynamic" ErrorMessage="ss" SetFocusOnError="true" ValidationGroup="DetailsViewValidation"></asp:RequiredFieldValidator>
</EditItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="JobTitle" InsertVisible="false" HeaderText="JobTitle" SortExpression="JobTitle" />
<asp:CommandField ShowInsertButton="True" />
</Fields>
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" />
</asp:DetailsView>
I tried everything I could think of, and, naturally, I searched it online.
When I click on Insert, the value (0) is simply inserted, no questions asked.
Related
I've got a GridView that I'd like to have two buttons with two different actions. I had tried making both of them select buttons, which would be optimal, but I can't get ASP.NET to tell me which of the two buttons triggered the event. It'll tell you the row index, but not the column from what I see.
I changed one of my buttons to an edit button so that it then calls a different method, but then it puts the row into edit mode. I do not see a way to cancel the edit, AND it is mis-using the intended use of the code.
The buttons are in the first and last columns of the gv.
<asp:GridView ID="gvMedList" runat="server" AutoGenerateColumns="False" BackColor="White" BorderColor="#999999" BorderStyle="None" BorderWidth="1px" CellPadding="3" DataSourceID="dsMedList" GridLines="Vertical" OnSelectedIndexChanged="gvMedAction" OnDataBound="gvMedList_DataBound" OnRowEditing="gvRefillButton">
<AlternatingRowStyle BackColor="#DCDCDC" />
<Columns>
<asp:CommandField ButtonType="Button" SelectText=" -STOP- " ShowSelectButton="True" ShowCancelButton="False" />
<asp:CheckBoxField DataField="Active_Med" HeaderText="Active" SortExpression="Active_Med" >
<HeaderStyle Width="50px" />
<ItemStyle HorizontalAlign="Center" />
</asp:CheckBoxField>
<asp:BoundField DataField="Medication_List_ID" HeaderText="Medication_List_ID" InsertVisible="False" ReadOnly="True" SortExpression="Medication_List_ID" >
<HeaderStyle Width="50px" />
</asp:BoundField>
<asp:BoundField DataField="Label_Name" HeaderText="Medication" SortExpression="Label_Name" >
<HeaderStyle Width="150px" />
</asp:BoundField>
<asp:BoundField DataField="Med_Form" HeaderText="Form" SortExpression="Med_Form" >
<HeaderStyle Width="50px" />
</asp:BoundField>
<asp:BoundField DataField="dose" HeaderText="Dose" SortExpression="dose" >
<HeaderStyle Width="50px" />
</asp:BoundField>
<asp:BoundField DataField="dose_unit" HeaderText="Unit" SortExpression="dose_unit" >
<HeaderStyle Width="50px" />
</asp:BoundField>
<asp:BoundField DataField="Med_Amt" HeaderText="Med_Amt" SortExpression="Med_Amt" Visible="False" />
<asp:BoundField DataField="Amount_Unit" HeaderText="Amount_Unit" SortExpression="Amount_Unit" Visible="False" />
<asp:BoundField DataField="Med_Sched_Label" HeaderText="Frequency" SortExpression="Med_Sched_Label" >
<HeaderStyle Width="150px" />
</asp:BoundField>
<asp:BoundField DataField="Med_Dispense" HeaderText="Dispense" SortExpression="Med_Dispense" >
<HeaderStyle Width="50px" />
</asp:BoundField>
<asp:BoundField DataField="Dispense_Unit" HeaderText="Unit" SortExpression="Dispense_Unit" ShowHeader="False" >
<ItemStyle Width="50px" />
</asp:BoundField>
<asp:BoundField DataField="Med_Refill" HeaderText="Med_Refill" SortExpression="Med_Refill" Visible="False" />
<asp:BoundField DataField="Comments" HeaderText="Comments" SortExpression="Comments" />
<asp:CommandField ButtonType="Button" EditText="-REFILL-" ShowCancelButton="False" ShowEditButton="True" />
</Columns>
<FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
<HeaderStyle BackColor="#27627E" Font-Bold="True" ForeColor="White" BorderColor="White" BorderStyle="Ridge" BorderWidth="2px" Height="20px" HorizontalAlign="Center" VerticalAlign="Middle" Width="125px" />
<PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
<RowStyle BackColor="#EEEEEE" ForeColor="Black" />
<SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#0000A9" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#000065" />
</asp:GridView>
Use the CommandName property
Front-End
<asp:GridView ID="gvMedList" runat="server" AutoGenerateColumns="False" BackColor="White" BorderColor="#999999" BorderStyle="None" BorderWidth="1px" CellPadding="3" DataSourceID="dsMedList" GridLines="Vertical" OnSelectedIndexChanged="gvMedAction" OnDataBound="gvMedList_DataBound" OnRowEditing="gvRefillButton" OnRowCommand="gvMedList_RowCommand">
<Columns>
<asp:TemplateField HeaderText="ColumnName">
<ItemTemplate>
<asp:Button ID="btnDoSomething" runat="server" CommandName="DoSomething" Text="Do Something" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="AnotherColumn">
<ItemTemplate>
<asp:ImageButton ID="btnImageSomething" runat="server" CommandName="DoSomethingElse" ImageUrl="~/images/yes.png" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Code-Behind (assumed C#...if you need VB let me know)
protected void gvMedList_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "DoSomething")
{
// code to execute
}
if (e.CommandName == "DoSomethingElse")
{
// code to execute
}
}
my gridview picks records form database, it's fine but problem is that when new record is updated than it is not necessarily added at the end of all records in gridview, it could appear 2nd last, in middle, at start even. I mean it doesn't appear sorted or in sequence.
.cs
protected void btnSearch_Click1(object sender, EventArgs e)
{
SqlDataSourceETTVehicleLed.SelectParameters["RegistrationNo"].DefaultValue = txtboxVehicleNo.Text;
gvVehicleLedger.DataBind();
gvVehicleLedger.Visible = true;
}
.aspx
<form id="form1" runat="server">
<div>
<asp:Button ID="btnHome" runat="server" Text="Home" CssClass="button" Width="7%" OnClick="btnHome_Click" />
</div>
<br />
<div>
<asp:Panel ID="pnlInput" runat="server" DefaultButton="btnSearch">
<asp:TextBox ID="txtboxVehicleNo" runat="server"></asp:TextBox>  
<asp:Button ID="btnSearch" Text="Search" Width="9%" CssClass="button" runat="server" OnClick="btnSearch_Click1" />
</asp:Panel>
</div>
<br />
<br />
<div>
<asp:GridView ID="gvVehicleLedger" ShowHeaderWhenEmpty="True" runat="server" CellPadding="7" DataSourceID="SqlDataSourceETTVehicleLed" ForeColor="#333333" GridLines="None" AllowPaging="True" AutoGenerateColumns="False" DataKeyNames="Vehicle No" Width="100%" BorderColor="#CCCCCC" BorderStyle="Solid" BorderWidth="1px">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:BoundField DataField="Vehicle No" HeaderText="Vehicle No" ReadOnly="True" SortExpression="Vehicle No" >
<HeaderStyle HorizontalAlign="Left" Wrap="False" />
<ItemStyle HorizontalAlign="Left" />
</asp:BoundField>
<asp:BoundField DataField="Transaction Date" HeaderText="Transaction Date" SortExpression="Transaction Date">
<HeaderStyle HorizontalAlign="Left" />
<ItemStyle HorizontalAlign="Left" Wrap="False" />
</asp:BoundField>
<asp:BoundField DataField="Engine Capacity" HeaderText="Engine Capacity" SortExpression="Engine Capacity" >
<HeaderStyle HorizontalAlign="Left" Wrap="False" />
<ItemStyle HorizontalAlign="Left" Wrap="False" />
</asp:BoundField>
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" >
<HeaderStyle HorizontalAlign="Left" />
<ItemStyle HorizontalAlign="Left" />
</asp:BoundField>
<asp:BoundField DataField="Description" HeaderText="Description" SortExpression="Description" >
<HeaderStyle HorizontalAlign="Left" />
<ItemStyle HorizontalAlign="Left" />
</asp:BoundField>
<asp:BoundField DataField="No Of Months" HeaderText="No Of Months" SortExpression="No Of Months" >
<HeaderStyle HorizontalAlign="Center" Wrap="False" />
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="Amount" HeaderText="Amount" SortExpression="Amount" >
<HeaderStyle HorizontalAlign="Right" />
<ItemStyle HorizontalAlign="Right" />
</asp:BoundField>
</Columns>
<EditRowStyle BackColor="#2461BF" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F5F7FB" />
<SortedAscendingHeaderStyle BackColor="#6D95E1" />
<SortedDescendingCellStyle BackColor="#E9EBEF" />
<SortedDescendingHeaderStyle BackColor="#4870BE" />
<emptydatarowstyle backcolor="#eff3fb"
forecolor="Red"/>
<EmptyDataTemplate>
No Records Found
</EmptyDataTemplate>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSourceETTVehicleLed" runat="server" ConnectionString="<%$ ConnectionStrings:ETTConnectionStr %>" SelectCommand="Vehicleledger" SelectCommandType="StoredProcedure">
<SelectParameters>
<asp:ControlParameter ControlID="txtboxVehicleNo" Name="RegistrationNo" PropertyName="Text" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
</div>
</form>
Store Procedure:
ALTER procedure [dbo].[Vehicleledger]
#RegistrationNo varchar(20)
as
begin
SELECT dbo.Vehicle.RNo AS 'Vehicle No', dbo.TransactionTable.DateOfTransaction as 'Transaction Date', dbo.EngineCapacity.EngineCapcity as 'Engine Capacity',
dbo.MainCategories.Name as 'Name', dbo.Categories.Description as 'Description', dbo.TransactionTable.NoOfMonth as 'No Of Months', dbo.TransactionTable.Amount as 'Amount'
FROM dbo.Categories INNER JOIN
dbo.MainCategories ON dbo.Categories.MainCategory_ID = dbo.MainCategories.CatID INNER JOIN
dbo.TransactionTable ON dbo.Categories.ID = dbo.TransactionTable.CategoryID INNER JOIN
dbo.EngineCapacity ON dbo.TransactionTable.EngineCapacityID = dbo.EngineCapacity.ID RIGHT OUTER JOIN
dbo.Vehicle ON dbo.TransactionTable.RNo = dbo.Vehicle.RNo
where dbo.Vehicle.RNo=#RegistrationNo
end
You would need to provide an ORDER BY at the end of your query. Without knowing your table schema I can only guess as to what you should order by. However, if you do have an autonumbering primary key, I would suggest using that to order by.
I would like to adjust footer template horizontal align as left in grid but, it's not working any one can explain please. this is my code
<asp:GridView ID="Grd_View" ShowFooter="True" runat="server" OnRowEditing="Grd_View_RowEditing" AutoGenerateColumns="False"
DataKeyNames="CustomerCode" CellPadding="4" OnRowCommand="Grd_View_RowCommand" GridLines="None" ShowHeader="True"
AllowPaging="True" AllowSorting="True" CssClass="style2" ForeColor="#333333" OnRowDataBound="Grd_View_RowDataBound"
OnRowDeleting="Grd_View_RowDeleting" >
<FooterStyle BackColor="#555555" ForeColor="White" HorizontalAlign="Center" Font-Bold="True" />
<Columns>
<asp:BoundField DataField="CustomerCode" HeaderText="Customer Code" InsertVisible="False"
ReadOnly="True" SortExpression="CustomerCode" ItemStyle-HorizontalAlign="Center" />
<asp:BoundField DataField="CustomerName" HeaderText="Customer Name" SortExpression="CustomerName" ItemStyle-HorizontalAlign="Center" />
<asp:BoundField DataField="Address" HeaderText="Address" SortExpression="Address" ItemStyle-HorizontalAlign="Center" />
<asp:BoundField DataField="TellNo" HeaderText="Tell No" SortExpression="TellNo" ItemStyle-HorizontalAlign="Center" />
<asp:BoundField DataField="FaxNo" HeaderText="Fax No" SortExpression="FaxNo" ItemStyle-HorizontalAlign="Center" />
<asp:BoundField DataField="Email" HeaderText="Email" SortExpression="Email" ItemStyle-HorizontalAlign="Center" />
<asp:BoundField DataField="PIC" HeaderText="Person in Contact" SortExpression="PIC" ItemStyle-HorizontalAlign="Center" />
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="btnEdit" runat="server" CommandArgument='<%#Eval("CustomerCode")%>' CommandName="Edit" Text="Edit">
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="btnDelete" runat="server" CommandArgument='<%#Eval("CustomerCode")%>' CommandName="Delete" Text="Delete">
</asp:LinkButton>
</ItemTemplate>
<FooterTemplate> <asp:LinkButton ForeColor="white" ID="btnNew" runat="server" CommandArgument='<%#Eval("CustomerCode")%>' CommandName="New" Text="New">
</asp:LinkButton>
</FooterTemplate>
</asp:TemplateField>
</Columns>
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<PagerStyle BackColor="#777777" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle BackColor="#555555" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#999999" />
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
</asp:GridView>
Try using the below:
<FooterTemplate>
<asp:LinkButton ForeColor="white" ID="btnNew" runat="server" CommandName="New" Text="New">
</asp:LinkButton>
</FooterTemplate>
<FooterStyle HorizontalAlign="Left" />
The Reason your footer template will always be in right is because you are creating it as the last column in the GridView along with the Delete column.
To solve this you need to create the footer template in the first column or I'll recommend just putting the footer template inside with edit template field in the first column and the delete template field in the second column.
I have an admin page where a user will select a document path and add that path to a certain column of a database. I am using a fileupload on the page where they can find the document and copy the path and then paste it into the details view. However, I want to skip this step and I want them to select a document and automatically make the path show up in the details view.
<asp:FileUpload ID="FileUpload1" runat="server" Visible="False" Width="384px" /><br />
<br />
<div>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<center> <asp:DetailsView ID="DetailsView1" runat="server" AllowPaging="True" AutoGenerateRows="False"
DataKeyNames="ID" DataSourceID="SqlDataSource1" Height="128px" Width="544px" Visible="False" OnModeChanged="Button2_Click" CellPadding="4" ForeColor="#333333" GridLines="None" >
<Fields>
<asp:BoundField DataField="Order" HeaderText="Order" SortExpression="Order" />
<asp:BoundField DataField="Department" HeaderText="Department" SortExpression="Department"/>
<asp:BoundField DataField="DOC_Type" HeaderText="DOC_Type" SortExpression="DOC_Type" />
<asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />
<asp:BoundField DataField="Revision" HeaderText="Revision" SortExpression="Revision" />
<asp:BoundField DataField="DOC" HeaderText="DOC" SortExpression="DOC" />
<asp:BoundField DataField="Active" HeaderText="Active" SortExpression="Active" />
<asp:BoundField DataField="Rev_Date" HeaderText="Rev_Date" SortExpression="Rev_Date" />
<asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" ReadOnly="True"
SortExpression="ID" Visible="False" />
<asp:CommandField ShowInsertButton="True" />
</Fields>
<FooterStyle BackColor="#5D7B9D" BorderStyle="None" Font-Bold="True" ForeColor="White" />
<CommandRowStyle BackColor="#E2DED6" BorderStyle="None" Font-Bold="True" />
<RowStyle BackColor="#F7F6F3" BorderStyle="None" ForeColor="#333333" />
<FieldHeaderStyle BackColor="#E9ECF1" BorderStyle="None" Font-Bold="True" />
<EmptyDataRowStyle BorderStyle="None" />
<PagerStyle BackColor="#284775" BorderStyle="None" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle BackColor="#5D7B9D" BorderStyle="None" Font-Bold="True" ForeColor="White" />
<InsertRowStyle BorderStyle="None" />
<EditRowStyle BackColor="#999999" BorderStyle="None" />
<AlternatingRowStyle BackColor="White" BorderStyle="None" ForeColor="#284775" />
</asp:DetailsView>
<br />
I need to get the fileupload1 into the DOC contenttemplate area so instead of showing an empty textbox it will show just a textbox it will show the fileupload
I'm not sure if this helps, but it is a start:
<asp:TemplateField HeaderText="DOC">
<InsertItemTemplate>
<asp:FileUpload ID="FileUploadDOC" runat="server" Width="384px" />
</InsertItemTemplate>
</asp:TemplateField>
In Codebehind you can set the doc-path in the following way:
Private Sub DetailsView1_ItemInserting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DetailsViewInsertEventArgs) Handles DetailsView1.ItemInserting
Dim FileUploadDOC As FileUpload = DirectCast(Me.DetailsView1.FindControl("FileUploadDOC"), FileUpload)
e.Values("DOC") = FileUploadDOC.PostedFile.FileName
End Sub
work on C# vs05 asp.net .want to show image on gridview....From Northwind database .Table..Categories .this table contain the picture column......i want to show this column on my gridview.......
my code is bellow.............
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4"
DataSourceID="SqlDataSource1" ForeColor="#333333" GridLines="None" Width="284px" PageSize="3">
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<Columns>
<asp:CommandField ShowSelectButton="True" />
<asp:BoundField DataField="CategoryName" HeaderText="CategoryName" SortExpression="CategoryName" />
<asp:BoundField DataField="Description" HeaderText="Description" SortExpression="Description" />
<asp:TemplateField HeaderText="Picture">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Image ID="Image1" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#999999" />
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
</asp:GridView>
You can use an ImageField, like so:
<asp:ImageField DataImageUrlField='<%# "ThumbNailImage.ashx?ImID="+ Eval("Picture") %>'></asp:ImageField>