I have been getting this error after the submit button has been pressed on my detailsview which has two databound dropdownlists. I do not use Eval or XPath anywhere in my code, but use Bind often.
Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.InvalidOperationException: Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.
Source Error:
[No relevant source lines]
Source File: c:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\root\9b1781c5\212dfe37\App_Web_newrequestform.aspx.1420f3bc.0khdma4p.0.cs Line: 0
Stack Trace:
[InvalidOperationException: Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.]
System.Web.UI.Page.GetDataItem() +2802118
ASP.authorized_leaverequest_newrequestform_aspx.__DataBinding__control69(Object sender, EventArgs e) in c:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\root\9b1781c5\212dfe37\App_Web_newrequestform.aspx.1420f3bc.0khdma4p.0.cs:0
System.Web.UI.Control.OnDataBinding(EventArgs e) +91
System.Web.UI.WebControls.ListControl.OnDataBinding(EventArgs e) +14
System.Web.UI.WebControls.ListControl.PerformSelect() +34
System.Web.UI.WebControls.BaseDataBoundControl.DataBind() +74
System.Web.UI.WebControls.BaseDataBoundControl.EnsureDataBound() +66
System.Web.UI.WebControls.ListControl.OnPreRender(EventArgs e) +29
System.Web.UI.Control.PreRenderRecursiveInternal() +103
System.Web.UI.Control.PreRenderRecursiveInternal() +175
System.Web.UI.Control.PreRenderRecursiveInternal() +175
System.Web.UI.Control.PreRenderRecursiveInternal() +175
System.Web.UI.Control.PreRenderRecursiveInternal() +175
System.Web.UI.Control.PreRenderRecursiveInternal() +175
System.Web.UI.Control.PreRenderRecursiveInternal() +175
System.Web.UI.Control.PreRenderRecursiveInternal() +175
System.Web.UI.Control.PreRenderRecursiveInternal() +175
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2496
Disassembly points to ---> mov dword ptr [ebp-58h],eax
I have been searching this error for days without luck. I already tried WEBSWAPP as well as searched many forums. Please help.
I am trying to use dropdownlists to let a user select values to fill a database. Every field in my form is a template and is databound.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
using System.Web.Services;
using System.Web.Services.Protocols;
using AjaxControlToolkit;
namespace TimeAndAttendanceUI.Authorized.LeaveRequest
{
public partial class newRequestForm : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DetailsView1.DataBind();
}
}
protected void DetailsView1_DataBound(object sender, EventArgs e)
{
TextBox employeeID = (TextBox)(DetailsView1.FindControl("TextBox17"));
DropDownList approverID = (DropDownList)(DetailsView1.FindControl("DropDownList4"));
if (DetailsView1.CurrentMode == DetailsViewMode.Insert)
{
var CurrUser = "";
Account.Login uusr = new Account.Login();
CurrUser = uusr.User.Identity.Name.ToString().ToUpper();
employeeID.Text = CurrUser.Substring(3, 3);
string supusrid = "";
DataHandler.DataHandler Dt = new DataHandler.DataHandler();
supusrid = Dt.SelectRequestApproverForm(supusrid);
approverID.SelectedValue = supusrid;
}
}
protected void DropDownList6_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList lvtype = (DropDownList)(DetailsView1.FindControl("DropDownList6"));
string selectedValue = lvtype.SelectedValue.ToString().ToUpper();
if (selectedValue == "4 " || selectedValue == "1F")
{
DropDownList lvreason = (DropDownList)(DetailsView1.FindControl("DropDownList5"));
SqlDataSource1.SelectParameters["LEAVECODE"].DefaultValue = selectedValue;
lvreason.Visible = true;
}
}
protected void Button2_Click(object sender, EventArgs e)
{
DetailsView1.DataSource = null;
DetailsView1.DataBind();
}
protected void Button2_Click1(object sender, EventArgs e)
{
Button3.Visible = true;
}
protected void Button3_Click(object sender, EventArgs e)
{
DetailsView1.Visible = true;
DetailsView1.ChangeMode(DetailsViewMode.Insert);
if (DetailsView1.CurrentMode.Equals(DetailsViewMode.Insert))
{
Button3.Visible = false;
Button4.Visible = false;
DetailsView2.Visible = true;
DetailsView3.Visible = true;
}
}
}
}
Sorry for bombarding with code.
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<asp:Label ID="HoursLabel" runat="server"></asp:Label>
<br />
<table class="style1">
<tr>
<td class="style2" colspan="2">
<asp:DetailsView ID="DetailsView1" runat="server"
DataSourceID="ObjectDataSource1" DataKeyNames="bwrequestid"
Height="29px" Width="928px" AutoGenerateRows="False" CellPadding="4" ForeColor="#333333"
GridLines="None" Style="margin-right: 0px; text-align: left; margin-top: 0px;"
ondatabound="DetailsView1_DataBound"
oniteminserted="DetailsView1_ItemInserted"
onitemcommand="DetailsView1_ItemCommand" >
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<CommandRowStyle BackColor="#E2DED6" Font-Bold="True" />
<EditRowStyle BackColor="#999999" />
<FieldHeaderStyle BackColor="#E9ECF1" Font-Bold="True" Width="180px" />
<Fields>
<asp:TemplateField AccessibleHeaderText="Request ID" HeaderText="Leave Request ID" Visible="False">
<EditItemTemplate>
<asp:Label ID="Label18" runat="server" Text='<%# Bind("bwrequestid") %>'></asp:Label>
<asp:TextBox ID="TextBox169" runat="server" Text='<%# bind("bwrequestid") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:Label ID="Label19" runat="server" Text='<%# Bind("bwrequestid") %>'></asp:Label>
<asp:TextBox ID="TextBox165" runat="server" Text='<%# bind("bwrequestid") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("bwrequestid") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField AccessibleHeaderText="Employee ID" HeaderText="Employee ID" >
<EditItemTemplate>
<asp:Label ID="Label20" runat="server" Text='<%# bind("emppid") %>'></asp:Label>
</EditItemTemplate>
<InsertItemTemplate>
<asp:Label ID="Label21" runat="server" Text='<%# bind("emppid") %>'></asp:Label>
<asp:TextBox ID="TextBox17" runat="server" Text='<%# bind("emppid") %>'
></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# bind("emppid") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField AccessibleHeaderText="Approver ID" HeaderText="Approver ID">
<EditItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("approverid") %>' Height="21px"
Width="68px"></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:DropDownList ID="DropDownList4" runat="server" style="text-transform:uppercase;" DataSourceID="SqlDataSourceApprover"
DataTextField="APPROVNAME" DataValueField="APPROVERID"
SelectedValue='<%# Bind("approverid") %>'>
</asp:DropDownList>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("approverid") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Leave Type" SortExpression="reqleavecode">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList7" runat="server" AppendDataBoundItems="True"
AutoPostBack="True" DataSourceID="SQLLEAVECODE"
style="text-transform:uppercase;" DataTextField="LEAVETYPE"
DataValueField="LEAVECODE"
onselectedindexchanged="DropDownList6_SelectedIndexChanged"
SelectedValue='<%# BIND("REQLEAVECODE") %>' ViewStateMode="Enabled"
Width="165px" Height="19px">
<asp:ListItem></asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
<InsertItemTemplate>
<asp:DropDownList ID="DropDownList6" runat="server" DataSourceID="SQLLEAVECODE"
DataTextField="LEAVETYPE" style="text-transform:uppercase;" DataValueField="LEAVECODE"
onselectedindexchanged="DropDownList6_SelectedIndexChanged"
AppendDataBoundItems="True" ViewStateMode="Enabled" AutoPostBack="True"
SelectedValue='<%# BIND("REQLEAVECODE") %>' Width="166px" Height="19px"><asp:ListItem></asp:ListItem>
</asp:DropDownList>
<br />
<asp:Label ID="Label26" runat="server" Text='<%# bind("reqleavecode") %>'></asp:Label>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label9" runat="server" Text='<%# Bind("reqleavecode") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField AccessibleHeaderText="Leave Type" HeaderText="Leave Reason"
Visible="False">
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Bind("leavetype") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("leavetype") %>' Height="21px"
Width="43px"></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:DropDownList ID="DropDownList5" runat="server" AppendDataBoundItems="True"
AutoPostBack="True" DataSourceID="SqlDataSource1" DataTextField="REASON"
DataValueField="REASON" Height="19px"
onselectedindexchanged="DropDownList5_SelectedIndexChanged1"
SelectedValue='<%# Bind("leavetype") %>' Width="163px">
</asp:DropDownList>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1Reason" runat="server"
ControlToValidate="DropDownList5" ErrorMessage="*Leave Reason Required"
ForeColor="Red"></asp:RequiredFieldValidator>
</InsertItemTemplate>
</asp:TemplateField>
<asp:TemplateField AccessibleHeaderText="start date" HeaderText="Leave Date">
<EditItemTemplate>
<asp:TextBox ID="TextBox5" runat="server"
Text='<%# Bind("lstdate", "{0:MM/dd/yyyy}") %>' Height="21px"
Width="72px"></asp:TextBox>
<asp:CalendarExtender ID="TextBox5_CalendarExtender" runat="server" Enabled="True" TargetControlID="TextBox5">
</asp:CalendarExtender>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox5" runat="server"
Text='<%# Bind("lstdate", "{0:MM/dd/yyyy}") %>' Height="21px"
Width="67px"></asp:TextBox>
<asp:CalendarExtender ID="TextBox5_CalendarExtender" runat="server" Enabled="True"
TargetControlID="TextBox5">
</asp:CalendarExtender>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
ControlToValidate="TextBox5" ErrorMessage="*Leave Date Required"
ForeColor="Red"></asp:RequiredFieldValidator>
<br />
<asp:CompareValidator ID="CompareValidator18" runat="server"
ControlToCompare="TextBox7" ControlToValidate="TextBox5"
ErrorMessage="Leave date cannot be after start date" ForeColor="Red"
Operator="LessThanEqual" ToolTip="Must choose start date before end date"></asp:CompareValidator>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label7" runat="server" Text='<%# Bind("lenddate") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField AccessibleHeaderText="lenddate" HeaderText="Return Date">
<EditItemTemplate>
<asp:TextBox ID="TextBox7" runat="server"
Text='<%# Bind("lenddate", "{0:MM/dd/yyyy}") %>' Height="21px" Width="67px"></asp:TextBox>
<asp:CalendarExtender ID="TextBox7_CalendarExtender" runat="server" Enabled="True"
TargetControlID="TextBox7">
</asp:CalendarExtender>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox7" runat="server"
Text='<%# Bind("lenddate", "{0:MM/dd/yyyy}") %>' Height="21px"
Width="67px"></asp:TextBox>
<asp:CalendarExtender ID="TextBox7_CalendarExtender" runat="server" Enabled="True" TargetControlID="TextBox7">
</asp:CalendarExtender>
<asp:CompareValidator ID="CompareValidator1" runat="server"
ControlToCompare="TextBox5" ControlToValidate="TextBox7"
ErrorMessage="Return date cannot be before start date" ForeColor="Red"
Operator="GreaterThanEqual"></asp:CompareValidator>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label15" runat="server" Text='<%# Bind("lsttime") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField AccessibleHeaderText="Leave Time" HeaderText="Leave Time">
<EditItemTemplate>
<asp:TextBox ID="TextBox15" runat="server" Height="21px"
Text='<%# Bind("lsttime") %>' ToolTip="Type 'A' or 'P' to switch AM/PM"
Width="85px"></asp:TextBox>
<asp:MaskedEditExtender ID="TextBox15_MaskedEditExtender" runat="server"
AcceptAMPM="True" CultureAMPMPlaceholder="" CultureCurrencySymbolPlaceholder=""
CultureDateFormat="" CultureDatePlaceholder="" CultureDecimalPlaceholder=""
CultureThousandsPlaceholder="" CultureTimePlaceholder="" Enabled="True"
ErrorTooltipEnabled="True" Mask="99:99" MaskType="Time"
TargetControlID="TextBox15" />
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox15" runat="server" Text='<%# Bind("lsttime") %>' Width="85px"
Height="22px" ToolTip="Type 'A' or 'P' to switch AM/PM"></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label8" runat="server" Text='<%# Bind("lendtime") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField AccessibleHeaderText="Return Time" HeaderText="Return Time">
<EditItemTemplate>
<asp:TextBox ID="TextBox8" runat="server" Text='<%# Bind("lendtime") %>' Width="85px"></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox8" runat="server" Text='<%# Bind("lsttime") %>' Width="85px"
Height="22px" ToolTip="Type 'A' or 'P' to switch AM/PM"></asp:TextBox>
<asp:MaskedEditExtender ID="TextBox8_MaskedEditExtender" runat="server"
AcceptAMPM="True" CultureAMPMPlaceholder="" CultureCurrencySymbolPlaceholder=""
CultureDateFormat="" CultureDatePlaceholder="" CultureDecimalPlaceholder=""
CultureThousandsPlaceholder="" CultureTimePlaceholder="" Enabled="True"
ErrorTooltipEnabled="True" Mask="99:99" MaskType="Time"
TargetControlID="TextBox8" />
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label10" runat="server" Text='<%# Bind("standby") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField AccessibleHeaderText="Standby" HeaderText="On Standby">
<EditItemTemplate>
<asp:TextBox ID="TextBox10" runat="server" Text='<%# Bind("standby") %>' MaxLength="1" Width="21px"></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource10"
DataTextField=""Y/N"" DataValueField=""Y/N"" SelectedValue='<%# BIND("standby") %>'>
</asp:DropDownList>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label12" runat="server" Text='<%# Bind("rsndecline") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField AccessibleHeaderText="Reason Decline"
HeaderText="Request Decline Reason" InsertVisible="False">
<EditItemTemplate>
<asp:TextBox ID="TextBox12" style="text-transform:uppercase;" runat="server" Text='<%# Bind("rsndecline") %>'
Visible="False"></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox12" runat="server" Text='<%# Bind("rsndecline") %>'
Visible="False"></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label13" runat="server" Text='<%# Bind("status") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField AccessibleHeaderText="Status" HeaderText="Request Status" InsertVisible="False">
<EditItemTemplate>
<asp:TextBox ID="TextBox13" runat="server" Text='<%# Bind("status") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox13" runat="server" Text='<%# Bind("status") %>' Visible="False"></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label14" runat="server" Text='<%# Bind("empremarks") %>'
ToolTip="Additional information pertaining to leave request"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField AccessibleHeaderText="empremarks" HeaderText="Leave Request Remarks">
<EditItemTemplate>
<asp:TextBox ID="TextBox14" runat="server" style="text-transform:uppercase;" Text='<%# Bind("empremarks") %>'
TextMode="MultiLine" Height="50px" Width="449px"
ToolTip="Additional information pertaining to leave request"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server"
ControlToValidate="TextBox14" Display="Dynamic" Enabled="False"
ErrorMessage="*Leave Remarks Required for "Other" selection"
ForeColor="Red"></asp:RequiredFieldValidator>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox14" runat="server" style="text-transform:uppercase;" Text='<%# Bind("empremarks") %>'
TextMode="MultiLine" Height="47px" Width="452px"
ToolTip="Additional information pertaining to leave request."></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Button ID="Button1" runat="server" CausesValidation="False"
CommandName="Edit" Height="21px" Text="Edit" Width="65px" />
<asp:Button ID="Button2" runat="server" CausesValidation="False"
CommandName="New" Height="21px" Text="New" Width="65px" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ShowHeader="False">
<EditItemTemplate>
<asp:Button ID="Button1" runat="server" CausesValidation="True"
CommandName="Update" Text="Update" />
<asp:Button ID="Button2" runat="server" CausesValidation="False"
CommandName="Cancel" Text="Cancel" />
</EditItemTemplate>
<InsertItemTemplate>
<asp:Button ID="Button1" runat="server" CommandName="Insert"
Text="Submit Request" />
<asp:Button ID="Button2" runat="server" CausesValidation="False"
CommandName="Cancel" Height="21px" onclick="Button2_Click1" Text="Cancel" />
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label5" runat="server" Text='<%# Bind("lstdate") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Fields>
</asp:DetailsView>
</td>
</tr>
</table>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionStringEvent %>"
ProviderName="<%$ ConnectionStrings:ConnectionStringEvent.ProviderName %>"
SelectCommand="SELECT REASON, LEAVECODE FROM BWLEAVRSN">
<SelectParameters>
<asp:Parameter Name="LEAVECODE" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
<asp:SqlDataSource ID="SQLLEAVECODE" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionStringEvent %>"
ProviderName="<%$ ConnectionStrings:ConnectionStringEvent.ProviderName %>"
SelectCommand="SELECT LEAVETYPE, LEAVECODE FROM BWLVTYPE ORDER BY LEAVECODE">
</asp:SqlDataSource>
<asp:SqlDataSource ID="SqlDataSourceApprover" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionStringEvent %>"
ProviderName="<%$ ConnectionStrings:ConnectionStringEvent.ProviderName %>"
SelectCommand="SELECT APPROVERID, APPROVNAME FROM BWAPPROVER ORDER BY APPROVERID">
</asp:SqlDataSource>
<asp:SqlDataSource ID="SqlDataSource10" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionStringEvent %>"
ProviderName="<%$ ConnectionStrings:ConnectionStringEvent.ProviderName %>"
SelectCommand="SELECT * FROM BWYNSELECT ORDER BY ALPH"></asp:SqlDataSource>
<asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
</asp:ToolkitScriptManager>
<asp:Label ID="Label27" runat="server" ></asp:Label>
</asp:Content>
One thing that I notice, which I suspect is just somehow missing from copy and paste is that the first is missing. I don't think it'd compile so I'm guessing that's not existing in your code. However, it's worth checking.
Your error is probably caused by having the SqlDataSource inside the TemplateField. Move those outside of the DetailsView and give it a whirl.
Before
<asp:DetailsView ....
....
<InsertItemTemplate>
<asp:DropDownList ID="DropDownList4" runat="server" style="text-transform:uppercase;" DataSourceID="SqlDataSourceApprover"
DataTextField="APPROVNAME" DataValueField="APPROVERID"
SelectedValue='<%# Bind("approverid") %>'>
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSourceApprover" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionStringEvent %>"
ProviderName="<%$ ConnectionStrings:ConnectionStringEvent.ProviderName %>"
SelectCommand="SELECT APPROVERID, APPROVNAME FROM BWAPPROVER ORDER BY APPROVERID">
</asp:SqlDataSource>
</InsertItemTemplate>
....
<EditItemTemplate>
<asp:DropDownList ID="DropDownList7" runat="server" AppendDataBoundItems="True"
AutoPostBack="True" DataSourceID="SQLLEAVECODE"
style="text-transform:uppercase;" DataTextField="LEAVETYPE"
DataValueField="LEAVECODE"
onselectedindexchanged="DropDownList6_SelectedIndexChanged"
SelectedValue='<%# BIND("REQLEAVECODE") %>' ViewStateMode="Enabled"
Width="165px" Height="19px">
<asp:ListItem></asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
<InsertItemTemplate>
<asp:DropDownList ID="DropDownList6" runat="server" DataSourceID="SQLLEAVECODE"
DataTextField="LEAVETYPE" style="text-transform:uppercase;" DataValueField="LEAVECODE"
onselectedindexchanged="DropDownList6_SelectedIndexChanged"
AppendDataBoundItems="True" ViewStateMode="Enabled" AutoPostBack="True"
SelectedValue='<%# BIND("REQLEAVECODE") %>' Width="166px" Height="19px"><asp:ListItem></asp:ListItem>
</asp:DropDownList>
<asp:SqlDataSource ID="SQLLEAVECODE" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionStringEvent %>"
ProviderName="<%$ ConnectionStrings:ConnectionStringEvent.ProviderName %>"
SelectCommand="SELECT LEAVETYPE, LEAVECODE FROM BWLVTYPE ORDER BY LEAVECODE">
</asp:SqlDataSource>
<asp:DropDownList ID="DropDownList5" runat="server" AppendDataBoundItems="True"
AutoPostBack="True" DataSourceID="SqlDataSource1" DataTextField="REASON"
DataValueField="REASON" Height="19px"
onselectedindexchanged="DropDownList5_SelectedIndexChanged1"
SelectedValue='<%# Bind("leavetype") %>' Visible="False" Width="163px">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionStringEvent %>"
ProviderName="<%$ ConnectionStrings:ConnectionStringEvent.ProviderName %>"
SelectCommand="SELECT REASON, LEAVECODE FROM BWLEAVRSN">
<SelectParameters>
<asp:ControlParameter ControlID="Label27" Name="LEAVECODE" PropertyName="Text"
Type="String" />
</SelectParameters>
</asp:SqlDataSource>
....
</asp:DetailsView>
After
<asp:DetailsView ....
....
<InsertItemTemplate>
<asp:DropDownList ID="DropDownList4" runat="server" style="text-transform:uppercase;" DataSourceID="SqlDataSourceApprover"
DataTextField="APPROVNAME" DataValueField="APPROVERID"
SelectedValue='<%# Bind("approverid") %>'>
</asp:DropDownList>
</InsertItemTemplate>
....
<EditItemTemplate>
<asp:DropDownList ID="DropDownList7" runat="server" AppendDataBoundItems="True"
AutoPostBack="True" DataSourceID="SQLLEAVECODE"
style="text-transform:uppercase;" DataTextField="LEAVETYPE"
DataValueField="LEAVECODE"
onselectedindexchanged="DropDownList6_SelectedIndexChanged"
SelectedValue='<%# BIND("REQLEAVECODE") %>' ViewStateMode="Enabled"
Width="165px" Height="19px">
<asp:ListItem></asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
<InsertItemTemplate>
<asp:DropDownList ID="DropDownList6" runat="server" DataSourceID="SQLLEAVECODE"
DataTextField="LEAVETYPE" style="text-transform:uppercase;" DataValueField="LEAVECODE"
onselectedindexchanged="DropDownList6_SelectedIndexChanged"
AppendDataBoundItems="True" ViewStateMode="Enabled" AutoPostBack="True"
SelectedValue='<%# BIND("REQLEAVECODE") %>' Width="166px" Height="19px"><asp:ListItem></asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="DropDownList5" runat="server" AppendDataBoundItems="True"
AutoPostBack="True" DataSourceID="SqlDataSource1" DataTextField="REASON"
DataValueField="REASON" Height="19px"
onselectedindexchanged="DropDownList5_SelectedIndexChanged1"
SelectedValue='<%# Bind("leavetype") %>' Visible="False" Width="163px">
</asp:DropDownList>
....
</asp:DetailsView>
<asp:SqlDataSource ID="SqlDataSourceApprover" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionStringEvent %>"
ProviderName="<%$ ConnectionStrings:ConnectionStringEvent.ProviderName %>"
SelectCommand="SELECT APPROVERID, APPROVNAME FROM BWAPPROVER ORDER BY APPROVERID">
</asp:SqlDataSource>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionStringEvent %>"
ProviderName="<%$ ConnectionStrings:ConnectionStringEvent.ProviderName %>"
SelectCommand="SELECT REASON, LEAVECODE FROM BWLEAVRSN">
<SelectParameters>
<asp:ControlParameter ControlID="Label27" Name="LEAVECODE" PropertyName="Text"
Type="String" />
</SelectParameters>
</asp:SqlDataSource>
<asp:SqlDataSource ID="SQLLEAVECODE" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionStringEvent %>"
ProviderName="<%$ ConnectionStrings:ConnectionStringEvent.ProviderName %>"
SelectCommand="SELECT LEAVETYPE, LEAVECODE FROM BWLVTYPE ORDER BY LEAVECODE">
</asp:SqlDataSource>
Alter your SqlDataSource directly for DropDownList5
Your error is now probably caused by the SqlDataSource1 trying to access a Label from inside the DetailsView. This can't really happen, so alter the parameter directly.
Change your parameter type
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionStringEvent %>"
ProviderName="<%$ ConnectionStrings:ConnectionStringEvent.ProviderName %>"
SelectCommand="SELECT REASON, LEAVECODE FROM BWLEAVRSN">
<SelectParameters>
<asp:Parameter Name="LEAVECODE" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
Alter the Parameter
protected void DropDownList6_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList lvtype = (DropDownList)(DetailsView1.FindControl("DropDownList6"));
string selectedValue = lvtype.SelectedValue.ToString().ToUpper();
if (selectedValue == "4 " || selectedValue == "1F")
{
DropDownList lvreason = (DropDownList)(DetailsView1.FindControl("DropDownList5"));
SqlDataSource1.SelectParameters["LEAVECODE"].DefaultValue = selectedValue;
lvreason.Visible = true;
}
}
Related
Am face some troubles to pass data from inner grid to main grid as shown in this pic
im tried too many solution i can found using C# and Jquery
this is simple photo to result
Now this is invoice read from database to show multiple data as shown the grid contain a another grid by normal code it was impossible to access to gridview2 to get data
all i want is get "amount" from gridview2 then calculate "sum" then pass it to "total" in gridview1
also im trid with Jquery it do nothing this is simple code of both fields i want to do sum with Jquery
<script type="text/javascript">
$(function () {
var fields = document.getElementsByClassName('clstocal');
var sum = 0;
for (var i = 0; i < fields.length; ++i) {
var item = fields[i];
sum += parseInt(item.innerHTML);
}
$("#totalprice").text(sum);
}); // this code get it from this site
</script>
<asp:TextBox ID="amount" CssClass ="clstocal" autopostback="true" runat="server" Enabled="False" Height="24px" Text='<%# Bind("body_total") %>' Width="98px" OnTextChanged="amount_TextChanged"></asp:TextBox>
and
<asp:TextBox ID="totalprice" runat="server" Enabled="False" Height="22px" Width="145px" OnTextChanged="totalprice_TextChanged" autopostback="true"></asp:TextBox>
there are a easy solution but not a good solution is sum the result to database then read it again but this is not efficient at all ..
To be honest the answer was simple
1. its impossible to access grid inside grid
2. solved by normal code in C# as shown
protected void GridHead(object sender, GridViewRowEventArgs e)
{
// MAin Grid which is grid number 1
if (e.Row.RowType == DataControlRowType.DataRow)
{
TextBox textboxPrice1 = e.Row.FindControl("totalprice") as TextBox;
textboxPrice1.Text = i.ToString();
i = 0; // Global double -> protected double i = 0; // THIS LINE TO STOP COUNTING TO OTHER NEXT INVOICE
}
}
protected void GridBody(object sender, GridViewRowEventArgs e)
{
// Inner grid which contain multipe item need to sun then send it to Main grid
if (e.Row.RowType == DataControlRowType.DataRow)
{
TextBox lblPrice2 = e.Row.FindControl("amount") as TextBox;
i += Convert.ToDouble(lblPrice2.Text);
}
}
and the code behind to handle the grid
<%# Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Reports._Default" %>
Welcome to Invoice Revision
<!-- change both grid from OnSelectedIndexChanged to onrowdatabound to use GridViewRowEventArgs instade EventArgs-->
<asp:GridView ID="GD_Head" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" Height="206px" Width="548px" AllowSorting="True" CellPadding="4" ForeColor="#333333" GridLines="None" onrowdatabound="GridHead">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<br />
<asp:Label ID="Label25" runat="server" Height="22px" Text="Invoice : " Width="60px"></asp:Label>
<asp:Label ID="Label1" width="145px" runat="server" Text='<%# Eval("invoice") %>' BorderStyle="Solid" BorderWidth="1px" Height="22px" Font-Size="Smaller"></asp:Label>
<asp:Label ID="Label26" runat="server" Height="22px" Text="Kind : " Width="60px"></asp:Label>
<asp:Label ID="Label35" runat="server" BorderWidth="1px" Height="22px" Text='<%# Eval("kind") %>' Width="71px"></asp:Label>
<asp:Label ID="Label27" runat="server" Height="22px" Text="Date : " Width="60px"></asp:Label>
<asp:Label ID="Label3" width="170px" runat="server" Text='<%# Eval("date") %>' BorderWidth="1px" Height="22px"></asp:Label>
<br />
<br />
<asp:Label ID="Label28" runat="server" Height="22px" Text="File : " Width="60px"></asp:Label>
<asp:Label ID="Label4" width="144px" runat="server" Text='<%# Eval("file") %>' BorderWidth="1px" Height="22px"></asp:Label>
<asp:Label ID="Label29" runat="server" Height="22px" Text="Awb" Width="60px"></asp:Label>
<asp:Label ID="Label5" width="313px" runat="server" Text='<%# Eval("awb") %>' BorderWidth="1px" Height="22px"></asp:Label>
<br />
<br />
<asp:Label ID="Label30" runat="server" Height="22px" Text="GL : " Width="60px"></asp:Label>
<asp:Label ID="Label6" width="144px" runat="server" Text='<%# Eval("gl") %>' BorderWidth="1px" Height="22px"></asp:Label>
<asp:Label ID="Label7" width="376px" runat="server" Text='<%# Eval("name") %>' BorderWidth="1px" Height="23px"></asp:Label>
<br />
<br />
<asp:Label ID="Label31" runat="server" Height="22px" Text="Total : " Width="60px"></asp:Label>
<!-- Convert String to int then sum them from body to head -->
<asp:TextBox ID="totalprice" OnTextChanged ="totalprice_TextChanged" runat="server" Enabled="False" Height="22px" Width="145px"></asp:TextBox>
<asp:Label ID="lb12" runat="server" BorderWidth="1px" Height="22px" Text='<%# Eval("curId") %>' Width="38px"></asp:Label>
<asp:Label ID="Label32" runat="server" Height="22px" Text="Rate : " Width="40px"></asp:Label>
<asp:Label ID="Label9" width="100px" runat="server" Text='<%# Eval("rate") %>' BorderStyle="Solid" BorderWidth="1px" Height="22px"></asp:Label>
<asp:Label ID="Label33" runat="server" Height="22px" Text="Emp" Width="40px"></asp:Label>
<asp:Label ID="lb10" width="65px" runat="server" Text='<%# Eval("emp") %>' BorderStyle="Solid" BorderWidth="1px" Height="22px"></asp:Label>
<asp:Label ID="Label34" runat="server" BorderWidth="1px" Height="22px" Text='<%# Eval("poste") %>' Width="62px"></asp:Label>
<br />
<br />
<asp:Label ID="Label14" runat="server" Height="22px" Text="No" Width="52px"></asp:Label>
<asp:Label ID="Label23" width="76px" runat="server" Text='GL' Height="22px"></asp:Label>
<asp:Label ID="Label18" runat="server" Height="22px" Text="Description" Width="250px"></asp:Label>
<asp:Label ID="Label19" runat="server" Height="22px" Text="Amount" Width="130px"></asp:Label>
<!-- change both grid from OnSelectedIndexChanged to onrowdatabound to use GridViewRowEventArgs instade EventArgs-->
<asp:GridView ID="GD_Body" runat="server" AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="serial" DataSourceID="SqlDataSource2" Height="39px" onrowdatabound="GridBody" Width="629px" GridLines="None">
<Columns>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:Label ID="lb40" runat="server" BorderWidth="1px" Height="22px" Text='<%# Eval("line_no") %>' Width="52px"></asp:Label>
<asp:Label ID="Label41" runat="server" BorderWidth="1px" Height="22px" Text='<%# Eval("body_gl") %>' Width="76px"></asp:Label>
<asp:Label ID="Label42" runat="server" BorderWidth="1px" Height="22px" Text='<%# Eval("body_name") %>' Width="250px"></asp:Label>
<asp:TextBox ID="amount" CssClass ="clstocal" runat="server" Enabled="False" Height="24px" Text='<%# Bind("body_total") %>' Width="98px"></asp:TextBox>
<asp:Label ID="UN" runat="server" BorderWidth="1px" Height="22px" Text="UN" Width="52px"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<br />
<asp:Label ID="Label22" runat="server" Text="---------------------------------------------------------------------------------------------------------------------" Width="600px"></asp:Label>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:Body %>" SelectCommand="SELECT * FROM [body] WHERE ([invoice] = #invoice)">
<SelectParameters>
<asp:ControlParameter ControlID="Label1" Name="invoice" PropertyName="Text" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
<br />
</ItemTemplate>
<ControlStyle Font-Bold="True" />
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
<RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
<SortedAscendingCellStyle BackColor="#FDF5AC" />
<SortedAscendingHeaderStyle BackColor="#4D0000" />
<SortedDescendingCellStyle BackColor="#FCF6C0" />
<SortedDescendingHeaderStyle BackColor="#820000" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:Head %>" SelectCommand="SELECT * FROM [head]"></asp:SqlDataSource>
and this is result
I hope this answer find you
if you have better answer please share with us
I have a parent grid GVInvoiceDet. CRUD operation on this grid is working fine. In each row of the parent, I have child grid GVTaxDetails which opens on click of image ie expand/collapse feature.
I am looking for CRUD within child grid also. But when I click on edit within child, the texbox for editing doesn't appear and the parent grid collapses.
Here is child grid rowediting code
protected void GVTaxDetails_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView gvTemp = (GridView)sender;
gvUniqueID = gvTemp.UniqueID;
//gvEditIndex = e.NewEditIndex;
gvTemp.EditIndex = e.NewEditIndex;
GVInvoiceDet.DataBind();
}
Parent row editing code
protected void GVInvoiceDet_RowEditing(object sender, GridViewEditEventArgs e)
{
GVInvoiceDet.EditIndex = e.NewEditIndex;
GVInvoiceDet.DataBind();
BindItemGrid();
}
ASPX file :
<asp:GridView ID="GVInvoiceDet" runat="server" Width="100%" AutoGenerateColumns="False"
HorizontalAlign="Left" BorderStyle="Outset" OnRowCancelingEdit="GVInvoiceDet_RowCancelingEdit"
OnRowEditing="GVInvoiceDet_RowEditing" OnRowDataBound="GVInvoiceDet_RowDataBound" ShowFooter="True"
OnRowCommand="GVInvoiceDet_RowCommand" OnRowUpdating="GVInvoiceDet_RowUpdating"
OnRowDeleting="GVInvoiceDet_RowDeleting" OnRowDeleted="GVInvoiceDet_RowDeleted"
OnRowUpdated="GVInvoiceDet_RowUpdated" AllowSorting="True" DataSourceID="DSItemGrid" DataKeyNames="invoiceno,itemcd">
<HeaderStyle BackColor="#0000ff" ForeColor="Yellow" />
<FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />
<EditRowStyle BackColor="#FFFFCC" ForeColor="#330099" />
<AlternatingRowStyle BackColor="#FE9CD5" />
<Columns>
<asp:TemplateField>
<ItemTemplate>
<a href="javascript:expandcollapse('div<%# Eval("itemcd") %>', 'one');">
<img id="imgdiv<%# Eval("itemcd") %>" alt="Click to show/hide Tax Details <%# Eval("itemcd") %>" border="0" src="Images/plus.png" />
</a>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-Width="10%" HeaderText="Item Code" SortExpression="itemcd">
<ItemTemplate>
<asp:Label ID="lblItemCode" runat="server" Text='<%# Eval("itemcd")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtItemCode" Width="90%" runat="server" Text='<%# Eval("itemcd")%>' Enabled="false"></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtItemCode" Width="90%" runat="server" Text='' Enabled="false"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-Width="10%" HeaderText="HSN Code" SortExpression="hsncd">
<ItemTemplate>
<asp:Label ID="lblItemHSNCode" runat="server" Text='<%# Eval("hsncd")%>'></asp:Label><asp:HiddenField ID="hdnItemGST" runat="server" Value='<%# Eval("itempercentage") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtItemHSN" Width="90%" runat="server" Text='<%# Eval("hsncd")%>' Enabled="false"></asp:TextBox><asp:HiddenField ID="hdnEditItemGST" runat="server" Value='<%# Eval("itempercentage") %>' />
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtItemHSN" Width="90%" runat="server" Text='' Enabled="false"></asp:TextBox><asp:HiddenField ID="hdnFooterItemGST" runat="server" Value='<%# Eval("itempercentage") %>' />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-Width="25%" HeaderText="Item Description" SortExpression="itemdesc">
<ItemTemplate>
<asp:Label ID="lblItemDesc" runat="server" Text='<%# Eval("itemdesc")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:Label ID="lblItemDesc" runat="server" Text='<%# Eval("itemdesc")%>' Visible="false"></asp:Label><asp:HiddenField ID="hdnItemDesc" runat="server" Value='<%# String.Format("{0}-{1}-{2}",Eval("itemcd"),Eval("hsncd"),Eval("itempercentage")) %>' />
<asp:DropDownList ID="eddlItemDesc" runat="server" AutoPostBack="true" OnSelectedIndexChanged="eddlItemDesc_SelectedIndexChanged"></asp:DropDownList>
</EditItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="fddlItemDesc" runat="server" AutoPostBack="true" OnSelectedIndexChanged="fddlItemDesc_SelectedIndexChanged"></asp:DropDownList>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-Width="5%" HeaderText="Quantity">
<ItemTemplate>
<asp:Label ID="lblItemQty" runat="server" Text='<%# Eval("quantity")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtItemQty" Width="90%" runat="server" Text='<%# Eval("quantity")%>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtItemQty" Width="90%" MaxLength="100" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-Width="5%" HeaderText="Unit Rate">
<ItemTemplate>
<asp:Label ID="lblItemUnitRate" runat="server" Text='<%# Eval("unitrate")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtItemUnitRate" Width="90%" runat="server" Text='<%# Eval("unitrate")%>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtItemUnitRate" Width="90%" MaxLength="100" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-Width="5%" HeaderText="Unit Value">
<ItemTemplate>
<asp:Label ID="lblItemUnitValue" runat="server" Text='<%# Eval("unitvalue")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtItemUnitValue" Width="90%" runat="server" Text='<%# Eval("unitvalue")%>' Enabled="false"></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtItemUnitValue" Width="90%" MaxLength="100" runat="server" Enabled="false"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-Width="5%" HeaderText="Final Value">
<ItemTemplate>
<asp:Label ID="lblItemFinalValue" runat="server" Text=''></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtItemFinalValue" Width="90%" runat="server" Text='' Enabled="false"></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtItemFinalValue" Width="90%" MaxLength="100" runat="server" Enabled="false"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="True" HeaderText="Edit" />
<asp:TemplateField HeaderText="Delete" ItemStyle-Width="5%">
<ItemTemplate>
<asp:LinkButton ID="linkDeleteInvoiceItem" CommandName="DeleteItem" runat="server">Delete</asp:LinkButton>
</ItemTemplate>
<FooterTemplate>
<asp:LinkButton ID="linkAddInvoiceItem" CommandName="AddItem" runat="server">Add</asp:LinkButton>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<tr>
<td colspan="100%">
<div id="div<%# Eval("itemcd") %>" style="display: none; position: relative; left: 15px; overflow: auto; width: 97%">
<asp:GridView ID="GVTaxDetails" BackColor="White" Width="100%" Font-Size="Small" AutoGenerateColumns="false"
runat="server" DataKeyNames="invoiceno,itemcd,valuecd" ShowFooter="True" OnRowUpdating="GVTaxDetails_RowUpdating"
OnRowCommand="GVTaxDetails_RowCommand" OnRowEditing="GVTaxDetails_RowEditing" GridLines="None"
OnRowUpdated="GVTaxDetails_RowUpdated" OnRowCancelingEdit="GVTaxDetails_CancelingEdit"
OnRowDataBound="GVTaxDetails_RowDataBound" OnRowDeleting="GVTaxDetails_RowDeleting" OnRowDeleted="GVTaxDetails_RowDeleted"
ShowHeaderWhenEmpty="true" BorderStyle="Double" BorderColor="#0083C1">
<RowStyle BackColor="Gainsboro" />
<AlternatingRowStyle BackColor="White" />
<HeaderStyle BackColor="#0083C1" ForeColor="White" />
<FooterStyle BackColor="White" />
<Columns>
<asp:TemplateField ItemStyle-Width="5%" HeaderText="SL No">
<ItemTemplate>
<asp:Label ID="lblViewTaxSlno" runat="server" Text='<%# Eval("slno")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtViewTaxSlno" runat="server" Text='<%# Eval("slno")%>' Enabled="false"></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtViewTaxSlno" MaxLength="3" Width="80%" runat="server" Text='<%# Convert.ToInt32(Container.DataItemIndex) + 1 %>' Enabled="false"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-Width="15%" HeaderText="Value">
<ItemTemplate>
<asp:HiddenField ID="hdnViewTaxDesc" runat="server" Value='<%# Eval("valuecd") %>' />
<asp:Label ID="lblViewTaxDesc" runat="server" Text='<%# Eval("valuedesc")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:HiddenField ID="hdnViewTaxDesc" runat="server" Value='<%# Eval("valuecd") %>' />
<asp:DropDownList ID="eddlViewTaxDesc" Width="90%" runat="server" AutoPostBack="true" OnSelectedIndexChanged="eddViewTaxDesc_SelectedIndexChanged"></asp:DropDownList>
</EditItemTemplate>
<FooterTemplate>
<asp:HiddenField ID="hdnViewTaxDesc" runat="server" Value='<%# Eval("valuecd") %>' />
<asp:DropDownList ID="fddViewTaxDesc" Width="90%" runat="server" OnSelectedIndexChanged="fddlViewTaxDesc_SelectedIndexChanged"></asp:DropDownList>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-Width="3%" HeaderText="Amount">
<ItemTemplate>
<asp:Label ID="lblViewTaxAmount" runat="server" Text='<%# Eval("value")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtViewTaxAmount" runat="server" Text='<%# Eval("value")%>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtViewTaxAmount" runat="server" Text='<%# Eval("value")%>'></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-Width="7%" HeaderText="Ref. Sl No">
<ItemTemplate>
<asp:Label ID="lblViewTaxRefNo" runat="server" Text='<%# Eval("refslno")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:HiddenField ID="hdnViewTaxRefNo" runat="server" Value='<%# Eval("refslno") %>' />
<asp:DropDownList ID="eddlViewTaxRefNo" Width="90%" runat="server" AutoPostBack="false"></asp:DropDownList>
</EditItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="fddViewTaxRefNo" Width="90%" runat="server" AutoPostBack="false"></asp:DropDownList>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-Width="10%" HeaderText="Tax-Mode">
<ItemTemplate>
<asp:Label ID="lblViewTaxMode" runat="server" Text='<%# Eval("absper").ToString()=="A"?"Absolute":"Percentage"%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:HiddenField ID="hdnViewTaxMode" runat="server" Value='<%# Eval("absper") %>' />
<asp:DropDownList ID="eddlViewTaxMode" Width="90%" runat="server" AutoPostBack="false">
<asp:ListItem Value="-1" Text="-Select Mode-" Selected="True"></asp:ListItem>
<asp:ListItem Value="A" Text="Absolute"></asp:ListItem>
<asp:ListItem Value="P" Text="Percentage"></asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="fddlViewTaxMode" Width="95%" runat="server" AutoPostBack="false">
<asp:ListItem Value="-1" Text="-Select Mode-" Selected="True"></asp:ListItem>
<asp:ListItem Value="A" Text="Absolute"></asp:ListItem>
<asp:ListItem Value="P" Text="Percentage"></asp:ListItem>
</asp:DropDownList>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-Width="10%" HeaderText="Operation">
<ItemTemplate>
<asp:Label ID="lblViewTaxOperation" runat="server" Text='<%# Eval("addsub").ToString()=="A"?"Add":"Subtract"%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:HiddenField ID="hdnViewTaxOperation" runat="server" Value='<%# Eval("addsub") %>' />
<asp:DropDownList ID="eddlViewTaxOperation" Width="90%" runat="server" AutoPostBack="false">
<asp:ListItem Value="-1" Text="-Select Operation-" Selected="True"></asp:ListItem>
<asp:ListItem Value="A" Text="Add"></asp:ListItem>
<asp:ListItem Value="S" Text="Subtract"></asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="fddlViewTaxOperation" Width="90%" runat="server" AutoPostBack="false">
<asp:ListItem Value="-1" Text="-Select Operation-" Selected="True"></asp:ListItem>
<asp:ListItem Value="A" Text="Add"></asp:ListItem>
<asp:ListItem Value="S" Text="Subtract"></asp:ListItem>
</asp:DropDownList>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-Width="3%" HeaderText="Final Amount">
<ItemTemplate>
<asp:Label ID="lblViewTaxFinalAmount" runat="server" Text='<%# Eval("finalvalue")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtViewTaxFinalAmount" runat="server" Text='<%# Eval("finalvalue")%>' Enabled="false"></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtViewTaxFinalAmount" runat="server" Text='<%# Eval("finalvalue")%>' Enabled="false"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:CommandField HeaderText="Edit" ShowEditButton="True" />
<asp:TemplateField HeaderText="Delete">
<ItemTemplate>
<asp:LinkButton ID="linkDeleteTaxDetails" CommandName="Delete" runat="server">Delete</asp:LinkButton>
</ItemTemplate>
<FooterTemplate>
<asp:LinkButton ID="linkAddTaxDetails" CommandName="AddTax" runat="server">Add</asp:LinkButton>
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</td>
</tr>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="DSItemGrid" runat="server" ConnectionString="<%$ ConnectionStrings:GSTCS %>" ProviderName="<%$ ConnectionStrings:GSTCS.ProviderName %>"
SelectCommand="select invoiceno, dt.itemcd itemcd, mast.itemdesc itemdesc, mast.hsncd hsncd, dt.quantity quantity,
mast.unitrate unitrate, dt.unitvalue unitvalue, mast.itempercentage itempercentage
from GSTINVOICEDT dt join GSTITEMMASTER mast on (dt.itemcd = mast.itemcd and dt.unitcode = mast.unitcode and dt.projectcode = mast.projectcode)
where dt.unitcode = :unitCode and dt.projectCode = :projCode and dt.invoiceno = :invoiceNo order by invoiceno, itemcd">
<SelectParameters>
<asp:SessionParameter DefaultValue="05" Name="unitCode" SessionField="unit" />
<asp:SessionParameter DefaultValue="0501" Name="projCode" SessionField="project" />
<asp:SessionParameter DefaultValue="INVOICENO" Name="invoiceNo" SessionField="invoiceNo" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
I got the work done... Posting the code for others if come across similar situation.
In parent rowdatabound have added code to keep the div open and update the editIndex.
protected void GVInvoiceDet_RowDataBound(object sender, GridViewRowEventArgs e) {
GridViewRow row = e.Row;
if(row.RowType == DataControlRowType.Header)
{
return;
}
else if (row.RowType == DataControlRowType.DataRow )
{
//Find Child GridView control
GridView gvTax = new GridView();
gvTax = (GridView)row.FindControl("GVTaxDetails");
if (gvTax.UniqueID == gvUniqueID)
{
gvTax.EditIndex = gvEditIndex;
//Expand the Child grid
ClientScript.RegisterStartupScript(GetType(), "Expand", "<SCRIPT LANGUAGE='javascript'>expandcollapse('div" + ((DataRowView)e.Row.DataItem)["itemcd"].ToString() + "','one');</script>");
}
}
}
expandcollapse is function on aspx side:
function expandcollapse(obj, row) {
var div = document.getElementById(obj);
var img = document.getElementById('img' + obj);
if (div.style.display == "none") {
div.style.display = "block";
if (row == 'alt') {
img.src = "Images/minus.png";
}
else {
img.src = "Images/minus.png";
}
img.alt = "Close View Taxes";
}
else {
div.style.display = "none";
if (row == 'alt') {
img.src = "Images/plus.png";
}
else {
img.src = "Images/plus.png";
}
img.alt = "Expand View Taxes";
}
}
1. my .aspx code is
asp:GridView ID="gvaccmaster" DataKeyNames="id"
runat="server" AutoGenerateColumns="False"
HeaderStyle-Font-Bold="true"
OnRowCancelingEdit="gridView_RowCancelingEdit"
OnRowDeleting="gridView_RowDeleting"
OnRowEditing="gridView_RowEditing"
OnRowUpdating="gridView_RowUpdating"
OnRowDataBound="gridView_RowDataBound" Font-Names="Arial"
Font-Strikeout="False" Font-Underline="False"
OnPageIndexChanging="OnPageIndexChanging" CellPadding="2"
Style="text-align: center; vertical-align: middle"
BorderColor="Black" BorderWidth="2px"> <AlternatingRowStyle
BorderStyle="None" /> <Columns> <asp:TemplateField HeaderText="ID"
ConvertEmptyStringToNull="False"> <ItemTemplate> <asp:Label ID="ID"
runat="server" Text='<%#Eval("id") %>' /> </ItemTemplate>
<EditItemTemplate> <asp:Label ID="id" runat="server"
Text='<%#Eval("id") %>' /> </EditItemTemplate> </asp:TemplateField>
<asp:TemplateField HeaderText="Name"
ConvertEmptyStringToNull="False"> <ItemTemplate> <asp:Label
ID="name" runat="server" Text='<%#Eval("name") %>' />
</ItemTemplate> <EditItemTemplate> <asp:TextBox ID="txtname"
runat="server" Text='<%#Eval("name") %>' />
<asp:RequiredFieldValidator runat="server" ID="rfv0"
ControlToValidate="txtname" ErrorMessage="*" ForeColor="Red"
ValidationGroup="validaiton" /> </EditItemTemplate>
</asp:TemplateField> <asp:TemplateField HeaderText="Opening
Balance"> <ItemTemplate> <asp:Label ID="opbalance" runat="server"
Text='<%#Eval("openingbalance") %>' /> </ItemTemplate>
<EditItemTemplate> <asp:TextBox ID="txtopbalance" runat="server"
Text='<%#Eval("openingbalance") %>' /> <asp:RequiredFieldValidator
runat="server" ID="rfv1" ControlToValidate="txtopbalance"
ErrorMessage="*" ForeColor="Red" ValidationGroup="validaiton" />
<asp:RegularExpressionValidator ID="rev4" runat="server"
ErrorMessage="invalid opening balance" ValidationGroup="validaiton"
ControlToValidate="txtopbalance" ForeColor="Red"
ValidationExpression="^\d+$"> </asp:RegularExpressionValidator>
</EditItemTemplate> </asp:TemplateField> <asp:TemplateField
HeaderText="Contact Person"> <ItemTemplate> <asp:Label ID="cperson"
runat="server" Text='<%#Eval("contactperson") %>' /> </ItemTemplate>
<EditItemTemplate> <asp:TextBox ID="txtcperson" runat="server"
Text='<%#Eval("contactperson") %>' /> <asp:RequiredFieldValidator
runat="server" ID="rfv2" ControlToValidate="txtcperson"
ErrorMessage="*" ForeColor="Red" ValidationGroup="validaiton" />
</EditItemTemplate> </asp:TemplateField> <asp:TemplateField
HeaderText="Address"> <ItemTemplate> <asp:Label ID="address"
runat="server" Text='<%#Eval("address") %>' /> </ItemTemplate>
<EditItemTemplate> <asp:TextBox ID="txtaddress" runat="server"
Text='<%#Eval("address") %>' /> <asp:RequiredFieldValidator
runat="server" ID="rfv3" ControlToValidate="txtaddress"
ErrorMessage="*" ForeColor="Red" ValidationGroup="validaiton" />
</EditItemTemplate> </asp:TemplateField> <asp:TemplateField
HeaderText="City"> <ItemTemplate> <asp:Label ID="city"
runat="server" Text='<%#Eval("city") %>' /> </ItemTemplate>enter
code here <EditItemTemplate> <asp:DropDownList ID="ddlcity"
runat="server" Height="25px" Width="100px">
<asp:ListItem>Ahmedabad</asp:ListItem>
<asp:ListItem>Baroda</asp:ListItem>
<asp:ListItem>Rajkot</asp:ListItem>
<asp:ListItem>Udaipur</asp:ListItem>
<asp:ListItem>Jaipur</asp:ListItem>
<asp:ListItem>Ajmer</asp:ListItem>
<asp:ListItem>Jodhpur</asp:ListItem>
<asp:ListItem>Bhopal</asp:ListItem>
<asp:ListItem>Indore</asp:ListItem>
<asp:ListItem>Gwalior</asp:ListItem>
<asp:ListItem>Ujjain</asp:ListItem>
<asp:ListItem>Mumbai</asp:ListItem>
<asp:ListItem>Pune</asp:ListItem>
<asp:ListItem>Nagpur</asp:ListItem>
<asp:ListItem>Nasik</asp:ListItem>
<asp:ListItem>Aurangabad</asp:ListItem>
<asp:ListItem>Akola</asp:ListItem> </asp:DropDownList>
</EditItemTemplate> </asp:TemplateField> <asp:TemplateField
HeaderText="State"> <ItemTemplate> <asp:Label ID="state"
runat="server" Text='<%#Eval("state") %>' /> </ItemTemplate>
<EditItemTemplate> <asp:DropDownList ID="ddlstate" runat="server"
Height="25px" Width="150px"> <asp:ListItem>Gujarat</asp:ListItem>
<asp:ListItem>Rajasthan</asp:ListItem> <asp:ListItem>Madhya
Pradesh</asp:ListItem> <asp:ListItem>Maharashtra</asp:ListItem>
</asp:DropDownList> </EditItemTemplate> </asp:TemplateField>
<asp:TemplateField HeaderText="Area"> <ItemTemplate> <asp:Label
ID="area" runat="server" Text='<%#Eval("area") %>' />
</ItemTemplate> <EditItemTemplate> <asp:TextBox ID="txtarea"
runat="server" Text='<%#Eval("area") %>' />
<asp:RequiredFieldValidator runat="server" ID="rfv4"
ControlToValidate="txtarea" ErrorMessage="*" ForeColor="Red"
ValidationGroup="validaiton" /> </EditItemTemplate>
</asp:TemplateField> <asp:TemplateField HeaderText="Pin Code">
<ItemTemplate> <asp:Label ID="pincode" runat="server"
Text='<%#Eval("pincode") %>' /> </ItemTemplate> <EditItemTemplate>
<asp:TextBox ID="txtpincode" runat="server" Text='<%#Eval("pincode")
%>' /> <asp:RequiredFieldValidator runat="server" ID="rfv5"
ControlToValidate="txtpincode" ErrorMessage="*" ForeColor="Red"
ValidationGroup="validaiton" /> <asp:RegularExpressionValidator
ID="rev3" runat="server" ErrorMessage="invalid pincode"
ValidationGroup="validaiton" ControlToValidate="txtpincode"
ForeColor="Red" ValidationExpression="\d{6}">
</asp:RegularExpressionValidator> </EditItemTemplate>
</asp:TemplateField> <asp:TemplateField HeaderText="Mobile">
<ItemTemplate> <asp:Label ID="mobile" runat="server"
Text='<%#Eval("mobile") %>' /> </ItemTemplate> <EditItemTemplate>
<asp:TextBox ID="txtmobile" runat="server" Text='<%#Eval("mobile")
%>' /> <asp:RequiredFieldValidator runat="server" ID="rfv6"
ControlToValidate="txtmobile" ErrorMessage="*" ForeColor="Red"
ValidationGroup="validaiton" /> <asp:RegularExpressionValidator
ID="rev2" runat="server" ErrorMessage="invalid mobile"
ValidationGroup="validaiton" ControlToValidate="txtmobile"
ForeColor="Red" ValidationExpression="^[7-9][0-9]{9}$">
</asp:RegularExpressionValidator> </EditItemTemplate>
</asp:TemplateField> <asp:TemplateField HeaderText="E-Mail">
<ItemTemplate> <asp:Label ID="email" runat="server"
Text='<%#Eval("email") %>' /> </ItemTemplate> <EditItemTemplate>
<asp:TextBox ID="txtemail" runat="server" Text='<%#Eval("email") %>'
/> <asp:RequiredFieldValidator runat="server" ID="rfv7"
ControlToValidate="txtemail" ErrorMessage="*" ForeColor="Red"
ValidationGroup="validaiton" /> <asp:RegularExpressionValidator
ID="rev1" runat="server" ErrorMessage="invalid email"
ValidationGroup="validaiton" ControlToValidate="txtemail"
ForeColor="Red"
ValidationExpression="\w+([-+.']\w+)*#\w+([-.]\w+)*\.\w+([-.]\w+)*">
</asp:RegularExpressionValidator> </EditItemTemplate>
</asp:TemplateField> <asp:TemplateField HeaderText="Actions">
<ItemTemplate> <asp:Button ID="ButtonEdit" runat="server"
CommandName="Edit" Text="Edit" Width="50px" /> <asp:Button
ID="ButtonDelete" runat="server" CommandName="Delete" Text="Delete"
Width="50px" /> </ItemTemplate> <EditItemTemplate> <asp:Button
ID="ButtonUpdate" runat="server" CommandName="Update" Text="Update"
ValidationGroup="validaiton" Width="60px" /> <asp:Button
ID="ButtonCancel" runat="server" CommandName="Cancel" Text="Cancel"
Width="60px" /> </EditItemTemplate> </asp:TemplateField> </Columns>
<EditRowStyle BorderStyle="Solid" BorderWidth="2px" /> <HeaderStyle
BorderColor="Black" BorderStyle="Solid" BorderWidth="2px"
Font-Names="Cambria" Font-Size="Large" BackColor="#CCCCCC"
Font-Bold="False" ForeColor="Blue" HorizontalAlign="Center"
VerticalAlign="Middle"></HeaderStyle> <RowStyle
HorizontalAlign="Center" VerticalAlign="Middle" /> </asp:GridView>
and code behind the page is
protected void btnview_Click(object sender, EventArgs e)
{
string query = txtsearch.Text;
foreach(GridViewRow row in gvaccmaster.Rows)
{
row.Visible = false;
}
if(query.Trim() != "")
{
foreach (GridViewRow row in gvaccmaster.Rows)
{
foreach (TableCell cell in row.Cells)
{
string cellText = cell.Text;
if (cell.Text == "" && cell.Controls.Count > 0)
{
cellText = ((LiteralControl)cell.Controls[0]).Text;
}
if (cellText.IndexOf(query) > -1)
{
row.Visible = true; break;
}
}
}
}
else
{
foreach (GridViewRow row in gvaccmaster.Rows)
{
row.Visible = true;
}
}
}
// im getting error on line
//if (cell.Text == "" && cell.Controls.Count > 0)
//{
// cellText = ((DataBoundLiteralControl)cell.Controls[0]).Text;
//}
I'm creating a page to enter employee's leave details in a dynamic gridview. Entry of data will be done by the from the footer of the gridview. When I try to get the values from the footer, all the text values as well as other values are coming as null or "".
Here is the code of RowCommand:
protected void gvEmployeeDetails_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName.Equals("ADD"))
{
TextBox txtAddEmpID = (TextBox)gvEmployeeDetails.FooterRow.FindControl("txtAddEmpID");
RadioButtonList rblFooter = (RadioButtonList)gvEmployeeDetails.FooterRow.FindControl("rblFooter");
TextBox txtAddDoj = (TextBox)gvEmployeeDetails.FooterRow.FindControl("txtAddDoj");
TextBox txtAddLeaves = (TextBox)gvEmployeeDetails.FooterRow.FindControl("txtAddLeaves");
TextBox txtAddDesignation = (TextBox)gvEmployeeDetails.FooterRow.FindControl("txtAddDesignation");
conn.Open();
string cmdstr = "insert into tblLeaves(EmpId,IsActive,DOJ,Leaves,Designation) values(#EmpId,#IsActive,#DOJ,#Leaves,#Designation)";
SqlCommand cmd = new SqlCommand(cmdstr, conn);
cmd.Parameters.AddWithValue("#EmpId", txtAddEmpID.Text);
cmd.Parameters.AddWithValue("#IsActive", rblFooter.SelectedValue);
cmd.Parameters.AddWithValue("#DOJ", txtAddDoj.Text);
cmd.Parameters.AddWithValue("#Leaves", txtAddLeaves.Text);
cmd.Parameters.AddWithValue("#Designation", txtAddDesignation.Text);
cmd.ExecuteNonQuery();
conn.Close();
BindData();
}
}
And below is the gridview:
<asp:GridView ID="gvEmployeeDetails" runat="server" Width="100%"
AutoGenerateColumns="False" ShowFooter="True"
onrowcommand="gvEmployeeDetails_RowCommand"
onrowdeleting="gvEmployeeDetails_RowDeleting"
onrowupdating="gvEmployeeDetails_RowUpdating"
onrowcancelingedit="gvEmployeeDetails_RowCancelingEdit"
onrowediting="gvEmployeeDetails_RowEditing" style="text-align: center" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="3">
<Columns>
<asp:TemplateField HeaderText="Employee ID">
<FooterTemplate>
<asp:TextBox ID="txtAddEmpID" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvFooterEmpID" runat="server" ControlToValidate="txtAddEmpID" ErrorMessage="*" ForeColor="Red"></asp:RequiredFieldValidator>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="lblEmpID" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "EmpId") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:Label ID="lblEditEmpID" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "EmpId") %>'></asp:Label>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Is Active">
<EditItemTemplate>
<asp:RadioButtonList ID="rblEdit" runat="server">
<asp:ListItem>Yes</asp:ListItem>
<asp:ListItem>No</asp:ListItem>
</asp:RadioButtonList>
</EditItemTemplate>
<FooterTemplate>
<asp:RadioButtonList ID="rblFooter" runat="server">
<asp:ListItem>Yes</asp:ListItem>
<asp:ListItem>No</asp:ListItem>
</asp:RadioButtonList>
<asp:RequiredFieldValidator ID="rfvFooterIsActive" runat="server" ControlToValidate="rblFooter" ErrorMessage="*" ForeColor="Red"></asp:RequiredFieldValidator>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="lblIsActive" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "IsActive") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Date Of Joining">
<ItemTemplate>
<asp:Label ID="lblDoj" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "DOJ") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtDoj" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "DOJ") %>'></asp:TextBox>
<ajaxToolkit:CalendarExtender ID="txtDoj_CalendarExtender" runat="server" BehaviorID="txtDoj_CalendarExtender" TargetControlID="txtDoj" />
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtAddDoj" runat="server" MaxLength="10" OnTextChanged="txtAddDoj_TextChanged" ></asp:TextBox>
<ajaxToolkit:CalendarExtender ID="txtAddDoj_CalendarExtender" runat="server" BehaviorID="txtAddDoj_CalendarExtender" TargetControlID="txtAddDoj" />
<asp:RequiredFieldValidator ID="rfvFooterDOJ" runat="server" ControlToValidate="txtAddDoj" ErrorMessage="*" ForeColor="Red"></asp:RequiredFieldValidator>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="No. Of Leaves">
<ItemTemplate>
<asp:Label ID="lblLeaves" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "Leaves") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtLeaves" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "Leaves") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtAddLeaves" runat="server" ></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvFooterLeaves" runat="server" ControlToValidate="txtAddLeaves" ErrorMessage="*" ForeColor="Red"></asp:RequiredFieldValidator>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Designation">
<ItemTemplate>
<asp:Label ID="lblDesignation" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "Designation") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtDesignation" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "Designation") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtAddDesignation" runat="server" ></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvFooterDesig" runat="server" ControlToValidate="txtAddDesignation" ErrorMessage="*" ForeColor="Red"></asp:RequiredFieldValidator>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Action">
<ItemTemplate>
<asp:ImageButton ID="imgbtnEdit" runat="server" CommandName="Edit" ImageUrl="~/Images/Edit.png" Height="32px" Width="32px"/>
<asp:ImageButton ID="imgbtnDelete" runat="server" CommandName="Delete" ImageUrl="~/Images/Delete.png" Height="32px" Width="32px"/>
</ItemTemplate>
<EditItemTemplate>
<asp:ImageButton ID="imgbtnUpdate" runat="server" CommandName="Update" ImageUrl="~/Images/icon-update.png"/>
<asp:ImageButton ID="imgbtnCancel" runat="server" CommandName="Cancel" ImageUrl="~/Images/icon-Cancel.png"/>
</EditItemTemplate>
<FooterTemplate>
<asp:LinkButton ID="lbtnAdd" runat="server" CommandName="ADD" Text="Add" Width="100px"></asp:LinkButton>
</FooterTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="White" ForeColor="#000066" />
<HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
<RowStyle ForeColor="#000066" />
<SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#007DBB" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#00547E" />
</asp:GridView>
Please correct me where I'm going wrong.
You are not using sender in your find control code line. Try to replace below line.
TextBox txtAddEmpID = (TextBox)gvEmployeeDetails.FooterRow.FindControl("txtAddEmpID");
this.
protected void gridview_RowCommand(object sender, System.Web.UI.WebControls.GridViewCommandEventArgs e)
{
if (e.CommandName == "Add") {
TextBox txtAddEmpID = (TextBox)((GridView)sender).FooterRow.FindControl("txtAddEmpID");
}
}
<asp:Panel ID="pnlGrdShift" runat="server" ScrollBars="Auto" Width="900px" Height="520px" CssClass="srcColor">
<cc1:GridView ID="gvShift" runat="server" AutoGenerateColumns="False" AllowSorting="True"
CssClass="grid"
OnDataBound="gvShift_DataBound"
DataSourceID="odsShiftDetails"
AllowPaging="True"
ShowFooter="false"
onrowcancelingedit="gvShift_RowCancelingEdit"
onrowcommand="gvShift_RowCommand"
onrowdeleting="gvShift_RowDeleting"
onrowediting="gvShift_RowEditing"
onrowupdating="gvShift_RowUpdating"
OnSelectedIndexChanged="gvShift_SelectedIndexChanged"
OnRowDataBound="gvShift_RowDataBound">
<AlternatingRowStyle CssClass="altrowstyle" />
<HeaderStyle CssClass="headerstyle" />
<RowStyle CssClass="rowstyle" Wrap="false" />
<EmptyDataRowStyle BackColor="#edf5ff" Height="300px" VerticalAlign="Middle" HorizontalAlign="Center" />
<EmptyDataTemplate >
No Records Found
</EmptyDataTemplate>
<Columns>
<asp:TemplateField HeaderText="E Code" SortExpression="userecode" HeaderStyle-Font-Bold="true" HeaderStyle-Font-Names="Calibre" HeaderStyle-ForeColor="White">
<ItemTemplate>
<asp:Label ID="lblUserECode" runat="server" Text='<%#Eval("userecode") %>' CssClass="GridContent" />
</ItemTemplate>
<EditItemTemplate>
<asp:Label ID="lblEditUserECode" runat="server" Text='<%#Eval("userecode") %>' style="width:50px;" CssClass="GridContent" />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="User Name" SortExpression="username" HeaderStyle-Font-Bold="true" HeaderStyle-Font-Names="Calibre" HeaderStyle-ForeColor="White">
<ItemTemplate>
<asp:Label ID="lblUserName" runat="server" Text='<%#Eval("username") %>' CssClass="GridContent" />
</ItemTemplate>
<EditItemTemplate>
<asp:Label ID="lblEditUserName" runat="server" Text='<%#Eval("username") %>' style="width:100px;" CssClass="GridContent"/>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Shift Start Time" SortExpression="ShiftStartTime" HeaderStyle-Font-Bold="true" HeaderStyle-Font-Names="Calibre" HeaderStyle-ForeColor="White">
<ItemTemplate>
<asp:Label ID="lblShiftStartTime" runat="server" Text='<%#Eval("ShiftStartTime") %>' CssClass="GridContent"/>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="ddlShiftStartTime" runat="server" Text='<%#Eval("ShiftStartTime") %>' style="width:65px;" CssClass="GridContent" />
<asp:RegularExpressionValidator ID="RegularExpValidatorddlShiftStartTime" runat="server" ControlToValidate="ddlShiftStartTime" ValidationExpression="^([0-9]|0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]$" ErrorMessage="*" Font-Bold="true" ForeColor="Red" ToolTip="Must be in HH:MM" />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Shift End Time" SortExpression="ShiftEndTime" HeaderStyle-Font-Bold="true" HeaderStyle-Font-Names="Calibre" HeaderStyle-ForeColor="White">
<ItemTemplate>
<asp:Label ID="lblShiftEndTime" runat="server" Text='<%#Eval("ShiftEndTime") %>' CssClass="GridContent" />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="ddlShiftEndTime" runat="server" Text='<%#Eval("ShiftEndTime") %>' style="width:65px;" CssClass="GridContent" />
<asp:RegularExpressionValidator ID="RegularExpValidatorddlShiftEndTime" runat="server" ControlToValidate="ddlShiftEndTime" ValidationExpression="^([0-9]|0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]$" ErrorMessage="*" Font-Bold="true" ForeColor="Red" ToolTip="Must be in HH:MM" />
</EditItemTemplate>
</asp:TemplateField>
<%--<asp:BoundField DataField="ShiftEndTIme" HeaderText="Shift End Time" SortExpression="ShiftEndTIme"/>--%>
<asp:TemplateField HeaderText="Saturday Shift Start Time" SortExpression="WeekendShiftStartTime" HeaderStyle-Font-Bold="true" HeaderStyle-Font-Names="Calibre" HeaderStyle-ForeColor="White">
<ItemTemplate>
<asp:Label ID="lblWeekendShiftStartTime" runat="server" Text='<%#Eval("WeekendShiftStartTime") %>' CssClass="GridContent" />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="ddlWeekendShiftStartTime" runat="server" Text='<%#Eval("WeekendShiftStartTime") %>' style="width:65px;" CssClass="GridContent" />
<asp:RegularExpressionValidator ID="RegularExpValidatorddlWeekendShiftStartTime" runat="server" ControlToValidate="ddlWeekendShiftStartTime" ValidationExpression="^([0-9]|0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]$" ErrorMessage="*" Font-Bold="true" ForeColor="Red" ToolTip="Must be in HH:MM" />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Saturday Shift End Time" SortExpression="weekendshiftendtime" HeaderStyle-Font-Bold="true" HeaderStyle-Font-Names="Calibre" HeaderStyle-ForeColor="White">
<ItemTemplate>
<asp:Label ID="lblWeekendShiftEndTime" runat="server" Text='<%#Eval("weekendshiftendtime") %>' CssClass="GridContent"/>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="ddlWeekendShiftEndTime" runat="server" Text='<%#Eval("weekendshiftendtime") %>' style="width:65px;" CssClass="GridContent" />
<asp:RegularExpressionValidator ID="RegularExpValidatorddlWeekendShiftEndTime" runat="server" ControlToValidate="ddlWeekendShiftEndTime" ValidationExpression="^([0-9]|0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]$" ErrorMessage="*" Font-Bold="true" ForeColor="Red" ToolTip="Must be in HH:MM" />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField ShowHeader="False" >
<EditItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" CommandName="Update" Text="Update" ForeColor="White"></asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel" ForeColor="White"></asp:LinkButton>
</EditItemTemplate>
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Edit" Text="Edit" ForeColor="White"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<PagerTemplate >
<table width="100%" >
<tr>
<td style="text-align: left">
Page Size:
<asp:DropDownList ID="ddPageSize" runat="server" EnableViewState="true" OnSelectedIndexChanged="ddPageSize_SelectedIndexChanged" AutoPostBack="true" style="width:50px;">
<asp:ListItem Text="10" ></asp:ListItem>
<asp:ListItem Text="20" ></asp:ListItem>
<asp:ListItem Text="30" ></asp:ListItem>
<asp:ListItem Text="40" ></asp:ListItem>
<asp:ListItem Text="50" ></asp:ListItem>
</asp:DropDownList>
</td>
<td style="text-align: right">
<asp:Label ID="lblPageCount" runat="server"></asp:Label>
</td>
</tr>
</table>
</PagerTemplate>
</cc1:GridView>
</asp:Panel></td></tr>
</table>
<div style="margin-top:5px" class="PagerGrid">
<asp:DataPager ID="pager" runat="server" PagedControlID="gvShift">
<Fields>
<asp:NextPreviousPagerField FirstPageText="<<" LastPageText=">>"
NextPageText=">" PreviousPageText="<" ShowFirstPageButton="True"
ShowNextPageButton="False" ButtonCssClass="datapager" />
<asp:NumericPagerField ButtonCount="10" NumericButtonCssClass="datapager" CurrentPageLabelCssClass="datapager" />
<asp:NextPreviousPagerField LastPageText=">>" NextPageText=">"
ShowLastPageButton="True" ShowPreviousPageButton="False" ButtonCssClass="datapager" />
</Fields>
</asp:DataPager>
</div>
<br />
<asp:ObjectDataSource ID="odsShiftDetails" runat="server"
SelectMethod="GetShiftInfoSortedPage" TypeName="EQ.DAL.ShiftInfoDB"
EnablePaging="True" SelectCountMethod="GetShiftInfoCount"
SortParameterName="sortExpression">
<SelectParameters>
<asp:ControlParameter ControlID="hfSearchCriteria" Name="searchCriteria" Direction="Input" />
</SelectParameters>
</asp:ObjectDataSource>
Code behind for Row Updation is:-
protected void gvShift_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
try
{
TextBox txtShiftStartTime = (TextBox)gvShift.Rows[e.RowIndex].FindControl("ddlShiftStartTime");
TextBox txtShiftEndTime = (TextBox)gvShift.Rows[e.RowIndex].FindControl("ddlShiftEndTime");
TextBox txtWeekendShiftStartTime = (TextBox)gvShift.Rows[e.RowIndex].FindControl("ddlWeekendShiftStartTime");
TextBox txtWeekendShiftEndTime = (TextBox)gvShift.Rows[e.RowIndex].FindControl("ddlWeekendShiftEndTime");
Label lblUserecode = (Label)gvShift.Rows[e.RowIndex].FindControl("lblEditUserECode");
string userecode = lblUserecode.Text.ToString();
string _ShiftStart = txtShiftStartTime.Text.ToString();
string _ShiftEnd = txtShiftEndTime.Text.ToString();
string _WeekendShiftStart = txtWeekendShiftStartTime.Text.ToString();
string _WeekendShiftend = txtWeekendShiftEndTime.Text.ToString();
EmployeeQuotientCL.Entities.ConfigurationVariables _configVariables = new ConfigurationVariables();
string databaseConnectionString = _configVariables.ConnectionString;
SqlConnection sqlConnection = null;
if (((databaseConnectionString + string.Empty) != string.Empty))
{
DBConnect dbConnect = new DBConnect(_configVariables.ConnectionString);
sqlConnection = dbConnect.SQLConnection;
SqlCommand cmd = new SqlCommand();
cmd.Connection = sqlConnection;
cmd.CommandText = "UPDATE UserInfo SET ShiftStartTime ='" + _ShiftStart + "',ShiftEndTime='" + _ShiftEnd.ToString() + "',Weekendshiftstarttime='" + _WeekendShiftStart.ToString() + "',Weekendshiftendtime='" + _WeekendShiftend.ToString() + "' WHERE UserECode=" + userecode.ToString();
sqlConnection.Open();
cmd.ExecuteNonQuery();
gvShift.DataSource = null;
gvShift.DataBind();
sqlConnection.Close();
}
}
catch (Exception ex)
{
}
}
While Updating the Rows in grid , I am getting the below error message.
Microsoft JScript runtime error: Sys.WebForms.PageRequestManagerServerErrorException: >Updating is not supported by ObjectDataSource 'odsShiftDetails' unless the UpdateMethod is >specified.
Guide me how to fix this error. Thanks in advance.
You need to add an update method to your gridview. Look at this tutorial: Gridview ObjectdataSource