I have two itemTemplate in Gridview, need to perform multiplication operation and store it in another column, How would i do that? i.e multiply productPrice and productQuantity and store in totalPrice label. Thank You
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:AutopartConnectionString %>"
SelectCommand="SELECT [id], [productID], [productName], [productPrice], [productQuantity], [totalPrice] FROM [cartDetails]"
UpdateCommand="UPDATE [cartDetails] SET [productQuantity]=#productQuantity WHERE [productID]=#productID"
DeleteCommand="DELETE [cartDetails] WHERE [productID]=#productID">
<%--<UpdateParameters>
<asp:Parameter Name="productQuantity" Type="Int32" />
<asp:Parameter Name="productQuantity" Type="String" />
</UpdateParameters>--%>
<DeleteParameters>
<asp:Parameter Name="productID" Type="Int32" />
</DeleteParameters>
</asp:SqlDataSource>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="id" DataSourceID="SqlDataSource1">
<Columns>
<asp:TemplateField HeaderText="id" InsertVisible="False" SortExpression="id" Visible="False">
<EditItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("id") %>'></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("id") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="productID" SortExpression="productID" >
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("productID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="productName" SortExpression="productName">
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Eval("productName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="productPrice" SortExpression="productPrice">
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Eval("productPrice") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="productQuantity" SortExpression="productQuantity">
<EditItemTemplate>
<asp:TextBox ID="TextBox4" runat="server" Text='<%# Eval("productQuantity") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label5" runat="server" Text='<%# Eval("productQuantity") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="True" />
<asp:TemplateField HeaderText="totalPrice" SortExpression="totalPrice">
<ItemTemplate>
<asp:Label ID="Label6" runat="server" Text='<%# ((Convert.ToDecimal(Eval("productPrice")))*(Convert.ToDecimal(Eval("productQuantity")))).ToString() %>'></asp:Label>
<%-- <asp:Label ID="Label6" runat="server" Text='<%# Bind("totalPrice") %>'></asp:Label>--%>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowDeleteButton="True" />
</Columns>
</asp:GridView>
My Table structure
Create table cartDetails(
id int not null identity(1,1) primary key,
productID int not null,
productName VARCHAR(150) not null ,
productPrice float,
productQuantity int,
totalPrice float
);
Is there any reason for not doing the math in the Select? or you can even make totalPrice a computed field in the table designer.
SELECT [id]
, [productID]
, [productName]
, [productPrice]
, [productQuantity]
, [productPrice] * [productQuantity] as [totalPrice]
FROM [cartDetails]
and then just bind totalPrice
<ItemTemplate>
<asp:Label ID="Label6" runat="server" Text='<%# Bind("totalPrice") %>'</asp:Label>
</ItemTemplate>
Also, regarding math within an ItemTemplate; you must use Eval() and you must make sure there are no null values. Judicious use of isnull(<field>,0) in the SELECT statement as needed.
Related
It is interesting that the asp:Label in ItemTemplate converts the following value userId=%UserId%¶m=%param% into userId=%UserId%¶m=%param%. It converts ¶ into ¶.
The aspx code looks something like this, it uses asp:ObjectDataSource to get the data to bind:
<asp:DetailsView ID="dvLink" runat="server" DataSourceID="dsLink" AutoGenerateRows="False" DefaultMode="Insert" DataKeyNames="LinkId" OnModeChanged="dvLink_ModeChanged">
<Fields>
<asp:BoundField DataField="Description" HeaderText="Description" SortExpression="Description" />
<asp:TemplateField HeaderText="Parameters" SortExpression="Parameters">
<ItemTemplate>
<asp:Label ID="lblParameters" runat="server" Text='<%# Bind("Parameters") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="tbParameters" runat="server" Text='<%# Bind("Parameters") %>' Width="500px"></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="tbParameters" runat="server" Text='<%# Bind("Parameters") %>' Width="500px"></asp:TextBox>
</InsertItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="True" ShowInsertButton="True" />
</Fields>
</asp:DetailsView>
<asp:ObjectDataSource ID="dsLink" runat="server" SelectMethod="GetLink"
TypeName="LinkLib" InsertMethod="SaveLink"
OnInserted="dsLink_Inserted" OnUpdated="dsLink_Updated"
UpdateMethod="SaveLink" DataObjectTypeName="LinkLib">
<SelectParameters>
<asp:ControlParameter ControlID="gvLinks" Name="LinkId" PropertyName="SelectedValue"
Type="Int32" />
<asp:Parameter Name="auth" Type="Object" />
</SelectParameters>
</asp:ObjectDataSource>
How can I escape ¶ during bind so it doesn't convert it into html code?
Thanks to #Greg for pointing out to a right SO article, that solved the issue. Here is the solution, if someone has a similar issue:
I changed <asp:label> to <asp:Literal> to fix the issue. Also, make sure to use the following attribute Mode="Encode".
I replaced this aspx code
<ItemTemplate>
<asp:Label ID="lblParameters" runat="server" Text='<%# Bind("Parameters") %>'></asp:Label>
</ItemTemplate>
to
<ItemTemplate>
<asp:Literal ID="lblParameters" runat="server" Mode="Encode" Text='<%# Bind("Parameters") %>'></asp:Literal>
</ItemTemplate>
So, I am creating a basic CRUD system, where you can register clients, and I want to make every textbox a required field, but I don't know how to do that.
Can someone help me ?
My code :
<asp:BoundField DataField="cliente_id" HeaderText="Id do Cliente" ReadOnly="true" />
<asp:TemplateField HeaderText="Nome do Cliente">
<ItemTemplate>
<asp:Label ID="lblClienteNome" runat="server" Text='<%# Eval("cliente_nome")%>'/>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtclienteNome" runat="server" Text='<%# Eval("cliente_nome")%>'/>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtbnome" runat="server" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText = "E-mail do Cliente">
<ItemTemplate>
<asp:Label ID="lblClienteEmail" runat="server" Text='<%# Eval("cliente_email")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtclienteEmail" runat="server" Text='<%# Eval("cliente_email")%>'/>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtbemail" runat="server" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText = "Data de Nascimento">
<ItemTemplate>
<asp:Label ID="lbldataNascimento" runat="server" Text='<%# Eval("cliente_dataNascimento")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtdataNascimento" runat="server" Text='<%# Eval("cliente_dataNascimento")%>'/>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtbdataNascimento" runat="server" />
<asp:LinkButton ID="btnAdd" CommandName="AddNew" runat="server" CssClass="btn btn-large btn-info pull-right"><i class="glyphicon glyphicon-plus"></i> Adicionar</asp:LinkButton>
</FooterTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="True" ShowDeleteButton="true" />
</Columns>
Simple way to do is using adding the following code under all the textbox
<asp:RequiredFieldValidator runat="server" id="reqName" controltovalidate="textboxid" errormessage="Please enter your Value!" />
for the past days i've been trying to find a solution for my problem but so far i couldn't.
I Have a Gridview that in edit mode, one of the fields have a DropDownList of people that is populated by a Select that uses two other fields of that row (State and City). I tried everything i found on the web about it and i couldn't make it work. Can you help me please?
Code below
<asp:GridView ID="GridView1" runat="server" AllowSorting="True"
DataKeyNames="id,Municipio,UF" DataSourceID="SqlDataSource5">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:CommandField ShowEditButton="True" />
...stuff...
<asp:TemplateField HeaderText="UF" SortExpression="UF">
<EditItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("UF") %>'>
</asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("UF") %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Municipio" SortExpression="Municipio">
<EditItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("Municipio") %>'></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("Municipio") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="People" SortExpression="People">
<ItemTemplate>
<asp:Label ID="LbPeople" runat="server" Text='<%# Bind("Cultura") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="DdlPeople" runat="server" AutoPostBack="true"
DataSourceID="SqlDataSourcePeopleEdit" DataTextField="People"
DataValueField="People">
</asp:DropDownList>
<asp:HiddenField ID="Hidden1" Value='<%# Bind("Municipio") %>' runat="server"/>
<asp:HiddenField ID="Hidden2" Value='<%# Bind("UF") %>' runat="server"/>
<asp:SqlDataSource ID="SqlDataSourcePeopleEdit" runat="server"
ConnectionString="<%$ ConnectionStrings:dbdb %>"
ProviderName="<%$ ConnectionStrings:somar.ProviderName %>"
SelectCommand="SELECT People FROM Table_People WHERE (Cidade = #Municipio) AND (UF = #UF)"
CancelSelectOnNullParameter="false">
<SelectParameters>
<asp:ControlParameter ControlID="Hidden1" Name="Municipio" PropertyName="Value" Type="String" />
<asp:ControlParameter ControlID="Hidden2" Name="UF" PropertyName="Value" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
</EditItemTemplate>
</asp:TemplateField>
...stuff...
</asp:GridView>
What am i doing wrong?
UPDATE
when i click edit, the dropdownlist does not load values.
And if i set the property CancelSelectOnNullParameter="false" on SqlDataSource it shows 'Must declare scalar variable #xxx'
I have inspected the hiddenfields and they have values loaded, so the problem is the controlparameter taht does not get the value from it
What you should do is have the hidden fields and the sqldatasource before the dropdownlist. The issue you are experiencing is due to when the data is loaded.
<asp:GridView ID="GridView1" runat="server" AllowSorting="True"
DataKeyNames="id,Municipio,UF" DataSourceID="SqlDataSource5">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:CommandField ShowEditButton="True" />
...stuff...
<asp:TemplateField HeaderText="UF" SortExpression="UF">
<EditItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("UF") %>'>
</asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("UF") %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Municipio" SortExpression="Municipio">
<EditItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("Municipio") %>'></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("Municipio") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="People" SortExpression="People">
<ItemTemplate>
<asp:Label ID="LbPeople" runat="server" Text='<%# Bind("Cultura") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:HiddenField ID="Hidden1" Value='<%# Bind("Municipio") %>' runat="server"/>
<asp:HiddenField ID="Hidden2" Value='<%# Bind("UF") %>' runat="server"/>
<asp:SqlDataSource ID="SqlDataSourcePeopleEdit" runat="server"
ConnectionString="<%$ ConnectionStrings:dbdb %>"
ProviderName="<%$ ConnectionStrings:somar.ProviderName %>"
SelectCommand="SELECT People FROM Table_People WHERE (Cidade = #Municipio) AND (UF = #UF)"
CancelSelectOnNullParameter="false">
<SelectParameters>
<asp:ControlParameter ControlID="Hidden1" Name="Municipio" PropertyName="Value" Type="String" />
<asp:ControlParameter ControlID="Hidden2" Name="UF" PropertyName="Value" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
<asp:DropDownList ID="DdlPeople" runat="server" AutoPostBack="true"
DataSourceID="SqlDataSourcePeopleEdit" DataTextField="People"
DataValueField="People">
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
...stuff...
</asp:GridView>
I would like to insert a database entry by using standard functions of my GridView and SqlDataSource. Has there been any update on this topic since the dirty workarounds some versions ago (e.g. see here, here)? I could not find any new tutorials on the web.
For example, is there any handler that allows the entry to be written to the database by using the standard -Datafield and the UpdateCommand of the SqlDataSource? At the moment, the UpdateCommand does not seem to get my entered value.
How can I debug those SqlDataSource-Database-Queries properly?
Heres the solution I've tried to get it working with: My database table tblname contains only one column, "name".
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:VConStr %>"
DeleteCommand="DELETE FROM [tblname] WHERE [name] = #name"
InsertCommand="INSERT INTO [tblname] ([name]) VALUES (#name)"
SelectCommand="SELECT [name] FROM [tblname]"
UpdateCommand="UPDATE [tblname] SET [name] = #name WHERE [name] = #name">
<DeleteParameters><asp:Parameter Name="name" Type="String" /></DeleteParameters>
<InsertParameters><asp:Parameter Name="name" Type="String" /></InsertParameters>
<UpdateParameters><asp:Parameter Name="name" Type="String" /></UpdateParameters>
</asp:SqlDataSource>
<asp:GridView ID="GridView1" runat="server" AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="name" DataSourceID="SqlDataSource1" ShowFooter="true">
<Columns>
<asp:TemplateField HeaderText="Name" ShowHeader="True">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("name") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("name") %>' />
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="Insertname" name="name_new" runat="server" Text='<%# Bind("name") %>' />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Action" ShowHeader="True">
<ItemTemplate>
<asp:Button ID="Button1" runat="server" Text="Edit" CommandName="Edit" />
<asp:Button ID="Button4" runat="server" Text="Delete" CommandName="Delete" OnClientClick="return confirm('Sure?');" />
</ItemTemplate>
<EditItemTemplate>
<asp:Button ID="Button2" runat="server" Text="Edit entry" CommandName="Update" />
<asp:Button ID="Button3" runat="server" Text="Cancel" CommandName="Cancel" />
</EditItemTemplate>
<FooterTemplate>
<asp:Button ID="Button5" runat="server" Text="New" CommandName="Insert" />
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="bookname" Height="504px"
Width="289px">
<Columns>
<asp:TemplateField HeaderText="image">
<ItemTemplate>
<asp:Image ID="Image1" runat="server"
ImageUrl='<%# String.Format("~/path/to/image/" + Eval("image")) %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Eval("image") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="bookname" HeaderText="bookname" ReadOnly="True"
SortExpression="bookname" />
<asp:BoundField DataField="price" HeaderText="price" SortExpression="price" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT [image], [bookname], [price] FROM [books]">
</asp:SqlDataSource>
My Image field is saved in (SQL server 2005)...and image inserted in database through file upload button ...
and now i want to show data in gridview but not display image field and other field shown in gridview very well becauese other fields are text format.
<asp:Image ID="Image1" runat="server" ImageUrl='<%# String.Format("~/path/to/image/{0}" , Eval("image")) %>' />
Your Problem was that string.Format accepts arguments and you were using a concat operation. Here i have used {0} - stands for argument at/for index 0. Similarly for multiple arguments you can use string.Format("{0} {1} {2}",var1,var2,var3).
Also above i am assuming {0} will be replaced by file name with extension , like abc.png.
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="bookname" Height="504px"
Width="289px">
<Columns>
<asp:TemplateField HeaderText="image">
<ItemTemplate>
<asp:Image ID="Image1" runat="server"
ImageUrl='<%# String.Format("~/path/to/image/{0}" , Eval("image")) %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Eval("image") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="bookname" HeaderText="bookname" ReadOnly="True"
SortExpression="bookname" />
<asp:BoundField DataField="price" HeaderText="price" SortExpression="price" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT [image], [bookname], [price] FROM [books]">
</asp:SqlDataSource>
I would expect rather something like
ImageUrl='<%# this.ResolveUrl("~/path/to/image/" + Eval("image")) %>' />
rather than string.Format
As your image is stored into database, you need to use a handler to retrieve the image. You can get a referance here http://www.codeproject.com/Articles/271590/Show-an-image-saved-in-a-SQL-Table-on-an-ASP-Net-I