Please Help...
I'm trying to Bind by GridView to datasource on RowEditing event, but it throws an exception: [TargetInvocationException: Exception has been thrown by the target of an invocation.] Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.
I'm sharing my code.
[CODE BEHIND]
namespace Exchange.MailboxMove.WebUI {
public partial class BlackoutScreen : System.Web.UI.Page
{
protected DateTime FromDateTime;
protected DateTime ToDateTime;
protected string region;
protected string server;
protected bool IsWeekly;
static List<string> listRegions = null;
static List<Server> listServers = null;
static List<Server> lServers = null;
static List<Blackout> lblackout = null;
protected void Page_Load(object sender, EventArgs e)
{
GetBlackouts();
if (!IsPostBack)
{
fillTimePicker();
fillBlackoutControls();
}
}
#region fillBlackoutControls
protected void fillBlackoutControls()
{
try
{
ObjectManager EXMailbox = new ObjectManager(EnvironmentType.Development);
listRegions = EXMailbox.GetRegions();
ddlRegion.DataSource = listRegions;
ddlRegion.DataBind();
ddlRegion.Items.Insert(0, "-- Select --");
ddlRegion.SelectedIndex = 0;
lServers = EXMailbox.GetServers();
listServers = new List<Server>();
if (ddlRegion.SelectedIndex == 0)
{
ddlServer.Items.Clear();
ddlServer.Enabled = false;
}
else
{
ddlServer.Enabled = true;
foreach (Server r in lServers)
if (r.Region == ddlRegion.SelectedValue.ToString())
listServers.Add(r);
ddlServer.DataSource = listServers;
ddlServer.DataTextField = "Name";
ddlServer.DataBind();
}
}
catch (Exception ex)
{
Response.Write(ex);
}
}
#endregion
#region fillTimePicker
protected void fillTimePicker()
{
try
{
ddlFromTimeHour.Items.Clear();
ddlFromTimeHour.Items.Insert(0, "-");
ddlFromTimeMinutes.Items.Clear();
ddlFromTimeMinutes.Items.Insert(0, "-");
ddlFromTimeAMPM.Items.Clear();
ddlFromTimeAMPM.Items.Insert(0, "-");
ddlToTimeHour.Items.Clear();
ddlToTimeHour.Items.Insert(0, "-");
ddlToTimeMinutes.Items.Clear();
ddlToTimeMinutes.Items.Insert(0, "-");
ddlToTimeAMPM.Items.Clear();
ddlToTimeAMPM.Items.Insert(0, "-");
DateTime Time = DateTime.MinValue;
do
{
ddlFromTimeHour.Items.Add(Time.ToString("hh"));
ddlToTimeHour.Items.Add(Time.ToString("hh"));
Time = Time.AddHours(1);
} while (Time.Hour < 12);
do
{
ddlFromTimeMinutes.Items.Add(Time.ToString("mm"));
ddlToTimeMinutes.Items.Add(Time.ToString("mm"));
Time = Time.AddMinutes(15);
} while (Time.Minute != 00);
ddlFromTimeAMPM.Items.Add("AM");
ddlFromTimeAMPM.Items.Add("PM");
ddlToTimeAMPM.Items.Add("AM");
ddlToTimeAMPM.Items.Add("PM");
}
catch (Exception ex)
{
Response.Write(ex);
}
}
#endregion
#region getFromDateTime
protected void getFromDateTime()
{
try
{
String FromTime;
FromTime = ddlFromTimeHour.SelectedItem.Value + ":" + ddlFromTimeMinutes.SelectedItem.Value + " " + ddlFromTimeAMPM.SelectedItem.Value;
String FromDate;
FromDate = txtFromDate.Text;
String StringFromDateTime;
StringFromDateTime = FromDate + " " + FromTime;
FromDateTime = Convert.ToDateTime(StringFromDateTime);
}
catch (Exception ex)
{
Response.Write(ex);
}
}
#endregion
#region getToDateTime
protected void getToDateTime()
{
try
{
String ToTime;
ToTime = ddlToTimeHour.SelectedItem.Value + ":" + ddlToTimeMinutes.SelectedItem.Value + " " + ddlToTimeAMPM.SelectedItem.Value;
String ToDate;
ToDate = txtToDate.Text;
String StringToDateTime;
StringToDateTime = ToDate + " " + ToTime;
ToDateTime = Convert.ToDateTime(StringToDateTime);
}
catch (Exception ex)
{
Response.Write(ex);
}
}
#endregion
#region GetData
protected void getData()
{
getFromDateTime();
getToDateTime();
region = ddlRegion.SelectedItem.Value;
server = ddlServer.SelectedItem.Value;
IsWeekly = cbIsWeekly.Checked;
}
#endregion
#region Create Blackout
protected void btnCreate_Click(object sender, EventArgs e)
{
getData();
try
{
ObjectManager EXMailbox = new ObjectManager(EnvironmentType.Development);
Blackout blackout = new Blackout(FromDateTime, ToDateTime, region, server, IsWeekly);
EXMailbox.CreateBlackout(blackout);
}
catch (Exception ex)
{
Response.Write(ex);
}
//Refresh the Grid View
GetBlackouts();
ClearControls();
}
#endregion
#region GetBlackouts
protected void GetBlackouts()
{
try
{
ObjectManager EXMailbox = new ObjectManager(EnvironmentType.Development);
lblackout = EXMailbox.GetBlackouts();
BlackoutGridView.DataSource=lblackout;
BlackoutGridView.DataBind();
}
catch (Exception ex)
{
Response.Write(ex);
}
}
#endregion
#region ClearControls
public void ClearControls()
{
ddlRegion.SelectedIndex = 0;
ddlServer.SelectedIndex = 0;
txtFromDate.Text = String.Empty;
txtToDate.Text = String.Empty;
ddlFromTimeHour.SelectedIndex = 0;
ddlFromTimeMinutes.SelectedIndex = 0;
ddlFromTimeAMPM.SelectedIndex = 0;
ddlToTimeHour.SelectedIndex = 0;
ddlToTimeMinutes.SelectedIndex = 0;
ddlToTimeAMPM.SelectedIndex = 0;
cbIsWeekly.Checked = false;
}
#endregion
protected void BlackoutGridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
ObjectManager EXMailbox = new ObjectManager(EnvironmentType.Development);
string _blackoutid = BlackoutGridView.Rows[e.RowIndex].Cells[0].Text;
int blackoutid = Int32.Parse(_blackoutid);
EXMailbox.DeleteBlackout(blackoutid);
GetBlackouts();
}
protected void BlackoutGridView_RowEditing(object sender, GridViewEditEventArgs e)
{
BlackoutGridView.EditIndex = e.NewEditIndex;
**BlackoutGridView.DataSource = lblackout;** //Throws exception here.
**BlackoutGridView.DataBind();**
}
protected void BlackoutGridView_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
BlackoutGridView.EditIndex = -1;
BlackoutGridView.DataBind();
}
protected void ddlRegion_SelectedIndexChanged(object sender, EventArgs e)
{
listServers.Clear();
if (ddlRegion.SelectedIndex == 0)
{
ddlServer.Items.Clear();
ddlServer.Enabled = false;
}
else
{
ddlServer.Enabled = true;
foreach (Server r in lServers)
if (r.Region == ddlRegion.SelectedValue.ToString())
listServers.Add(r);
ddlServer.DataSource = listServers;
ddlServer.DataTextField = "Name";
ddlServer.DataBind();
ddlServer.Items.Insert(0, "ALL");
}
}
}
}
[Desginer]
<%@ Page Language="C#" AutoEventWireup="true" Inherits="Exchange.MailboxMove.WebUI.BlackoutScreen" Codebehind="BlackoutScreen.aspx.cs" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
Mailbox Move Tool
/* Highlight Table Columns */ function hCol(n){ var tTab=document.getElementById('theT') for (var i_tem = 0; i_tem<style type="text/css">
.style1
{
width: 17pc;
height: 23px;
}
.style2
{
width: 383px;
}
.style3
{
width: 383px;
height: 23px;
}
.style4
{
width: 508px;
}
.style5
{
width: 508px;
height: 23px;
}
</style>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<div>
<br />
<asp:Label ID="Label1" runat="server" Font-Bold="True" Font-Names="Arial" Font-Size="Small"
ForeColor="Navy" Style="font-size: 14pt" Text="Mailbox Move Tool"></asp:Label><br />
<br />
<table id="theT" width=701px style="height: inherit; background-color : CornflowerBlue">
<tr>
<td style="height: 21px; width: 25pc" align="center">
<asp:Label ID="Label4" runat="server" Style="font-family: Arial"
Text='<a style="text-decoration :none; color: #000000" href="MailboxSearchScreen.aspx">Mailbox Search</a> '></asp:Label></td>
<td style="height: 21px; width: 25pc" align="center">
<asp:Label ID="Label5" runat="server" Style="font-family: Arial"
Text='<a style="text-decoration :none; color: #000000" href="RequestHistoryScreen.aspx">Request History</a> '></asp:Label></td>
<td style="height: 21px; width: 25pc" align="center">
<asp:Label ID="Label35" runat="server" Style="font-family: Arial"
Text='<a style="text-decoration :none; color: #000000" href="ReportScreen.aspx">Report</a> '></asp:Label></td>
<td style="height: 21px; width: 25pc" align="center">
<asp:Label ID="Label18" runat="server" Style="font-family: Arial"
Text='<a style="text-decoration :none; color: #000000" href="BlackoutScreen.aspx">Admin</a> '></asp:Label>
</td>
<td style="height: 21px; width: 25pc" align="right" valign="bottom">
<asp:Label ID="lblLogout" runat="server" Font-Names="arial" Style="font-size: 8pt"
Text="Logout"></asp:Label></td>
</tr>
</table>
<br />
<asp:Panel ID="Panel2" runat="server" BackColor="LightSteelBlue" Height="88px"
Width="703px">
<Table style="width: 700px">
<tr>
<td style="width:17pc">
<asp:Label ID="lblRegion" runat="server"
Style="font-size: 10pt; font-family: Arial" Text="Region:"></asp:Label>
</td>
<td style="width:17pc">
<asp:DropDownList ID="ddlRegion" runat="server" Width="126px"
AutoPostBack="True" onselectedindexchanged="ddlRegion_SelectedIndexChanged">
</asp:DropDownList>
</td>
<td class="style4">
<asp:Label ID="lblFromDate" runat="server" Style="font-size: 10pt;
font-family: Arial" Text="From Date:"></asp:Label>
</td>
<td style="width:17pc">
<asp:TextBox ID="txtFromDate" runat="server"
Style="font-size: 8pt; font-family: Arial" Width="120px"></asp:TextBox>
<cc1:CalendarExtender ID="CalendarExtender1" runat="server" Format="MMMM d, yyyy"
TargetControlID="txtFromDate" >
</cc1:CalendarExtender>
<asp:RequiredFieldValidator runat="server" id="reqFromDate" Text="*" ErrorMessage="From Date is required." controltovalidate="txtFromDate" />
<cc1:ValidatorCalloutExtender ID="reqFromDateValidatorCallout" runat="server" Enabled="True" TargetControlID="reqFromDate">
</cc1:ValidatorCalloutExtender>
</td>
<asp:CustomValidator runat="server" id="reqFromDateCustom" controltovalidate="txtFromDate" Display="Dynamic" ClientValidationFunction="BlackoutValidateDate" />
<cc1:ValidatorCalloutExtender ID="reqFromDateCustomValidatorCallout" runat="server" Enabled="True" TargetControlID="reqFromDateCustom">
</cc1:ValidatorCalloutExtender>
<td class="style2">
<asp:Label ID="lblToDate" runat="server" Style="font-size: 10pt;
font-family: Arial" Text="To Date:"></asp:Label>
</td>
<td style="width:15pc">
<asp:TextBox ID="txtToDate" runat="server"
Style="font-size: 8pt; font-family: Arial" Width="120px"></asp:TextBox>
<cc1:CalendarExtender ID="CalendarExtender2" runat="server" Format="MMMM d, yyyy"
TargetControlID="txtToDate" >
</cc1:CalendarExtender>
<asp:RequiredFieldValidator runat="server" id="reqToDate" Text="*" ErrorMessage="To Date is required." controltovalidate="txtToDate" />
<cc1:ValidatorCalloutExtender ID="reqToDateValidatorCallout" runat="server" Enabled="True" TargetControlID="reqToDate">
</cc1:ValidatorCalloutExtender>
</td>
<asp:CustomValidator runat="server" id="reqToDateCustom" controltovalidate="txtToDate" Display="Dynamic" ClientValidationFunction="BlackoutValidateDate" />
<cc1:ValidatorCalloutExtender ID="reqToDateCustomValidatorCallout" runat="server" Enabled="True" TargetControlID="reqToDateCustom">
</cc1:ValidatorCalloutExtender>
</tr>
<tr>
<td class="style1">
<asp:Label ID="lblServer" runat="server"
Style="font-size: 10pt; font-family: Arial" Text="Server:"></asp:Label>
</td>
<td class="style1">
<asp:DropDownList ID="ddlServer" runat="server" Width="126px">
</asp:DropDownList>
</td>
<td class="style5">
<asp:Label ID="lblFromTime" runat="server" Style="font-size: 10pt;
font-family: Arial" Text="From Time:"></asp:Label>
</td>
<td class="style1">
<asp:DropDownList ID="ddlFromTimeHour" runat="server"
style="margin-left: 0px" Width="38px">
</asp:DropDownList>
<asp:DropDownList ID="ddlFromTimeMinutes" runat="server"
Width="38px">
</asp:DropDownList>
<asp:DropDownList ID="ddlFromTimeAMPM" runat="server"
Width="44px">
</asp:DropDownList>
</td>
<td class="style3">
<asp:Label ID="lblToTime" runat="server" Style="font-size: 10pt;
font-family: Arial" Text="To Time:"></asp:Label>
</td>
<td class="style1">
<asp:DropDownList ID="ddlToTimeHour" runat="server"
style="margin-left: 0px" Width="38px">
</asp:DropDownList>
<asp:DropDownList ID="ddlToTimeMinutes" runat="server"
Width="38px">
</asp:DropDownList>
<asp:DropDownList ID="ddlToTimeAMPM" runat="server"
Width="44px">
</asp:DropDownList>
</td>
</tr>
<tr>
<td class="style1">
<asp:CheckBox ID="cbIsWeekly" runat="server" Font-Names="Arial"
Font-Size="Small" Text="Weekly" TextAlign="Left" />
</td>
<td class="style1">
</td>
<td class="style5">
</td>
<td class="style1">
</td>
<td class="style3">
</td>
<td class="style1" align="center">
<asp:Button ID="btnCreate" runat="server" Text="Create"
onclick="btnCreate_Click" />
</td>
</tr>
</Table>
<br />
</asp:Panel>
<br />
<asp:Panel ID="Panel3" runat="server" BackColor="LightSteelBlue" Width="701px">
<asp:Label ID="Label3" runat="server" BackColor="CornflowerBlue" Style="font-size: 10pt;
font-family: Arial" Text="Current Blackouts" Width="694px"></asp:Label><br />
<asp:GridView ID="BlackoutGridView" runat="server" AutoGenerateColumns="False"
Width="700px" OnRowDeleting="BlackoutGridView_RowDeleting"
OnRowEditing="BlackoutGridView_RowEditing"
onrowcancelingedit="BlackoutGridView_RowCancelingEdit">
<HeaderStyle CssClass="GridHeader" />
<RowStyle CssClass="GridItem" />
<PagerSettings
Position="Bottom"
Mode="NextPreviousFirstLast"
FirstPageText="First"
LastPageText="Last"
NextPageText="Next"
PreviousPageText="Prev" />
<PagerStyle
Font-Names="Arial"
Font-Size="Small"
HorizontalAlign="Right" />
<Columns>
<asp:BoundField DataField="BlackoutId" HeaderText="BlackoutId" >
</asp:BoundField>
<asp:TemplateField HeaderText="Region">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList11" runat="server" Text='<%# Bind("Region") %>'
DataSourceID="lblackout" DataTextField="Region" DataValueField="Region"></asp:DropDownList>
<asp:ObjectDataSource ID="lblackout" runat="server" SelectMethod="GetDataItem"
TypeName="Exchange.MailboxMove.WebUI.BlackoutScreen"></asp:ObjectDataSource>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("Region") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField HeaderText="Server" DataField="Server">
</asp:BoundField>
<asp:BoundField HeaderText="From Date" DataField="StartDateTime"
DataFormatString="{0:D}">
</asp:BoundField>
<asp:BoundField HeaderText="To Date" DataField="EndDateTime"
DataFormatString="{0:D}">
</asp:BoundField>
<asp:BoundField HeaderText="From Time" DataField="StartDateTime"
DataFormatString="{0:t}">
</asp:BoundField>
<asp:BoundField HeaderText="To Time" DataField="EndDateTime"
DataFormatString="{0:t}">
</asp:BoundField>
<asp:CheckBoxField HeaderText="IsWeekly" DataField="IsWeekly" ItemStyle-HorizontalAlign="Center">
<ItemStyle HorizontalAlign="Center" />
</asp:CheckBoxField>
<asp:CommandField DeleteText="Cancel" HeaderText="Action"
ShowDeleteButton="True" ShowEditButton="True" ShowHeader="True"
HeaderStyle-Font-Names="Arial" HeaderStyle-Font-Size="10pt"
ItemStyle-Font-Names="Arial" ItemStyle-Font-Size="8pt"
ItemStyle-HorizontalAlign="Center" >
<HeaderStyle Font-Names="Arial" Font-Size="10pt" />
<ItemStyle Font-Names="Arial" Font-Size="8pt" />
</asp:CommandField>
</Columns>
</asp:GridView>
<br />
</asp:Panel>
<table style="font-size: 10pt; width: 700px; height="20px" font-family: Arial; height: 80px">
<tr align="right">
<td align="right" valign="bottom" style="font-size: 8pt; height">
</td>
</tr>
</table>
<table style="background-color: cornflowerblue" width="701">
<tr>
<td>
</td>
<td>
</td>
<td>
</td>
<td>
</td>
</tr>
</table>
<br />
</div>
</form>
Thanks in advance.