javascript function is executed twice - asp.net

I have the code below.onClientClick event of the linkbutton showVlaues javascript function is executed. I tried to trace the code in the debug mode and when I click on the link button, the debuuger control is tranferred to the showvalues function and then it goes to the code behind and goes to page load event of the application and then it goes again to the showvalues function, I am not sure why it does that, but in debug mode, I can see that it goes to both the $get lines and when I tried to see the value of dataitems, it dataitems is not defined in current context and it changes the value of showValues. Is their any way I can avoid it that control does not go to showvalues function again. I want it to be executed only once when I click on the link button.
any help will be appreciated.
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript">
function showValues(IDValue, timeValue, minValue){
var dataItems = $get('<%= PanelBar1.Items[0].Items[0].FindControl ("Grid_IDPicker").ClientID %>')
var lbl_ID = $get('<%= PanelBar1.Items[0].Items[0].FindControl("lbl_ID").ClientID %>');
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1"
EmptyDataText="There are no data records to display.">
<Columns>
<asp:BoundField DataField="id" HeaderText="id" SortExpression="id" />
<asp:BoundField DataField="time" HeaderText="time" SortExpression="time" />
<asp:BoundField DataField="min" HeaderText="min" SortExpression="min" />
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lnkClick" OnClientClick='<%#String.Format("return showValues({0},'{1}','{2}')", Eval("id"),Eval("time"),Eval("min")) %>' runat="server"> <%# Eval("id") %></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:TestDBConnectionString1 %>"
ProviderName="<%$ ConnectionStrings:TestDBConnectionString1.ProviderName %>"
SelectCommand="SELECT [id], [time], [min] FROM [name]"></asp:SqlDataSource>
</form>
</body>
</html>

Related

My asp.net data grid won't reading from xml file

This is my first time using asp.net so pleaseaccept my apologies if this is a dumb question.
I am trying to make a web application that will work on our work sharepoint. the idea is a team of people can select when they are available to call or not and if they are the contact point that day so that people on different floors can see their availability without a line of sight.
i would normally make a basic php script but sharepoint doesnt like php and we cannot reference to external resources on our network.
The issue i have is the table that should be pulling data from the xml is just a blank page... originally it gave an error saying it couldnt find that data field and i realised i needed to use xpath to be more specific... it now doesnt give any error, like its finding the field but doesnt draw it. the code is below:
XML database:
<?xml version="1.0" encoding="utf-8" ?>
<CRM>
<agent>
<id>user1</id>
<name>name</name>
<turret>extension</turret>
<email>email</email>
<status>XXXXX</status>
<contact>N/A</contact>
</agent>
<agent>
<id>user2</id>
<name>Name 2</name>
<turret>extension</turret>
<email>email</email>
<status>XXXXX</status>
<contact>N/A</contact>
</agent>
<agent>
<id>user3</id>
<name>Name 3</name>
<turret>Extension Number</turret>
<email>email address</email>
<status>XXXXX</status>
<contact>N/A</contact>
</agent>
</CRM>
and the datagrid that should be pulling that data is here:
<%# Page Language="vb" AutoEventWireup="false" CodeBehind="CSTvisibility.aspx.vb" Inherits="CRM_availabilty.CSTvisibility" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:XmlDataSource ID="data" runat="server" DataFile="~/crmdata.xml" XPath="/agent"></asp:XmlDataSource>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BorderStyle="Ridge" DataSourceID="data">
<Columns>
<asp:BoundField DataField="name" HeaderText="CRM Agent" />
<asp:BoundField DataField="turret" HeaderText="Turret" />
<asp:BoundField DataField="email" HeaderText="Email" />
<asp:BoundField DataField="status" HeaderText="Available" />
<asp:BoundField DataField="contact" HeaderText="Contact Point" />
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
Switch to TemplateField instead of BoundField. Then you have access to the XPath.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" DataSourceID="data">
<Columns>
<asp:TemplateField HeaderText="CRM Agent">
<ItemTemplate>
<%# XPath("name") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Turret">
<ItemTemplate>
<%# XPath("turret") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Email">
<ItemTemplate>
<%# XPath("email") %>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
The XPath in the XmlDataSource must be XPath="CRM/agent"

Error running Fuction(s,e) in Web Form with Master Page in Asp.Net

I am using a combo box (outside the grid) to filter a Grid content. The code works just fine in a stand alone Web Form. However, when the same code runs in a Web From with a Master Page and I change selection in the combo box, I am getting error: "0x800a1391 - JavaScript runtime error: 'TimesheetGrid' is undefined" for TimesheetGrid.PerformCallback(s.GetValue()). Any idea what I am doing wrong and how to fix it? Thank you very much for your time and energy!
Working Web Form:
<%# Page Language="vb" AutoEventWireup="false"
CodeBehind="PayPeriodTest.aspx.vb" Inherits="TTS.PayPeriodTest" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<dx:ASPxComboBox ID="BoxPayPeriods" runat="server" DataSourceID="SqlPayPeriods" EnableClientSideAPI="True" TextField="PayRange" ValueField="PayPeriodKey">
<ClientSideEvents
SelectedIndexChanged="function(s, e) { TimesheetGrid.PerformCallback(s.GetValue()); }">
</ClientSideEvents>
</dx:ASPxComboBox>
<dx:ASPxGridView ID="TimesheetGrid" runat="server" AutoGenerateColumns="False" DataSourceID="SqlTimesheet" KeyFieldName="RecordID">
<Columns>
<dx:GridViewDataTextColumn FieldName="RecordID" ReadOnly="True" VisibleIndex="0">
<EditFormSettings Visible="False" />
</dx:GridViewDataTextColumn>
<dx:GridViewDataTextColumn FieldName="ShipCountry" VisibleIndex="1">
</dx:GridViewDataTextColumn>
<dx:GridViewDataTextColumn FieldName="PayPeriodID" VisibleIndex="2">
</dx:GridViewDataTextColumn>
</Columns>
</dx:ASPxGridView>
<br />
<asp:SqlDataSource ID="SqlTimesheet" runat="server" ConnectionString="<%$ ConnectionStrings:prd_ttsConnectionString %>" SelectCommand="SELECT RecordID, ShipCountry, PayPeriodID FROM TIMESHEET WHERE (PayPeriodID = #Param1)">
<SelectParameters>
<asp:SessionParameter DefaultValue="6" Name="Param1" SessionField="PayPeriod" />
</SelectParameters>
</asp:SqlDataSource>
<br />
<asp:SqlDataSource ID="SqlPayPeriods" runat="server" ConnectionString="<%$ ConnectionStrings:prd_ttsConnectionString %>" SelectCommand="SELECT * FROM [vw_PayPeriods]"></asp:SqlDataSource>
</div>
</form>
Non Working Web Form with a Master Page:
<%# Page Title="" Language="vb" AutoEventWireup="false" MasterPageFile="~/Main.master" CodeBehind="PayPeriodSub.aspx.vb" Inherits="TTS.PayPeriodSub" %>
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
<dx:ASPxComboBox ID="BoxPayPeriods" runat="server" DataSourceID="SqlPayPeriods" EnableClientSideAPI="True" TextField="PayRange" ValueField="PayPeriodKey">
<ClientSideEvents
SelectedIndexChanged="function(s, e) { TimesheetGrid.PerformCallback(s.GetValue()); }">
</ClientSideEvents>
</dx:ASPxComboBox>
<dx:ASPxGridView ID="TimesheetGrid" runat="server" AutoGenerateColumns="False" DataSourceID="SqlTimesheet" KeyFieldName="RecordID">
<Columns>
<dx:GridViewDataTextColumn FieldName="RecordID" ReadOnly="True" VisibleIndex="0">
<EditFormSettings Visible="False" />
</dx:GridViewDataTextColumn>
<dx:GridViewDataTextColumn FieldName="ShipCountry" VisibleIndex="1">
</dx:GridViewDataTextColumn>
<dx:GridViewDataTextColumn FieldName="PayPeriodID" VisibleIndex="2">
</dx:GridViewDataTextColumn>
</Columns>
</dx:ASPxGridView>
<br />
<asp:SqlDataSource ID="SqlTimesheet" runat="server" ConnectionString="<%$ ConnectionStrings:prd_ttsConnectionString %>" SelectCommand="SELECT RecordID, ShipCountry, PayPeriodID FROM TIMESHEET WHERE (PayPeriodID = #Param1)">
<SelectParameters>
<asp:SessionParameter DefaultValue="6" Name="Param1" SessionField="PayPeriod" />
</SelectParameters>
</asp:SqlDataSource>
<br />
<asp:SqlDataSource ID="SqlPayPeriods" runat="server" ConnectionString="<%$ ConnectionStrings:prd_ttsConnectionString %>" SelectCommand="SELECT * FROM [vw_PayPeriods]"></asp:SqlDataSource>
</asp:Content>
Click here to see the error screenshot
This is because, when you use master page, the ID which is rendered to client wil be "TimesheetGrid". Therefore the javascript does not recognize the control with the ID you have provided.
You have two ways to resolve the issue.
using ClientIDMode="Static".
<dx:ASPxGridView ID="TimesheetGrid" ClientIDMode="Static" runat="server" AutoGenerateColumns="False" DataSourceID="SqlTimesheet" KeyFieldName="RecordID">
<Columns>
Use ClientIDMode="Static" only if your web form(including master page or other user controls) has unique ID.
Use "<%=TimesheetGrid.ClientID%>" to get the exact client ID which is rendered to client.
Hope this helps!

'ASP.default_aspx' does not contain a definition for 'DS_Filtering' and no extension method

I'm getting the error below after enabling SqlDataSource filtering event on default.aspx.
'ASP.default_aspx' does not contain a
definition for 'DS_Filtering' and no
extension method
Am i missing anything?
Please help
Below might give you idea how this works, please check:
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>ASP.NET Example</title>
</head>
<body>
<form id="FORM1" runat="server">
<p>Show all employees with the following title:
<asp:DropDownList
id="DropDownList1"
runat="server"
AutoPostBack="True">
<asp:ListItem>Sales Representative</asp:ListItem>
<asp:ListItem>Sales Manager</asp:ListItem>
<asp:ListItem>Vice President, Sales</asp:ListItem>
</asp:DropDownList></p>
<asp:SqlDataSource
id="SqlDataSource1"
runat="server"
ConnectionString="<%$ ConnectionStrings:NorthwindConnection %>"
SelectCommand="SELECT EmployeeID,FirstName,LastName,Title FROM Employees"
FilterExpression="Title='{0}'" OnFiltering="SqlDataSource1_Filtering">
<FilterParameters>
<asp:ControlParameter Name="Title" ControlId="DropDownList1" PropertyName="SelectedValue"/>
</FilterParameters>
</asp:SqlDataSource><br />
<asp:GridView
id="GridView1"
runat="server"
DataSourceID="SqlDataSource1"
AutoGenerateColumns="False">
<columns>
<asp:BoundField Visible="False" DataField="EmployeeID" />
<asp:BoundField HeaderText="First Name" DataField="FirstName" />
<asp:BoundField HeaderText="Last Name" DataField="LastName" />
</columns>
</asp:GridView>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</form>
</body>
</html>
Server Side:
protected void SqlDataSource1_Filtering(object sender, SqlDataSourceFilteringEventArgs e)
{
Label1.Text = e.ParameterValues[0].ToString();
}

date picker in gridview not popping - vs2010

My code:
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<asp:GridView runat="server" ID="MyGridView" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="Col1" HeaderText="Column 1" />
<asp:BoundField DataField="Col2" HeaderText="Date 1" />
<asp:BoundField DataField="Col3" HeaderText="Date 2" />
<asp:TemplateField HeaderText="Date 2" >
<EditItemTemplate>
<asp:TextBox ID="txtDate" CssClass="datepickerCompleted"
runat="server" Text="2011/1/1" ></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Col4" HeaderText="Date 3" />
<asp:TemplateField HeaderText="Date 3"></asp:TemplateField>
</Columns>
</asp:GridView>
<script type="text/javascript" language="javascript" src="<%= VirtualPathUtility.ToAbsolute("~/Script/jquery-1.4.1-vsdoc.js")%>"></script>
<script type="text/javascript">
$(function () {
$(".datepickerCompleted").datepicker();
});
</script>
</asp:Content>
Followed example from here:
enter link description here
When I look at the source, i do not see any value populate in txtDate text box, which i suspect is then not firing the jquery method..
how do i debug?
Your code looks good, but the datepicker is part of jQuery UI which is a separate download/script--it's not included in the jquery-1.4.1 file, and it looks like that's the only script you have referenced.
EDIT: Here's an example that works for me:
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
<script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script>
<script src="Scripts/jquery-ui-1.8.12.custom.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(function () {
$(".datepickerCompleted").datepicker();
});
</script>
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<asp:GridView runat="server" ID="MyGridView" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField HeaderText="Date 2">
<ItemTemplate>
<asp:TextBox ID="txtDate" CssClass="datepickerCompleted" runat="server" Text="2011/1/1" ></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</asp:Content>
You can download a copy of jQuery UI here, and here's some more information on the datapicker.
Based on the active/voted Answer, I think one more statement shall be added for better visibility of the datepicker. Otherwise it would be blurred/dimmed by the background. The proposed statement is put at 2nd line (assuming jquery-ui-1.8.14.custom.css file is downloaded & put in Css folder):
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
<link type="text/css" href="Css/jquery-ui-1.8.14.custom.css" rel="Stylesheet" />
<script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script>
<script src="Scripts/jquery-ui-1.8.12.custom.min.js" type="text/javascript"></script>
...

