我在Visual Studio 2010中创建了一个网站。在网页上,我想在gridview中显示日期。当我去配置数据源时,我使用查询构建器来获取我想要的数据 - 即,我想显示当前登录的用户的所有记录。用户是从Windows登录确定的。我的Select语句是:
SELECT tblWorkHours.EmployeeName, tblWorkHours.BeginDateOff, tblWorkHours.EndDateOff,
tblWorkHours.AllDay_YesNo, tblWorkHours.BeginTimeOff, tblWorkHours.EndTimeOff,
tblWorkHours.Approved, tblCodesWork.Description, tblEmployees.Login FROM tblWorkHours INNER JOIN
tblCodesWork ON tblWorkHours.WorkCode = tblCodesWork.WorkCodeID INNER JOIN tblEmployees ON
tblWorkHours.Employee = tblEmployees.EmployeeID WHERE (tblEmployees.Login = @username) ORDER BY
tblWorkHours.BeginDateOff DESC
当我在“配置数据源”向导中测试查询时,查询将返回正确的数据。当我运行网页时,没有gridview。我尝试在网页上添加一个字段,以确保我获得了正确的用户名,这是有效的。我究竟做错了什么?任何帮助将不胜感激!
以下是我的网页代码:
<%@ Page Title="" Language="vb" AutoEventWireup="false" MasterPageFile="~/Site.Master"
CodeBehind="TimeOffAllByUser.aspx.vb" Inherits="timework.TimeOffAllByUser" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<asp:GridView ID="GridView1" runat="server"
DataSourceID="SqlDataSource1" AllowPaging="True" AllowSorting="True">
<Columns>
<asp:BoundField DataField="EmployeeName" HeaderText="EmployeeName"
SortExpression="EmployeeName">
</asp:BoundField>
<asp:BoundField DataField="BeginDateOff"
HeaderText="BeginDateOff" SortExpression="BeginDateOff">
</asp:BoundField>
<asp:BoundField DataField="EndDateOff"
HeaderText="EndDateOff" SortExpression="EndDateOff">
</asp:BoundField>
<asp:CheckBoxField DataField="AllDay_YesNo" HeaderText="AllDay_YesNo"
SortExpression="AllDay_YesNo">
</asp:CheckBoxField>
<asp:BoundField DataField="BeginTimeOff"
HeaderText="BeginTimeOff" SortExpression="BeginTimeOff">
</asp:BoundField>
<asp:BoundField DataField="EndTimeOff"
HeaderText="EndTimeOff" SortExpression="EndTimeOff">
</asp:BoundField>
<asp:CheckBoxField DataField="Approved" HeaderText="Approved"
SortExpression="Approved">
</asp:CheckBoxField>
<asp:BoundField DataField="Description" HeaderText="Description"
SortExpression="Description">
</asp:BoundField>
<asp:DynamicField DataField="Login" HeaderText="Login" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:TimeSQLConnectionString1 %>"
SelectCommand="SELECT tblWorkHours.EmployeeName, tblWorkHours.BeginDateOff,
tblWorkHours.EndDateOff, tblWorkHours.AllDay_YesNo, tblWorkHours.BeginTimeOff,
tblWorkHours.EndTimeOff, tblWorkHours.Approved, tblCodesWork.Description, tblEmployees.Login
FROM tblWorkHours INNER JOIN tblCodesWork ON tblWorkHours.WorkCode = tblCodesWork.WorkCodeID
INNER JOIN tblEmployees ON tblWorkHours.Employee = tblEmployees.EmployeeID WHERE
(tblEmployees.Login = @username) ORDER BY tblWorkHours.BeginDateOff DESC" >
<SelectParameters>
<asp:QueryStringParameter Name="username"
QueryStringField="Split(HttpContext.Current.User.Identity.Name, quot;\", ,
CompareMethod.Text)"
Type="String" />
</SelectParameters>
</asp:SqlDataSource>
vb代码是:
Imports System
Imports System.Data
Imports System.Data.OleDb
Imports System.Data.SqlClient
Imports System.Configuration
Imports System.Web.Mvc
Public Class TimeOffAllByUser
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim vusername() As String
vusername = Split(HttpContext.Current.User.Identity.Name, "\", , CompareMethod.Text)
'TextBox1.Text = (vusername(1))
End Sub
End Class
答案 0 :(得分:0)
好的,我认为问题在于你使用的是QueryStringParameter,而在querystring字段中你使用的是表达式,这可能不起作用。你可以做的是
将QueryStringParameter更改为普通参数
<asp:Parameter Name="username" />
在您的DataSource声明中添加选择事件
OnSelecting="SqlDataSource1_Selecting"
在选择事件中指定参数值
Protected Sub SqlDataSource1_Selecting(ByVal sender as Object, ByVal e As SqlDataSourceSelectingEventArgs )
Dim vusername() As String
vusername = Split(HttpContext.Current.User.Identity.Name, "\", , CompareMethod.Text)
e.Command.Parameters["@username"].Value=vusername(1)
End Sub