当我想从我的数据库绑定网格中的某些东西时,我遇到了这个奇怪的错误 我一直试图在过去几个小时内找到以下问题的解决方法,但没有任何成功。我在互联网上找到了许多可能的解决方案,但它们都没有解决我的问题。
<%@ Page Title="" Language="C#" MasterPageFile="~/ABBMaster.Master" AutoEventWireup="true"
CodeBehind="UserEditPanel.aspx.cs" Inherits="HolidayTracker.Administrator.UserEditPanel" %>
<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>
<asp:Content ID="Content1" ContentPlaceHolderID="MainRegionContentPlaceHolder" runat="server">
<telerik:RadScriptManager ID="rsmTelerik" runat="server">
<Scripts>
<asp:ScriptReference Assembly="Telerik.Web.UI" Name="Telerik.Web.UI.Common.Core.js"></asp:ScriptReference>
<asp:ScriptReference Assembly="Telerik.Web.UI" Name="Telerik.Web.UI.Common.jQuery.js"></asp:ScriptReference>
<asp:ScriptReference Assembly="Telerik.Web.UI" Name="Telerik.Web.UI.Common.jQueryInclude.js"></asp:ScriptReference>
</Scripts>
</telerik:RadScriptManager>
<br />
<%-- Export script %>
<%-- <script type="text/javascript">
function onRequestStart(sender, args) {
if (args.get_eventTarget().indexOf("ExportToExcelButton") >= 0 ||
args.get_eventTarget().indexOf("ExportToWordButton") >= 0 ||
args.get_eventTarget().indexOf("ExportToCsvButton") >= 0) {
args.set_enableAjax(false);
}
}
</script>--%>
<asp:Panel ID="pnlContent" CssClass="pnlContent" runat="server">
<asp:Panel ID="pnlHeader" CssClass="pnlHeader" runat="server">
</asp:Panel>
<asp:Panel ID="pnlBody" CssClass="pnlBody" runat="server">
<telerik:RadAjaxPanel ID="RadAjaxPanel1" runat="server" Skin="Default" LoadingPanelID="RadAjaxLoadingPanel1">
<fieldset style="padding: 10px; margin-bottom: 10px;">
<legend>User form</legend>
<div class="formRowDiv">
<asp:Label ID="label4" CssClass="formLabel" runat="server" Text="Email"></asp:Label>
<telerik:RadComboBox ID="rcbSearch" CssClass="rowForm" runat="server" Width="260px"
Height="100px" EmptyMessage="Enter Email" EnableLoadOnDemand="true" AutoPostBack="true" AllowCustomText="true" MarkFirstMatch="true"
OnItemsRequested="rcbSearch_ItemsRequested" CausesValidation="false" OnSelectedIndexChanged="rcbSearch_IndexChanged" Skin="Default">
</telerik:RadComboBox>
<asp:RegularExpressionValidator ID="revEmail" runat="server" Display="Dynamic" ControlToValidate="rcbSearch"
ValidationExpression="^[\w\.\-]+@[a-zA-Z0-9\-]+(\.[a-zA-Z0-9\-]{1,})*(\.[a-zA-Z]{2,3}){1,2}$"
ErrorMessage="Enter a valid email address" />
<asp:RequiredFieldValidator ID="rfvEmail" runat="server" Display="Dynamic" ControlToValidate="rcbSearch"
ErrorMessage="Can not be empty" CssClass="rowFormValidation" />
</div>
<div class="formRowDiv">
<asp:Label ID="label1" CssClass="formLabel" runat="server" Text="Firstname"></asp:Label>
<telerik:RadTextBox ID="rtbFirstName" CssClass="rowForm" runat="server" ReadOnly="false" />
<asp:RequiredFieldValidator ID="rfvFirstName" runat="server" Display="Dynamic" ControlToValidate="rtbFirstName"
ErrorMessage="Can not be empty" CssClass="rowFormValidation" />
</div>
<div class="formRowDiv">
<asp:Label ID="label2" CssClass="formLabel" runat="server" Text="Lastname"></asp:Label>
<telerik:RadTextBox ID="rtbLastName" CssClass="rowForm" runat="server" ReadOnly="false" />
<asp:RequiredFieldValidator ID="rfvLastName" runat="server" Display="Dynamic" ControlToValidate="rtbLastName"
ErrorMessage="Can not be empty" CssClass="rowFormValidation" />
</div>
<div class="formRowDiv">
<asp:Label ID="label3" CssClass="formLabel" runat="server" Text="Username"></asp:Label>
<telerik:RadTextBox ID="rtbUserName" CssClass="rowForm" runat="server" ReadOnly="false" />
<asp:RequiredFieldValidator ID="rfvUserName" runat="server" Display="Dynamic" ControlToValidate="rtbUserName"
ErrorMessage="Can not be empty" CssClass="rowFormValidation" />
</div>
<div class="formRowDiv" style="display: none;">
<asp:Label ID="label5" CssClass="formLabel" runat="server" Text="AdAccount"></asp:Label>
<telerik:RadTextBox ID="rtbAdAccount" CssClass="rowForm" runat="server" ReadOnly="false" />
</div>
<div class="formRowDiv" style="display: none;">
<asp:Label ID="label6" CssClass="formLabel" runat="server" Text="AdDomain"></asp:Label>
<telerik:RadTextBox ID="rtbAdDomain" CssClass="rowForm" runat="server" ReadOnly="True" />
</div>
<div class="formRowDiv">
<asp:Label ID="label8" runat="server" Text="Role" CssClass="formLabel"></asp:Label>
<telerik:RadComboBox ID="rcbUserRoles" CssClass="rowForm" CheckBoxes="True" EmptyMessage="- Please select -"
runat="server" DataTextField="Name" DataValueField="RoleId">
</telerik:RadComboBox>
<asp:SqlDataSource ID="UserRoleSqlSource" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT * FROM [HtUserRole]"></asp:SqlDataSource>
</div>
<div class="formRowDiv">
<%-- <asp:Label ID="label7" CssClass="formLabel" runat="server" Text="Annual vacations"></asp:Label> --%>
<telerik:RadAjaxPanel ID="RadAjaxPanel2" runat="server" LoadingPanelID="RadAjaxLoadingPanel1">
<telerik:RadGrid ID="rgAnnualVacation" runat="server" CellSpacing="0" GridLines="None" AllowPaging="True" AutoGenerateDeleteColumn="True"
OnInsertCommand="rgAnnualVacation_InsertCommand" OnDeleteCommand="rgAnnualVacation_DeleteCommand" Height="200px"
OnUpdateCommand="rgAnnualVacation_UpdateCommand" OnNeedDataSource="rgAnnualVacation_NeedDataSource">
<MasterTableView AutoGenerateColumns="false" DataKeyNames="AnnualVacationId" CommandItemDisplay="Top" InsertItemPageIndexAction="ShowItemOnCurrentPage">
<Columns>
<telerik:GridBoundColumn DataField="AnnualVacationId" Visible="false" DataType="System.Int32" FilterControlAltText="Filter AnnualVacationId column" HeaderText="AnnualVacationId" ReadOnly="True" SortExpression="AnnualVacationId" UniqueName="AnnualVacationId">
</telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="UserId" Visible="false" DataType="System.Int32" FilterControlAltText="Filter UserId column" HeaderText="UserId" SortExpression="UserId" UniqueName="UserId">
</telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="FromDate" DataType="System.DateTime" FilterControlAltText="Filter FromDate column" HeaderText="FromDate" SortExpression="FromDate" UniqueName="FromDate">
</telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="ToDate" DataType="System.DateTime" FilterControlAltText="Filter ToDate column" HeaderText="ToDate" SortExpression="ToDate" UniqueName="ToDate">
</telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="WorkingTime" DataType="System.Int32" FilterControlAltText="Filter WorkingTime column" HeaderText="WorkingTime" SortExpression="WorkingTime" UniqueName="WorkingTime">
</telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="VacationDays" DataType="System.Double" FilterControlAltText="Filter VacationDays column" HeaderText="VacationDays" SortExpression="VacationDays" UniqueName="VacationDays">
</telerik:GridBoundColumn>
<telerik:GridEditCommandColumn />
</Columns>
<EditFormSettings EditFormType="Template">
<FormTemplate>
<table>
<tr>
<td>From</td>
<td>
<telerik:RadDatePicker ID="fromdatepicker" runat="server" MaxDate="1/1/2015" DbSelectedDate='<%# Eval("fromdate") == null ? null : Eval("fromdate") %>' TabIndex="4">
</telerik:RadDatePicker>
</td>
</tr>
<tr>
<td>To</td>
<td>
<telerik:RadDatePicker ID="todatepicker" runat="server" MinDate="1/1/1900" DbSelectedDate='<%# Eval("todate") == null ? null :Eval("todate")%>'
TabIndex="4">
</telerik:RadDatePicker>
</td>
</tr>
<tr>
<td>Working time (%)</td>
<td>
<telerik:RadNumericTextBox ID="txtWorkingTime" runat="server" NumberFormat-DecimalDigits="0" DbValue='<%# Bind("WorkingTime") %>'></telerik:RadNumericTextBox>
</td>
</tr>
<tr>
<td>Vacation days</td>
<td>
<telerik:RadNumericTextBox ID="txtVacationDays" runat="server" NumberFormat-DecimalDigits="2" DbValue='<%# Bind("VacationDays") %>'></telerik:RadNumericTextBox>
</td>
</tr>
</table>
<table style="margin-top: 15px;">
<tr>
<td>
<asp:Button ID="btnUpdate" Text='<%# (Container is GridEditFormInsertItem) ? "Insert" : "Update" %>'
runat="server" CommandName='<%# (Container is GridEditFormInsertItem) ? "PerformInsert" : "Update" %>'></asp:Button>
</td>
<td>
<asp:Button ID="btnCancel" Text="Cancel" runat="server" CausesValidation="False"
CommandName="Cancel"></asp:Button>
</td>
</tr>
</table>
</FormTemplate>
</EditFormSettings>
<%--<CommandItemSettings ShowExportToWordButton="true" ShowExportToExcelButton="true" ShowExportToCsvButton="true" />--%>
</MasterTableView>
<FilterMenu EnableImageSprites="False">
</FilterMenu>
</telerik:RadGrid>
</telerik:RadAjaxPanel>
</div>
<div class="formRowDiv">
<telerik:RadButton ID="rbBack" CssClass="rowButton" runat="server" Text="Back" OnClick="rbBack_Clicked"
CausesValidation="false" />
<telerik:RadButton ID="rbSave" CssClass="rowButton" runat="server" Text="Save" OnClick="rbSave_Clicked" />
</div>
</fieldset>
</telerik:RadAjaxPanel>
<telerik:RadAjaxLoadingPanel ID="RadAjaxLoadingPanel1" runat="server" Skin="Default">
</telerik:RadAjaxLoadingPanel>
</asp:Panel>
<asp:Panel ID="pnlFooter" CssClass="pnlFooter" runat="server">
</asp:Panel>
</asp:Panel>
</asp:Content>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HolidayTracker.Data.Model;
using Telerik.Web.UI;
using HolidayTracker.Code;
using System.Collections;
namespace HolidayTracker.Administrator
{
public partial class UserEditPanel : System.Web.UI.Page
{
private List<AnnualVacation> annualVacations = new List<AnnualVacation>();
private Boolean newUser;
private HtUser paramUser;
private HtUser user;
private HtUser User
{
get
{
user = HtUser.INIT_USER(this.Page, true);
return user;
}
}
protected void Page_Load(object sender, EventArgs e)
{
getParameters();
if (!IsPostBack)
{
bindData();
fillFieldsWithData();
initFieldSettings();
}
}
private void getParameters()
{
if (Page.Request["userId"] != null)
{
int userId = Constants.TryConvert(Page.Request["userId"], this.Page);
this.paramUser = HtUser.GetById(userId);
}
else
{
this.paramUser = new HtUser();
}
}
private void initFieldSettings()
{
if (!newUser)
{
//this.rcbSearch.Enabled = false;
}
}
private void bindData()
{
this.rcbUserRoles.DataSource = this.UserRoleSqlSource;
this.rcbUserRoles.DataBind();
//fill viewstate with annual vacations if empty
if (paramUser != null && paramUser.AnnualVacations.Any())
{
if (ViewState["AnnualVacationSource"] == null)
{
annualVacations.AddRange(paramUser.AnnualVacations);
ViewState["AnnualVacationSource"] = annualVacations;
}
}
}
private void fillFieldsWithData()
{
if (paramUser != null)
{
this.rtbFirstName.Text = paramUser.FirstName;
this.rtbLastName.Text = paramUser.LastName;
this.rtbUserName.Text = paramUser.UserName;
this.rcbSearch.Text = paramUser.Email;
this.rtbAdAccount.Text = paramUser.AdAccount;
this.rtbAdDomain.Text = paramUser.AdDomain;
//UserRole
IEnumerable<HtUserRole> userRoles = paramUser.HtUserRoles != null ? paramUser.HtUserRoles : null;
if (userRoles != null && userRoles.Count() > 0)
{
foreach (RadComboBoxItem rcbi in rcbUserRoles.Items)
{
rcbi.Checked = false;
foreach (HtUserRole ur in userRoles)
{
if (ur.RoleId.ToString() == rcbi.Value)
{
rcbi.Checked = true;
break;
}
}
}
}
}
}
protected void rcbSearch_ItemsRequested(object sender, RadComboBoxItemsRequestedEventArgs e)
{
if (e.Text != null && e.Text.Length > 2)
{
IEnumerable<HtUser> result = null;
AdLookup lookup = new AdLookup();
result = lookup.SearchAdUserByEmail(e.Text);//HtUser.SearchByEmail(e.Text);
if (result != null && result.Count() > 0)
{
foreach (HtUser user in result)
{
this.rcbSearch.Items.Add(new RadComboBoxItem(user.Email, user.UserId.ToString()));
}
}
}
}
protected void rcbSearch_IndexChanged(object sneder, RadComboBoxSelectedIndexChangedEventArgs e)
{
string email = e.Text;
AdLookup lookup = new AdLookup();
paramUser = lookup.GetAdUserByEmail(email);
fillFieldsWithData();
}
private void saveData()
{
HolidayTrackerEntities ctx = HtEntityFactory.Context;
HtUser userToSave = ctx.HtUsers.Where(u => u.UserId == paramUser.UserId).FirstOrDefault();
if (userToSave == null)
{
userToSave = new HtUser();
userToSave.IsNew = true;
}
userToSave.FirstName = this.rtbFirstName.Text;
userToSave.LastName = this.rtbLastName.Text;
userToSave.UserName = this.rtbUserName.Text;
userToSave.Email = this.rcbSearch.Text;
userToSave.AdAccount = this.rtbAdAccount.Text;
userToSave.AdDomain = this.rtbAdDomain.Text;
//UserRole
foreach (RadComboBoxItem rcbi in rcbUserRoles.Items)
{
HtUserRole newUserRole = HtUserRole.GetById(int.Parse(rcbi.Value));
if (rcbi.Checked)
{
if (userToSave.HtUserRoles.Where(x => x.RoleId == newUserRole.RoleId).Count() == 0)
{
userToSave.HtUserRoles.Add(newUserRole);
}
}
else
{
if (userToSave.HtUserRoles.Where(x => x.RoleId == newUserRole.RoleId).Count() > 0)
{
userToSave.HtUserRoles.Remove(newUserRole);
}
}
}
if (userToSave.IsNew)
{
ctx.AddToHtUsers(userToSave);
}
if (((List<AnnualVacation>)ViewState["AnnualVacationSource"]) != null)
{
List<AnnualVacation> vacationToSave = (List<AnnualVacation>)ViewState["AnnualVacationSource"];
IEnumerable<AnnualVacation> existing = paramUser.AnnualVacations;
foreach (AnnualVacation toSave in vacationToSave)
{
//if (toSave.IsNew) {
// ctx.AddToAnnualVacations(toSave);
//}
//Zu löschende aus context löschen
}
}
ctx.SaveChanges();
}
protected void rbBack_Clicked(object sender, EventArgs e)
{
Response.Redirect("~/Administrator/UserPanel.aspx");
}
protected void rbSave_Clicked(object sender, EventArgs e)
{
saveData();
fillFieldsWithData();
}
protected void rgAnnualVacation_InsertCommand(object sender, GridCommandEventArgs e)
{
try
{
GridEditableItem item = e.Item as GridEditableItem;
Hashtable values = new Hashtable();
item.ExtractValues(values);
AnnualVacation newAnnualVacation = new AnnualVacation();
item.UpdateValues(newAnnualVacation);
//newAnnualVacation.IsNew = true;
((List<AnnualVacation>)ViewState["AnnualVacationSource"]).Add(newAnnualVacation);
}
catch
{
//Hier fehlermeldung
}
}
protected void rgAnnualVacation_DeleteCommand(object sender, GridCommandEventArgs e)
{
int annualVacationId = int.Parse((e.Item as GridDataItem).GetDataKeyValue("AnnualVacationId").ToString());
((List<AnnualVacation>)ViewState["AnnualVacationSource"]).
Remove(((List<AnnualVacation>)ViewState["AnnualVacationSource"]).
Where(av => av.AnnualVacationId == annualVacationId).FirstOrDefault());
}
protected void rgAnnualVacation_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
rgAnnualVacation.DataSource = ViewState["AnnualVacationSource"];
if (rgAnnualVacation.DataSource == null)
{
rgAnnualVacation.DataSource = String.Empty;
}
}
protected void rgAnnualVacation_UpdateCommand(object sender, GridCommandEventArgs e)
{
}
}
}
答案 0 :(得分:0)
可能数据字段fromdate
和todate
区分大小写。我看到您的GridBoundColumns使用FromDate
和ToDate
。你试过了吗?
答案 1 :(得分:0)
这很奇怪,尝试在插入/编辑UserControl中添加一个空的Page_DataBinding事件处理程序:
Private Sub Page_DataBinding(sender As Object, e As System.EventArgs) Handles Me.DataBinding
End Sub
它适用于我,我将所有内容绑定到OpenAccess。