Can't find GridView in ASP

I am brand new to asp and downloaded some example code of a shopping cart. When I created a new project in Visual Studio, added all the files in and tried to compile I got the following error.
Error 1 The name 'gvShoppingCart' does not exist in the current context c:\users\slaphappysmoker\documents\visual studio 2010\Projects\Shopcart\Shopcart\ViewCart.aspx.cs 21 3 Shopcart
However I can see where gvShoppingCart gets defined in the ViewCart.aspx file.
<%# Page Language="C#" AutoEventWireup="true" CodeFile="ViewCart.aspx.cs" Inherits="ViewCart" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Shopping Cart</title>
<link href="Styles/StyleSheet.css" rel="stylesheet" type="text/css" />
</head>
<body>
<form id="form1" runat="server">
<div class="container">
<h1>Shopping Cart</h1>
< Back to Products
<br /><br />
<asp:GridView runat="server" ID="gvShoppingCart" AutoGenerateColumns="false" EmptyDataText="There is nothing in your shopping cart." GridLines="None" Width="100%" CellPadding="5" ShowFooter="true" DataKeyNames="ProductId" OnRowDataBound="gvShoppingCart_RowDataBound" OnRowCommand="gvShoppingCart_RowCommand">
<HeaderStyle HorizontalAlign="Left" BackColor="#3D7169" ForeColor="#FFFFFF" />
<FooterStyle HorizontalAlign="Right" BackColor="#6C6B66" ForeColor="#FFFFFF" />
<AlternatingRowStyle BackColor="#F8F8F8" />
<Columns>
<asp:BoundField DataField="Description" HeaderText="Description" />
<asp:TemplateField HeaderText="Quantity">
<ItemTemplate>
<asp:TextBox runat="server" ID="txtQuantity" Columns="5" Text='<%# Eval("Quantity") %>'></asp:TextBox><br />
<asp:LinkButton runat="server" ID="btnRemove" Text="Remove" CommandName="Remove" CommandArgument='<%# Eval("ProductId") %>' style="font-size:12px;"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="UnitPrice" HeaderText="Price" ItemStyle-HorizontalAlign="Right" HeaderStyle-HorizontalAlign="Right" DataFormatString="{0:C}" />
<asp:BoundField DataField="TotalPrice" HeaderText="Total" ItemStyle-HorizontalAlign="Right" HeaderStyle-HorizontalAlign="Right" DataFormatString="{0:C}" />
</Columns>
</asp:GridView>
<br />
<asp:Button runat="server" ID="btnUpdateCart" Text="Update Cart" OnClick="btnUpdateCart_Click" />
</div>
</form>
</body>
</html>
Here is the code that is calling the GridView in the ViewCart.aspx.cs file:
protected void BindData() {
// Let's give the data to the GridView and let it work!
// The GridView will take our cart items one by one and use the properties
// that we declared as column names (DataFields)
gvShoppingCart.DataSource = ShoppingCart.Instance.Items;
gvShoppingCart.DataBind();
}
How can I get the ViewCart.aspx.cs code file to recognize the ViewCart.aspx and the GridView contained within?
Edit: Added the function that calls the gridview.
Try to make sure that the gridview has a reference in the designer.cs file
protected global :: System.Web.UI.WebControls.GridView gvShoppingCart;

Resources