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

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"

Related

Link in ASP.NET

I'm not sure how to even go about looking up the answer to this question, so I apologize for not having done any research before posting. I have never touched ASP.NET before. I migrated one IIS project to a different server, and it's working as intended, except for one thing. I believe the below code snippet is responsible for creating links in a table, but it's hardcoding the links to the old URL instead of the new one. I'm not sure how to edit the ASP.NET to correct the problem and point to the new server instead. Any thoughts? Is a different code snippet responsible?
The code:
<%# Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
<!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>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="gvPublicPositionList" runat="server" AllowSorting="True" AutoGenerateColumns="False"
DataSourceID="SqlDataSource_PublicPositionList" Width="800">
<Columns>
<asp:TemplateField HeaderText="Title" SortExpression="Title">
<ItemTemplate>
<asp:LinkButton ID="lbGotoJobDescription" runat="server" CausesValidation="False" CommandName="Select"
Text='<% #Eval("Title") %>' PostBackUrl='<% #Eval("URL") %>' ></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Department" HeaderText="Department" SortExpression="Department" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource_PublicPositionList" runat="server" ConnectionString="<%$ ConnectionStrings:JobDescriptionsConnectionStringPublic2005 %>"
SelectCommand="spSelectPublicPositionList" SelectCommandType="StoredProcedure"></asp:SqlDataSource>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:JobDescriptionsConnectionStringPublic2005 %>"
SelectCommand="SELECT * FROM [tblDepartments]"></asp:SqlDataSource>
</div>
</form>
</body>
</html>
The link format:
javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("gvPublicPositionList$ctl02$lbGotoJobDescription", "", false, "", "CORRECT LINK BUT TO OLD SERVER", false, true))
The URL is coming from some data source in the code, so you'll have to find where that's getting populated. The <% #Eval("URL") %> code means that you're bound to something called URL - could be a database value, could be a property of some object, no way to know without seeing the data source.
So find out where this control is being populated (it will probably look something like someRepeater.DataSource = someDataSource;, and that will tell you what you need to change.

javascript function is executed twice

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>

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;

Get control instance in asp.net dynamic data

I am creating a web application using Asp.net dynamic data. I am using GridView to show data from the database.
In the grid view I am having following code for columns
<Columns>
<asp:DynamicField DataField="UserId" UIHint="Label" />
<asp:DynamicField DataField="Address" UIHint="Address"/>
<asp:DynamicField DataField="CreatedDate" UIHint="Label" />
</Columns>
But, before displaying I want to do some processing in C# code for each row. In normal ASP.net grid view we can handle OnRowDataBound method, and using FindControl("controlid") we can get the control instance, but in case of dynamic data, I am not getting any id attribute for columns, so I am not able to get the control instance to show updated data in that control depending on some conditions.
Thanks,
Ashwani
Try using a TemplateField instead of DynamicField: http://msdn.microsoft.com/en-us/library/bb288032.aspx
An example of how to use:
<asp:Templatefield headertext="Author Name">
<Itemtemplate>
<asp:label id="FirstNameLabel"
Text= '<%# Eval("au_fname") %>'
runat="server"/>
<asp:label id="LastNameLabel"
Text= '<%# Eval("au_lname") %>'
runat="server"/>
</Itemtemplate>
</asp:Templatefield>
Here are the options available to you:
<asp:TemplateField
AccessibleHeaderText="string"
ConvertEmptyStringToNull="True|False"
FooterText="string"
HeaderImageUrl="uri"
HeaderText="string"
InsertVisible="True|False"
ShowHeader="True|False"
SortExpression="string"
Visible="True|False">
<ControlStyle />
<FooterStyle />
<HeaderStyle />
<ItemStyle />
<AlternatingItemTemplate>
<!-- child controls -->
</AlternatingItemTemplate>
<EditItemTemplate>
<!-- child controls -->
</EditItemTemplate>
<FooterTemplate>
<!-- child controls -->
</FooterTemplate>
<HeaderTemplate>
<!-- child controls -->
</HeaderTemplate>
<InsertItemTemplate>
<!-- child controls -->
</InsertItemTemplate>
<ItemTemplate>
<!-- child controls -->
</ItemTemplate>
</asp:TemplateField>

DataGrid showing in the design window, but not in the webpage

I've just started learning ASP.net and I can't seem to get the GridView to correctly show itself when I compile it. Using Visual Studio '08 in the design view, it shows it just fine, but when I compile, it's absent.
<%# Page Language="vb" AutoEventWireup="false" CodeBehind="Default.aspx.vb" Inherits="WebAppTest._Default" %>
<!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>Untitled Page</title>
</head>
<body>
<form runat="server">
<div>
<asp:GridView id ="gridview1" runat ="server" AutoGenerateColumns = "False" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="3" >
<RowStyle ForeColor="#000066" />
<Columns>
<asp:BoundField HeaderText="Test1" />
<asp:BoundField HeaderText="Test2" />
<asp:TemplateField HeaderText="Pick one">
<ItemTemplate>
<asp:RadioButton ID="rbut1" runat="server" GroupName="test"/>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="White" ForeColor="#000066" />
<PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
<SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
</asp:GridView>
</div>
</form>
</body>
</html>
Are you binding any data at all to the Grid? Just populate it with some data.
If there is no data nothing would show up. So, if you are using some database...like MS-SQL server...use the properties of the Grid...use the DataBinding functionality and just link it up with some table.
Now, after you compile your grid will show up with all the contents of the table in the DB ..with respective headers.
I don't see your datasouce, if you assigning it in the code-behind - you need to DataBind() the grid to see it on the page.
You can also create a datasource in the design view, and assign it to the grid - no explicit databinding needed
Have you set the datasource for the gridview?
If yes then post the code where u r binding the gridview.
Please make sure you bind the gridview with datasource.
The Grid is showing everything you have bound to it, and nothing more.

Resources