I have updatePanel, in which I am having tables as shown
<asp:UpdatePanel ID="DynCreated" EnableViewState="true" UpdateMode="Conditional"
runat="server">
<ContentTemplate >
<div style="display: none" enableviewstate="true">
<asp:TextBox ID="flgClear" EnableViewState="true" Text="" runat="server"></asp:TextBox>
<asp:TextBox ID="flgAdd" EnableViewState="true" Text="" runat="server"></asp:TextBox>
<asp:TextBox ID="txtList" EnableViewState="true" Text="|" runat="server"></asp:TextBox>
<asp:TextBox ID="txtDelete" EnableViewState="true" Text="|" runat="server"></asp:TextBox></div>
<asp:Table ID="tblGrid" CssClass="grid" CellPadding="0" CellSpacing="0"
Width="100%" EnableViewState="true" runat="server"> <asp:TableRow TableSection="TableHeader" ID="trHeader" CssClass="crid-header Datagrid-header"
runat="server" EnableViewState="true">
<asp:TableHeaderCell ID="hdrSequence" runat="server">
<asp:Label ID="lblSequence" Text="#" CssClass="cel" runat="server" />
</asp:TableHeaderCell>
<asp:TableHeaderCell ID="hdrHouseBill" runat="server">
<asp:Label ID="lblHouseBill" Text="HouseBill" CssClass="ceva-form-label" runat="server" />
</asp:TableHeaderCell>
<asp:TableHeaderCell ID="hdrFileNumber" runat="server">
<asp:Label ID="lblFileNumber" Text="FileNumbere" CssClass="ceva-form-label" runat="server" />
</asp:TableHeaderCell>
</asp:TableRow>
</asp:Table>
<br />
<br />
<div class="text-center">
<asp:Button ID="btnAddNewHB" CssClass="tn btn-dialog" Text="Add New Status"
OnClientClick="return ShowNewLeg();" runat="server" />
<asp:Button Text="Validate" CssClass="cen btn-dialog" runat="server" ID="btnValidate" />
<asp:Button ID="btnSave" CssClass="tn-dialog" Text="Save" OnClientClick="return Save()" Visible="false"
runat="server" />
<asp:Button ID="btnCancel" CssClass=" btn-dialog" OnClientClick="window.returnValue = 1; window.close(); return false;"
Text="Cancel" CausesValidation="false" runat="server" />
<asp:Button ID="btnClear" CssClass=tn btn-dialog" Text="Clear All" OnClientClick="return ClearTable();"
runat="server" />
<input type="hidden" id="hndtimeformat" runat="server" name="hndtimeformat" value="0"
style="display: none" />
</div>
</ContentTemplate>
</asp:UpdatePanel>
From the back-end i am adding new Row to the tblGrid
For j As Integer = tblGrid.Rows.Count To maxRow
Dim newRow As TableRow= New TableRow()
newRow.Cells.Add(CreateNewLabel(tblGrid.Rows.Count, "SEQ",
tblGrid.Rows.Count, tblGrid.Rows.Count, "40px", bColor))
newRow.Cells.Add(CreateNewText(tblGrid.Rows.Count, "HB", "",
tblGrid.Rows.Count, "10px", "", 3, "none", False))
newRow.Cells.Add(CreateNewLabel(tblGrid.Rows.Count, "FN", "", tblGrid.Rows.Count, "60px", ""))
newRow.Cells.Add(CreateNewDD(tblGrid.Rows.Count, "ST", statusup, tblGrid.Rows.Count, "10px", "", 3, "none", True))
newRow.Cells.Add(CreateNewDT(tblGrid.Rows.Count, "datetime", "", tblGrid.Rows.Count, ""))
newRow.Cells.Add(CreateNewAC(tblGrid.Rows.Count, "LC", "", "", tblGrid.Rows.Count, "0.0", AutoCompletor.DisplayFieldType.Code, "", "", False))
newRow.Cells.Add(CreateNewText(tblGrid.Rows.Count, "SG", "", tblGrid.Rows.Count, "10px", "", 0, "none"))
newRow.Cells.Add(CreateNewCB(tblGrid.Rows.Count, "CB", "", tblGrid.Rows.Count, "5px", bColor))
newRow.Cells.Add(CreateNewText(tblGrid.Rows.Count, "Note", "", tblGrid.Rows.Count, "10px", "", 0, "none"))
newRow.Cells.Add(CreateNewLabel(tblGrid.Rows.Count, "Er", "", tblGrid.Rows.Count, "40px", bColor))
newRow.Cells.Add(CreateNewImage(tblGrid.Rows.Count, "btn", tblGrid.Rows.Count)) tblGrid.Rows.Add(newRow) Next j
I am getting data what ever i have before post back, after post back its not having any data which is present in the tabGrid.
Do I need to use ViewState or Session? is there any way i get the data?
Please advice !!
Put your data into a viewstate and get them after postback
You can use a ViewState variable if you need to use this data only during navigation inside this page. If you want keep alive data for the entire session better to use a Session variable, so you will not loose these rows also if you go in other pages.
Asp:Table isn't the right element to use if you want manage data, i suggest to try this solution (is only an example):
<asp:GridView runat="server" ID="tblGrid" CssClass="grid" CellPadding="0" CellSpacing="0">
<Columns >
<asp:TemplateField >
<HeaderTemplate >
<asp:Label ID="lblSequence" Text="#" CssClass="cel" runat="server" />
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lblSequenceItem" Text="<%# Eval("col1") %>" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
<asp:Label ID="lblHouseBill" Text="HouseBill" CssClass="ceva-form-label" runat="server" />
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lblHouseBillItem" Text="<%# Eval("col2") %>" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
<asp:Label ID="lblFileNumber" Text="FileNumbere" CssClass="ceva-form-label" runat="server" />
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lblFileNumberItem" Text="<%# Eval("col3") %>" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
and this for runtime data binding:
Creation of datatable
Dim dt As New Data.DataTable("rows")
Dim col1 As New Data.DataColumn("col1", GetType(String))
Dim col2 As New Data.DataColumn("col2", GetType(Integer))
Dim col3 As New Data.DataColumn("col3", GetType(String))
dt.Columns.Add(col1)
dt.Columns.Add(col2)
dt.Columns.Add(col3)
dt.Rows.Add ("test",1,"test") 'statement for row insert
ViewState("datasource") = dt
tblGrid.DataSource = dt
tblGrid.DataBind()
And binding in postback:
Dim dt As New Data.DataTable("rows")
If Not ViewState("datasource") Is Nothing Then dt = ViewState("datasource")
tblGrid.DataSource = dt
tblGrid.DataBind()
Related
In the VB code below, I'm trying to set the Hyperlink hypNote's text as "Add Note" when text is null. However it is not working. As a test, I've even set the Hyperlink text as "Test" and tried:
If hypNote.text = "Test" Then
hypNote.text = "Add Note"
End If
But still it doesn't work. Here's my code..
<asp:Panel ID="pnlOrders" runat="server">
<asp:Image ID="LiveOrders" alt="Live Gif" runat="server" class="extrasButton" ImageUrl="~/files/images/liveOrders.gif" />
<asp:SqlDataSource ID="DSOrders" runat="server" ConnectionString="<%$ ConnectionStrings:DBConnectionString %>" SelectCommand="SELECT TOP (100) PERCENT tblOrders.OrderID, tblOrders.stallmessage, tblOrders.price, tblAccounts.city, tblAccounts.postcode, tblOrders.phoneNo, tblOrders.tblNo, tblOrders.info, tblOrders.orderDate, tblOrders.orderStatus, tblOrders.type, tblOrders.timeFor, tblOrders.paid, tblOrders.tblNo
FROM tblOrders INNER JOIN tblAccounts ON tblOrders.accountID = tblAccounts.AccountID
WHERE tblOrders.orderStatus='Completed'
ORDER BY tblOrders.timeFor ASC">
</asp:SqlDataSource>
<asp:GridView ID="gdvOrders" width="100%" runat="server" style="font-size:1.5em" ShowHeaderWhenEmpty="True" EmptyDataText="No orders" AllowPaging="True" AutoGenerateColumns="False" CssClass="mGrid" DataKeyNames="orderID" DataSourceID="DSOrders" PageSize="20" AllowSorting="True">
<AlternatingRowStyle CssClass="alt" />
<Columns>
<asp:TemplateField HeaderText="Order">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("stallMessage") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="price" HeaderText="Price" />
<asp:BoundField DataField="phoneNo" HeaderText="Phone No" />
<asp:TemplateField HeaderText="Address/Table No.">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("tblNo") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-ForeColor="Red" HeaderText="Note">
<ItemTemplate>
<asp:HyperLink ID="hypNote" style="Font-Size:20px; color:Red;" runat="server" NavigateUrl='<%# "~/editNote.aspx?note=" & Eval("info").ToString & "&orderID=" & Eval("orderID").ToString %>' ><%# Eval("info") %></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="type" HeaderText="Type" />
<asp:BoundField DataField="orderDate" DataFormatString="{0: H:mm:ss}" HeaderText="Order Time" SortExpression="orderDate" />
<asp:BoundField DataField="timeFor" DataFormatString="{0: H:mm:ss}" HeaderText="Time For" SortExpression="timeFor" />
<asp:BoundField DataField="paid" HeaderText="Paid" />
<asp:TemplateField ShowHeader="True">
<ItemTemplate>
<asp:ImageButton visible="true" ID="lnkSent" runat="server" CausesValidation="False"
onclientclick="return confirm('Mark As Sent?');"
ImageUrl="~/files/images/icons/sendIcon.png" onclick="lnkSent_Click"></asp:ImageButton>
<asp:HiddenField ID="hidnOrderID" runat="server" Value='<%# Eval("orderID") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</asp:Panel>
Protected Sub gdvOrders_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gdvOrders.RowDataBound
For Each r As GridViewRow in gdvOrders.Rows
If r.RowType = DataControlRowType.DataRow Then
Dim hypNote As Hyperlink
hypNote = r.Cells(4).FindControl("hypNote")
If hypNote.text = "" Then
hypNote.text = "Add Note"
End If
End If
Next r
End Sub
Additionally, I wish to change the text color of the Hyperlink when "Add Note" is displayed.
I figured out my mistake. I changed this line to:
<asp:HyperLink ID="hypNote" style="Font-Size:20px; color:Red;" text='<%# Eval("info") %>' runat="server" NavigateUrl='<%# "~/editNote.aspx?note=" & Eval("info").ToString & "&orderID=" & Eval("orderID").ToString %>' ></asp:HyperLink>
I didn't have text= in there.
All worked fine then.
I have a search button and two textboxes that function as a datetime picker. What can I do to show all the records in a specific date interval? I am new to programming.
<%# Page Title="Report" Language="VB" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeFile="Report.aspx.vb"
Inherits="Report" %>
<asp:Content runat="server" ID="BodyContent" ContentPlaceHolderID="MainContent">
<asp:GridView ID="gvItems" runat="server" AutoGenerateColumns ="false">
<Columns>
<asp:BoundField HeaderText="Part Number" DataField="Part_Number" readonly="true" ></asp:BoundField>
<asp:BoundField HeaderText="Last Inventory" DataField="Last_Inv" readonly="true" ></asp:BoundField>
<asp:BoundField HeaderText="Last Quantity" DataField="Last_Qty" readonly="true" ></asp:BoundField>
<asp:BoundField HeaderText="Inventory Today" DataField="Today_Inv" readonly="true" ></asp:BoundField>
<asp:BoundField HeaderText="Today's Quantity" DataField="Today_Qty" readonly="true" ></asp:BoundField>
</Columns>
</asp:GridView>
<br />
<br />
<div class ="container" >
<div class="input-group class col-sm-3 col-md-6">
<asp:Button ID="btnSearch" runat="server" Text="Search" />
<asp:TextBox ID="txtbxSearch1" runat="server" TextMode ="Date" Width="205px"></asp:TextBox>
<asp:TextBox ID="txtbxSearch2" runat="server" TextMode ="Date" Width="205px"></asp:TextBox>
</div> <br /> <br />
</div>
<br />
<br />
Imports System.Data
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
Dim ds As DataSet = New DataSet
ds.Tables.Add(New DataTable)
ds.Tables(0).Columns.AddRange(New DataColumn() {New DataColumn("Id", GetType(System.Int32)), New DataColumn("Name", GetType(System.String)), New DataColumn("DOB", GetType(DateTime))})
ds.Tables(0).Rows.Add(1, "John Hammond", "12/12/1996")
ds.Tables(0).Rows.Add(2, "Mudassar Khan", "11/16/1995")
ds.Tables(0).Rows.Add(3, "Suzanne Mathews", "10/20/1997")
ds.Tables(0).Rows.Add(4, "Robert Schidner", "09/22/1991")
Dim dt2 As DataTable = New DataTable
dt2 = ds.Tables(0).Select.Where(() => { }, ((Convert.ToDateTime(p("DOB")) >= Convert.ToDateTime("12/12/1996")) _
AndAlso (Convert.ToDateTime(p("DOB")) >= Convert.ToDateTime("12/12/1996")))).CopyToDataTable
GridView1.DataSource = dt2
GridView1.DataBind
End Sub
Also, this link below should get you going in the right direction.
https://forums.asp.net/t/2041882.aspx?GridView+Filter+between+2+dates
I have a SqlDataSource dsDetails that selects from the DB table.
<asp:SqlDataSource ID="dsDetails" runat="server" SelectCommand="spGetDetails"
OnSelected="dsDetails_Selected" SelectCommandType="StoredProcedure">
<SelectParameters>
<asp:QueryStringParameter Name="mode" QueryStringField="mode" Type="String" DefaultValue="NULL" />
</SelectParameters>
</asp:SqlDataSource>
The result is bound to gridview in the aspx page
<asp:GridView CssClass="content" ID="gridDetails" runat="server" DataSourceID="dsDetails" AllowSorting="True" AllowPaging="True" PagerSettings-Position ="TopAndBottom" DataKeyNames="ID" AutoGenerateColumns="False" >
<PagerSettings Position="TopAndBottom" Mode="NumericFirstLast" />
<EmptyDataTemplate>
<b>There are no records to display.</b>
</EmptyDataTemplate>
<Columns>
<asp:BoundField DataField="empNum" HeaderText="Employee Number" SortExpression="empnum" ApplyFormatInEditMode="True" HtmlEncode="False" ReadOnly="True" />
<asp:BoundField DataField="empName" HeaderText="Emplopyee Name" SortExpression="empname" ApplyFormatInEditMode="True" HtmlEncode="False" ReadOnly="True" />
<asp:BoundField DataField="empDoj" HeaderText="Emplopyee Date of joining" SortExpression="empdoj" ApplyFormatInEditMode="True" HtmlEncode="False" ReadOnly="True" />
<asp:TemplateField HeaderText="Salary" SortExpression="sal">
<EditItemTemplate>
<asp:TextBox ID="txtSal" runat="server" Text='<%# Eval("sal", "{0:F}") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Eval("sal", "{0:C2}") %>'></asp:Label>--%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<EditItemTemplate>
<asp:Button ID="btnSave" runat="server" CausesValidation="True" CommandName="cmdSave"
Text="Save" />
<asp:Button ID="btnCancel" runat="server" CausesValidation="False" CommandName="cmdCancel"
Text="Cancel" />
</EditItemTemplate>
<ItemTemplate>
<asp:Button ID="btnApprove" runat="server" CausesValidation="False" CommandName="cmdApprove"
Text="Approve" />
<asp:Button ID="btnEdit" runat="server" CausesValidation="False" CommandName="cmdEdit"
Text="Edit" />
<asp:Button ID="btnDelete" runat="server" CausesValidation="false" CommandName="cmdDelete"
Text="Delete" />
<ajaxToolkit:ConfirmButtonExtender ID="cbeDelete" runat="server"
TargetControlID="btnDelete"
ConfirmText="Are you sure?" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Now what i have to achieve is Find the Sum of the Salary Column of the grid view. How to achieve this using the Data Source in the page load method? I have to get the sum of Salary for the entire result set (avg records returned 60-80)
Iterating through the rows and computing the sum in gridDetails_RowDataBound wont be effective since the rows.count will be max of 10 since the page size is 10. When the result set has more than 10 records say 55, It will compute the sum for the current gridview page alone (first 10 records). I have to get the sum for all 55 records and display it in a label in the pageload method.
Please provide suggestion..
The SQLDataSource provides a Selected event that fires after data retrieval has finished. See SqlDataSource.Selected Event. You should be able to do a sum on the rows in the grid in this event.
This worked for me
In PageLoad method add
dvSql = DirectCast(dsDetails.Select(DataSourceSelectArguments.Empty), DataView)
For Each drvSql As DataRowView In dvSql
amtSum += CDbl(drvSql("sal"))
Next
lblTotalAmt.Text = CStr(recoveryAmtSum)
In dsDetails_Selected method add this line
e.Command.Parameters("#approvaltype").Value = mode
I am trying to add a item to my database through a class made by me. I have this error
An exception of type 'System.FormatException' occurred in mscorlib.dll but was not handled in user code
Additional information: Input string was not in a correct format.
I am not sure where I went wrong but here is my code
protected void btnAdd_Click(object sender, EventArgs e)
{
string filename = FileUpload1.PostedFile.FileName;
int filebytes = FileUpload1.PostedFile.ContentLength;
byte[] image = new byte[filebytes];
FileUpload1.PostedFile.InputStream.Read(image, 0, filebytes);
string imagepath = "~/Image/" + filename;
pro.SaveNewProduct(int.Parse(txtID.Text), txtPName.Text, int.Parse(txtPrice.Text), txtDes.Text, imagepath, int.Parse(DDLCatagory.SelectedValue), int.Parse(DDLNPCAA.SelectedValue), DDLproductype.SelectedValue, int.Parse(txtQuant.Text));
}
public void SaveNewProduct(int id, string name, int price, string description, string image, int catagory, int popularity, string kindof, int quantity)
{
dal.ClearParams();
dal.AddParam("#ItemID", id);
dal.AddParam("#Name", name);
dal.AddParam("#Price", price);
dal.AddParam("#Descriptions", description);
dal.AddParam("#Images", image);
dal.AddParam("#Catagory", catagory);
dal.AddParam("#Popularity", popularity);
dal.AddParam("#Type", kindof);
dal.AddParam("#Quantity", quantity);
dal.ExecuteProcedure("spSNI");
}
Also I get the error at the pro.savenewproduct.
I have triple checked to see if I made any mistakes in what I typed as parameters and so far nothing seems off.
I am assuming you mean this, also I only typed 20 in the textbox for price to get this error:
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<div style="float:right; margin-right:300px">
<asp:Image ID="Image1" runat="server" Width="200px" Height="200px" />
<br />
<asp:FileUpload ID="FileUpload1" runat="server" />
<br /><br />
<asp:Label ID="lblPName" runat="server" Text="Product Name"></asp:Label>
<asp:TextBox runat="server" ID="txtPName"></asp:TextBox>
<asp:Label ID="lblPrice" runat="server" Text="Price"></asp:Label>
<asp:TextBox runat="server" ID="txtPrice"></asp:TextBox>
<br /><br />
<asp:Label ID="lblDes" runat="server" Text="Description"></asp:Label>
<asp:TextBox runat="server" ID="txtDes"></asp:TextBox>
<asp:Label ID="lblquant" runat="server" Text="Quantity"></asp:Label>
<asp:TextBox runat="server" ID="txtQuant"></asp:TextBox>
<br /><br />
<asp:DropDownList runat="server" ID="DDLproductype">
<asp:ListItem>Select Product Type</asp:ListItem>
<asp:ListItem>Physical Product</asp:ListItem>
<asp:ListItem>Service Product</asp:ListItem>
</asp:DropDownList>
<asp:DropDownList runat="server" ID="DDLNPCAA"></asp:DropDownList>
<asp:DropDownList runat="server" ID="DDLCatagory"></asp:DropDownList>
<br />
<asp:Label ID="lblID" runat="server" Text="ID of Existing Item"></asp:Label>
<asp:TextBox ID="txtID" runat="server"></asp:TextBox>
<asp:DropDownList ID="DDListofitems" runat="server"></asp:DropDownList>
<br />
<asp:Button ID="btnupdateID" runat="server" Text="Update ID of Item" OnClientClick="btnupdateID_Click" OnClick="btnupdateID_Click" />
<br /><br />
<asp:Button ID="btnAdd" runat="server" Text="Add New Item Or Update" OnClick="btnAdd_Click" />
I have an interesting ASP/VB.NET problem. I have a gridview where each row has its own datalist in a template column. I want to add to each item in the datalist a linkbutton that will trigger an event based on data in the datalist. But I'm not sure how to do it. It's in a project so it has the designer file which does list the gridview but not the datalist inside the gridview. When I try to add it, the listing for the datalist is removed later on when I compile.
My question is now do I get the linkbutton in the datalist in the gridview to do something?
<asp:GridView ID="gvCmteNom" runat="server" AutoGenerateColumns="False" showheader="true" HeaderStyle-BackColor="Silver" Width="1600px">
<Columns>
<asp:TemplateField HeaderText="CURRENT SERVICE">
<ItemTemplate>
<asp:TextBox ID="txtID" runat="server" Text='<%# Bind("NOMINEE_ID") %>' Visible="False" Width="25px" />
<asp:DataList ID="dlCurrentCmtes" runat="server" DataSourceID="dsCurrentCmte" RepeatLayout="Flow" DataKeyField="ID" RepeatDirection="Horizontal">
<ItemTemplate>
<asp:HiddenField runat="server" ID="hdnUserID" Value='<%# Eval("ID") %>' />
<asp:Label ID="lblDescription" runat="server" Text='<%# Eval("Description") %>' />
<asp:LinkButton runat="server" ID="lbIncrementYear" CommandName="IncrementYear" CommandArgument='<%# Eval("ProductID") %>' Text="Add Year" />
</ItemTemplate>
<SeparatorTemplate><br /><br /></SeparatorTemplate>
</asp:DataList>
<asp:SqlDataSource ID="dsCurrentCmte" runat="server"
ConnectionString=""
ProviderName="System.Data.SqlClient" SelectCommand="spCmteList" SelectCommandType="StoredProcedure">
<SelectParameters>
<asp:ControlParameter ControlID="txtID" Name="ID" PropertyName="Text" Type="String" DefaultValue="" />
</SelectParameters>
</asp:SqlDataSource>
</ItemTemplate>
</asp:TemplateField>
</Columns>
Two options:
Option 1
Specify the ItemCommand event handler for the DataList and use that to respond to the event:
<asp:DataList OnItemCommand="Item_Command" />
Then specify that function in the code behind:
Sub Item_Command(sender As Object, e As DataListCommandEventArgs)
If e.CommandName = "IncrementYear" Then
...
End If
End Sub
Option 2 Specify the OnClick eventhandler for the link button
<asp:LinkButton OnClick="LinkButton_Click" runat="server"/>
Then
Sub LinkButton_Click(sender As Object, e As EventArgs)
...
End Sub