How to use Ajax Calendar in Grid View to disable past date - c#

I use a CalendarExtender of AjaxControlToolkit in a GridView and a TextBox. And from that TextBox I want to disable future dates.
I am trying below code
<asp:GridView ID="Gridview2" runat="server" AutoGenerateColumns="false" DataKeyNames="Sno" ShowFooter="true" OnRowCommand="Gridview2_RowCommand" CssClass="table table-bordered table-striped"
OnRowCancelingEdit="Gridview2_RowCancelingEdit" OnRowDeleting="Gridview2_RowDeleting" OnRowEditing="Gridview2_RowEditing" OnRowUpdating="Gridview2_RowUpdating" OnRowDataBound="Gridview2_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="Sno" Visible="false">
<ItemTemplate>
<asp:Label ID="lbl_sno1" runat="server" Text='<%#Eval("Sno") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Date of visit" ShowHeader="false">
<EditItemTemplate>
<asp:TextBox ID="Txt_update_DateofVisit1" runat="server" Text='<%# Eval("DateofVisit","{0:MM/dd/yyyy}") %>' CssClass="form-control"/>
<cc1:CalendarExtender ID="CalendarExtenderedit11" CssClass="Calendar" TargetControlID="Txt_update_DateofVisit1" Format="MM/dd/yyyy" runat="server"></cc1:CalendarExtender>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lbl_DateofVisit1" runat="server" Text='<%# Eval("DateofVisit","{0:MM/dd/yyyy}") %>' />
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txt_DateofVisit_add" runat="server" CssClass="form-control" />
<cc1:CalendarExtender ID="CalendarExtendere21" CssClass="Calendar" TargetControlID="txt_DateofVisit_add" Format="MM/dd/yyyy" runat="server"></cc1:CalendarExtender>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton ID="imgbtnDelete_add" runat="server" CommandName="Delete" ImageUrl="~/img/grid_delete.png" Text="Edit" ToolTip="click here to delete" />
</ItemTemplate>
<FooterTemplate>
<asp:ImageButton ID="imgbtnAdd_add" runat="server" CommandName="AddNew" ImageUrl="~/img/grid_add.png" ToolTip="click here to add" CausesValidation="true" ValidationGroup="outadd" />
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Below is my RowDataBound
protected void Gridview2_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
AjaxControlToolkit.CalendarExtender CalendarExtendere21 = (AjaxControlToolkit.CalendarExtender)Gridview2.FooterRow.FindControl("CalendarExtendere21");
CalendarExtendere21.EndDate = DateTime.Now;
// CalendarExtendere21.StartDate = DateTime.Now;
}
}
I get below error:
Object reference not set to an instance of an object.

just use parameter e to find out control in grid
if (e.Row.RowType == DataControlRowType.Footer)
{
AjaxControlToolkit.CalendarExtender CalendarExtendere21 =
(e.Row.FindControl("CalendarExtendere21") as AjaxControlToolkit.CalendarExtender);
}
and don't forget to check for null

Related

Get text box value from grid view Header

I have grid with text box for filter data on header.I have button outside the grid.I am using this gridview for filtering the grid by some columns. When I click on button I want to get value from text box and dropdownlist on codebehind.cs, below is my webform.aspx code of gridview,
<asp:Button ID="btngetLocationDate" runat="server" Text="Get Filtered Data" OnClick="getTextBoxValue"></asp:Button>
<asp:GridView ID="TrackerGrid" runat="server" BackColor="#CCCCCC" AllowPaging="True" AutoGenerateColumns="false">
<AlternatingRowStyle HorizontalAlign="Center" VerticalAlign="Middle" />
<Columns>
<asp:TemplateField HeaderText="User ID" SortExpression="User ID">
<ItemTemplate>
<asp:Label ID="lbluserid" runat="server" Text='<%# Eval("User ID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Customer Name">
<HeaderTemplate>
Full Name:
<asp:DropDownList ID="ddlfullName" runat="server" AutoPostBack="true" OnSelectedIndexChanged="onselectDropdown">
<asp:ListItem Text="ALL" Value="ALL" Selected="True"></asp:ListItem>
</asp:DropDownList>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lblname" runat="server" Text='<%# Eval("Full Name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Mobile Number">
<HeaderTemplate>
Mobile Number:
<asp:TextBox ID="txtMobilenumber" runat="server" AutoPostBack="true"></asp:TextBox>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lblmobile" runat="server" Text='<%# Eval("Mobile Phone") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Email Address">
<HeaderTemplate>
Email Id:
<asp:TextBox ID="txtemail" runat="server" AutoPostBack="true"></asp:TextBox>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lblemail" runat="server" Text='<%# Eval("Email ID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Longitude">
<HeaderTemplate>
<asp:Label ID="lbllng" runat="server" Text="Longitude"></asp:Label>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lbllongi" runat="server" Text='<%# Eval("Longitude") %>' Width="70"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Lattitude">
<HeaderTemplate>
<asp:Label ID="lbllat" runat="server" Text="Lattitude"></asp:Label>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lbllatti" runat="server" Text='<%# Eval("Lattitude") %>' Width="70"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Activated On">
<HeaderTemplate>
<asp:Label ID="lbldevices" runat="server" Text="Activated On"></asp:Label></br>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lbldevice" runat="server" Text='<%# Eval("Activated On") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Location DateTime" SortExpression="Location DateTime">
<HeaderTemplate>
<asp:Label ID="Label1" runat="server" Text="Location DateTime"></asp:Label></br>
<asp:Label ID="lblStartDate" runat="server" AssociatedControlID="txtStartDate" Text="Start Date"></asp:Label>
<asp:TextBox ID="txtStartDate" runat="server"></asp:TextBox>
<cc1:calendarextender id="CalendarStartDate" runat="server" format="dd/MM/yyyy" targetcontrolid="txtStartDate" popupbuttonid="imgPopup1"></cc1:calendarextender>
<asp:ImageButton ID="imgPopup1" runat="server" ImageUrl="https://cdn4.iconfinder.com/data/icons/small-n-flat/24/calendar-alt-512.png" Height="20px" Width="30px" />
</br>
<asp:Label ID="lblEndDate" runat="server" Text="End Date" AssociatedControlID="txtEndDate"></asp:Label>
<asp:TextBox ID="txtEndDate" runat="server"></asp:TextBox>
<cc1:calendarextender id="CalendarEndDate" runat="server" format="dd/MM/yyyy" targetcontrolid="txtEndDate" popupbuttonid="imgPopup2"></cc1:calendarextender>
<asp:ImageButton ID="imgPopup2" runat="server" ImageUrl="https://cdn4.iconfinder.com/data/icons/small-n-flat/24/calendar-alt-512.png" Height="20px" Width="30px" />
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lblDatetime" runat="server" Text='<%# Eval("Location DateTime") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
and I am getting texbox value in codebehind code.
protected void getTextBoxValue(object sender, EventArgs e)
{
DropDownList ddlfullname = (TrackerGrid.HeaderRow.FindControl("ddlfullName") as DropDownList);
string selectedValue = ddlfullname.SelectedItem.Text;
TextBox mobilenumber = (TrackerGrid.HeaderRow.FindControl("txtMobilenumber") as TextBox);
string mobile = mobilenumber.Text;
TextBox email = TrackerGrid.HeaderRow.FindControl("txtemail") as TextBox;
string emailid = email.Text;
TextBox staloc = TrackerGrid.HeaderRow.FindControl("txtStartDate") as TextBox;
string startlocatdate = staloc.Text;
TextBox endloc = TrackerGrid.HeaderRow.FindControl("txtEndDate") as TextBox;
string endlocadate = endloc.Text;
}
I am getting empty value when I debugging this code.I think my code is correct but how i am not getting value of textboxes, i don't understand. Please help me for solving my issue.
Thanks in advance.
You probably did not wrap the DataBind() of the Gridview inside an IspostBack check. If you do not then the values will be rested by the DataBind instead of being saved with ViewState.
protected void Page_Load(object sender, EventArgs e)
{
//not here
TrackerGrid.DataSource = source;
TrackerGrid.DataBind();
if (IsPostBack == false)
{
//but inside an ispostback check
TrackerGrid.DataSource = source;
TrackerGrid.DataBind();
}
}

How to reference textbox from gridview in my codebehind C#

All the examples I've found have not been helpful. I have a textbox in the footer of my gridview called "txtMyCoverage". In my codebehind I just want to reference that textbox to say
a = txtMyCoverage.Text;
I tried
gvLimit.FindControl("txtMyCoverage").ToString();
but it comes back null reference. Thanks for any help!
Here is my grid
<asp:GridView Width="100%" ID="gvLimits"
CssClass="gridView no_min_width"
runat="server"
AutoGenerateColumns="False"
CellPadding="2" ShowFooter="true"
DataKeyNames="PolicyLimitID, LimitID"
HorizontalAlign="Center"
OnRowDataBound="gvLimits_RowDataBound">
<RowStyle HorizontalAlign="Center" />
<Columns>
<asp:TemplateField HeaderText="Coverage">
<ItemTemplate>
<asp:Label ID="txtLimitLetter" runat="server" Width="20px" CssClass="center" Text='<%# Bind("Limit.LimitLetter") %>' Enabled="false" ></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtMyCoverage" runat="server" Width="50px" CssClass="center" ></asp:TextBox>
</FooterTemplate>
</Columns>
</asp:GridView>
if You Used TextBox Like this:
<asp:TemplateField HeaderText="Coverage">
<ItemTemplate>
<asp:Label ID="txtLimitLetter" runat="server" Width="20px" CssClass="center" Text='<%# Bind("Limit.LimitLetter") %>' Enabled="false" ></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtMyCoverage" runat="server" Width="50px" CssClass="center" ></asp:TextBox>
</FooterTemplate>
then it You can find On rowbound like this:
On RowBoundEvent of Grid
protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Footer)
{
TextBox txtLimitLetter=(TextBox)e.Row.FindControl("txtLimitLetter");
}
}
or
you can Look Through GridView Rows
foreach(GridViewRow row in GridView2.Rows)
{
if (e.Row.RowType == DataControlRowType.Footer)
{
TextBox txtLimitLetter=(TextBox)e.Row.FindControl("txtLimitLetter");
}
}

Bind Dropdown list in Gridview footer which is in Tab Control

I am using ASP.NET and Entity Framework and Tab control of AJAX
I have a dropdown list control in gridview which is in TAB control Am not able to bind dropdown list control.
Tab Control --> Gridview --> Dropdown list
My ASP.NET Code is
<ajax:TabContainer ID="TabContainer2" runat="server" CssClass="fancy fancy-green">
<ajax:TabPanel ID="TabPanel1" runat="server">
<HeaderTemplate>
College Toppers
</HeaderTemplate>
<ContentTemplate>
<asp:Panel ID="Panel1" runat="server">
<div>
<asp:GridView ID="GridView1" DataKeyNames="TL_ID" runat="server"
AutoGenerateColumns="False" CssClass="Gridview" ShowFooter="True" OnRowDataBound="GRVToppers_RowDataBound">
<Columns>
<asp:TemplateField>
<EditItemTemplate>
<asp:ImageButton ID="imgbtnUpdate" CommandName="Update" runat="server" ImageUrl="~/Admin/Images/update.jpg" ToolTip="Update" Height="20px" Width="20px" /><asp:ImageButton ID="imgbtnCancel" runat="server" CommandName="Cancel" ImageUrl="~/Admin/Images/Cancel.jpg" ToolTip="Cancel" Height="20px" Width="20px" />
</EditItemTemplate>
<FooterTemplate>
<asp:ImageButton ID="imgbtnAdd" runat="server" ImageUrl="~/Admin/Images/AddNewitem.jpg" CommandName="AddNew" Width="30px" Height="30px" ToolTip="Add new User" ValidationGroup="validaiton" />
</FooterTemplate>
<ItemTemplate>
<asp:ImageButton ID="imgbtnEdit" CommandName="Edit" runat="server" ImageUrl="~/Admin/Images/Edit.jpg" ToolTip="Edit" Height="20px" Width="20px" /><asp:ImageButton ID="imgbtnDelete" CommandName="Delete" runat="server" ImageUrl="~/Admin/Images/delete.jpg" ToolTip="Delete" Height="20px" Width="20px" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Topper List No">
<EditItemTemplate>
<asp:Label ID="lblEditTpid" runat="server" Text='<%#Eval("TL_ID") %>' />
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblShowTpid" runat="server" Text='<%#Eval("TL_ID") %>' />
</ItemTemplate>
<ControlStyle Width="10px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Department Name">
<EditItemTemplate>
<asp:DropDownList ID="ddlEditDeptname" runat="server" Width="90" DataTextField='<%#Bind("DEPT_NAME") %>' DataValueField='<%#Bind("DEPT_ID") %>' AppendDataBoundItems="true"></asp:DropDownList>
</EditItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="ddladdnewDeptname" runat="server" Width="90" DataTextField='<%#Bind("DEPT_NAME") %>' DataValueField='<%#Bind("DEPT_ID") %>' AppendDataBoundItems="True"></asp:DropDownList>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="lblShowDeptname" runat="server" Text='<%#Eval("DEPT_NAME") %>' Width="90" />
</ItemTemplate>
<ControlStyle Width="30px" />
</asp:TemplateField>
</Columns>
<HeaderStyle BackColor="#61A6F8" Font-Bold="True" ForeColor="White"></HeaderStyle>
</asp:GridView>
</div>
</asp:Panel>
</ContentTemplate>
</ajax:TabPanel>
</ajax:TabContainer>
==================
My C# code is
protected void GRVToppers_RowDataBound(object sender, GridViewRowEventArgs e)
{
try
{
var dept = from n in ecme.DEPT_MASTER
select new { n.DEPT_ID, n.DEPT_NAME };
DropDownList ddl = null;
if (e.Row.RowType == DataControlRowType.Footer)
{
ddl = e.Row.FindControl("ddladdnewDeptname") as DropDownList;
int cnt = 0;
cnt = dept.Count();
ListItem[] items = new ListItem[cnt];
for (int i = 0; i < cnt; i++)
{
items[i] = new ListItem("DEPT_NAME", "DEPT_ID");
}
}
}
catch (Exception)
{ }
}
========================
You can do this very easily...
Remove
DataTextField='<%#Bind("DEPT_NAME") %>' DataValueField='<%#Bind("DEPT_ID") %>' AppendDataBoundItems="True"
and
DataTextField='<%#Bind("DEPT_NAME") %>' DataValueField='<%#Bind("DEPT_ID") %>' AppendDataBoundItems="true"
from aspx code
and Make changes in
protected void GRVToppers_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Footer)
{
var dept = from n in ecme.DEPT_MASTER
select new { n.DEPT_ID, n.DEPT_NAME };
DropDownList ddl = (DropDownList)e.Row.FindControl("ddladdnewDeptname");
ddl.DataTextField = "DEPT_NAME";
ddl.DataValueField = "DEPT_ID";
ddl.DataSource = dept.ToList();
ddl.DataBind();
ddl.Items.Insert(0, "--Select Department--");
}
}
100% sure this 'll work...

Nested grid edit doesn't work

I'm stuck on this problem where I have grid within grid where the nested grid is editable. I can't get the edit working. What's complicates things is that the nested grid is inside a modal popup extender.
It works to the point where I click on the edit button. Then the EmptyDataText property kicks in with the message. If I click on it second time the grid opens in edit mode but the update/cancel buttons don't work. The cancel button when click displays the EmptyDataText property.
Another issue is that this nested grid doesn't use a object data source so I'm going to have to all the updating and deleting in the code file. Not sure now to do that either.
I would like some advice as to how to make this corrected. Here is the code:
<asp:GridView ID="gvForum" runat="server" DataSourceID="odsForumApproval" DataKeyNames="id" Width="200px"
RepeatColumns="1" DataKeyField="id" CssClass="gridview"
AutoGenerateColumns="False" GridLines="None" OnSelectedIndexChanged="_OnCommand">
<AlternatingRowStyle CssClass="altbgcolor" />
<Columns>
<asp:TemplateField >
<ItemTemplate>
<asp:Label runat="server" ID="lblTitle" Text='<%# Bind("Title") %>' />
<asp:Panel id="div" runat="server" align="center" class="confirm" style="display:none" >
<asp:GridView runat="server" ID="gvForumDetail" AutoGenerateColumns="False" DataKeyNames="id"
AllowPaging='true' CssClass="gridview"
AllowSorting="true" PageSize="5" CellPadding="5" OnRowEditing="gvForumDetail_OnRowEditing"
OnRowCancelingEdit="gvForumDetail_CancelRecord" >
<AlternatingRowStyle CssClass="altbgcolor" />
<RowStyle VerticalAlign="Top" HorizontalAlign="Left" />
<HeaderStyle CssClass="greenbar" ForeColor="White" /> <Columns>
<asp:BoundField DataField="id" ReadOnly="true" Visible="false" />
<asp:TemplateField HeaderText="Title">
<ItemTemplate>
<asp:Label runat="server" ID="lblTraining" Text='<%# Bind("title") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID="txtTraining" Text='<%# Bind("title") %>' />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Post Message">
<ItemTemplate>
<asp:Label runat="server" ID="lblCompletionDate" Width="250" Text='<%# Bind("description") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID="txtDescription" Text='<%# Bind("description") %>' TextMode="MultiLine" Rows="5" Width="250" />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Posted By">
<ItemTemplate>
<asp:Label runat="server" ID="lblRecurence" Text='<%# Bind("MemberName") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Posted Date">
<ItemTemplate>
<asp:Label runat="server" ID="lblNotes" Text='<%# Eval("itemdate", "{0:d}") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:CheckBox runat="server" ID="cbxApproved" Text='<%# Bind("approved") %>' />
</EditItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowCancelButton="true" ShowEditButton="true" ShowDeleteButton="true" />
</Columns>
</asp:GridView>
<br />
<Club:RolloverLink ID="btnClose" runat="server" Text="Close" />
</asp:Panel>
<ajaxToolKit:ModalPopupExtender ID="ModalPopupExtender1" runat="server"
TargetControlID="lblTitle"
PopupControlID="div"
CancelControlID="btnClose"
BackgroundCssClass="modalBackground" />
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowSelectButton="True" />
</Columns>
Code behind:
public void _OnCommand(object sender, EventArgs e)
{
ObjectDataSource ods = new ObjectDataSource();
ods.ID = "ods_ForumDetail";
ods.EnableViewState = true;
ods.TypeName = "ForumApproval";
ods.SelectMethod = "GetForumById";
string id = "";
int rowIndex = gvForum.SelectedIndex;
id = gvForum.DataKeys[rowIndex].Value.ToString();
ods.SelectParameters.Add("id", System.TypeCode.Int32, id);
var ModalPopupExtender1 = (ModalPopupExtender)(gvForum.SelectedRow.FindControl("ModalPopupExtender1"));
ModalPopupExtender1.Show();
var gvForumDetail = (GridView)(gvForum.SelectedRow.FindControl("gvForumDetail"));
gvForumDetail.DataSource = ods;
gvForumDetail.DataBind();
}
protected void gvForumDetail_OnRowEditing(Object sender, GridViewEditEventArgs e)
{
var ModalPopupExtender1 = (ModalPopupExtender)(gvForum.SelectedRow.FindControl("ModalPopupExtender1"));
ModalPopupExtender1.Show();
var gvForumDetail = (GridView)(gvForum.SelectedRow.FindControl("gvForumDetail"));
gvForumDetail.EditIndex = e.NewEditIndex;
gvForumDetail.DataBind();
}
protected void gvForumDetail_CancelRecord(object sender, GridViewCancelEditEventArgs e)
{
var ModalPopupExtender1 = (ModalPopupExtender)(gvForum.SelectedRow.FindControl("ModalPopupExtender1"));
ModalPopupExtender1.Show();
var gvForumDetail = (GridView)(gvForum.SelectedRow.FindControl("gvForumDetail"));
gvForumDetail.EditIndex = -1;
gvForumDetail.DataBind();
}
I will try to answer part of your question, updating and deleting the code. you need to take advantage of the "onrowcommand" for the inner gridview (OnRowCommand).

I am getting null when I am using (DropDownList)e.Row.FindControl

Here is my code where it's giving the value of ddl = null:
//event handler to bind the data to the row before rendering the gridview
protected void gridview1_RowDataBound(object sender ,GridViewRowEventArgs e)
{
if ((e.Row.RowType == DataControlRowType.DataRow)&&(e.Row.DataItem!=null))
{
//DropDownList ddlNo = (DropDownList)e.Row.FindControl("ddlNo");
DropDownList ddlNo = (DropDownList)e.Row.FindControl("ddlNo");
if (ddlNo != null)
{
ddlNo.DataSource = Test.GetData();
ddlNo.DataBind();
ddlNo.SelectedValue = gridview1.DataKeys[e.Row.RowIndex].Values[0].ToString();//datakeys are used to retain the value of the no. in edit mode
}
}
}
Here is my aspx code:
<asp:GridView ID="gridview1" runat="server" AutoGenerateColumns="false" AutoGenerateDeleteButton="false" AutoGenerateEditButton="false" ShowFooter="true" EmptyDataText="No data available." PagerStyle-HorizontalAlign="Right" AllowPaging="true" AllowSorting="true" GridLines="Vertical" Width="98%" EnableViewState="true" OnRowCancelingEdit="gridview1_RowCancelingEdit" OnRowDataBound="gridview1_RowDataBound" OnRowEditing="gridview1_RowEditing" OnRowUpdating="gridview1_RowUpdating" OnRowCommand="gridview1_RowCommand" OnRowDeleting="gridview1_RowDeleting" DataKeyNames="testID">
<Columns>
<asp:TemplateField HeaderText="Select" HeaderStyle-HorizontalAlign="Left">
<ItemTemplate>
<asp:Label ID="lblSelect" runat="server" Text="Select"></asp:Label>
<%-- <asp:CheckBox ID="chkSelect" runat="server" AutoPostBack="true" OnCheckedChanged="chkSelect_CheckChanged" />--%>
</ItemTemplate>
<HeaderStyle HorizontalAlign="Left" />
</asp:TemplateField>
<asp:BoundField DataField="testID" HeaderText="SearchId" />
<asp:TemplateField HeaderText="Name" HeaderStyle-HorizontalAlign="Left">
<EditItemTemplate>
<asp:TextBox ID="txtName" runat="server" Text='<%#Eval("testDesc") %>' />
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtNewName" runat="server"></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="lblName" runat="server" Text='<%# Bind("testDesc") %>'></asp:Label>
</ItemTemplate>
<HeaderStyle HorizontalAlign="Left" />
</asp:TemplateField>
<asp:TemplateField HeaderText="No." HeaderStyle-HorizontalAlign="Left">
<EditItemTemplate>
<asp:DropDownList ID="ddlNo" runat="server" DataTextField="testNo" DataValueField="testNo" />
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblNo" runat="server" Text='<%# Bind("testNo") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="ddlNewNo" runat="server" DataTextField="testNo" DataValueField="testNo">
</asp:DropDownList>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Edit" ShowHeader="False">
<EditItemTemplate>
<asp:LinkButton ID="linkbt1" runat="server" CausesValidation="true" CommandName="Update" Text="Update">
</asp:LinkButton>
<asp:LinkButton ID="linkbt2" runat="server" CausesValidation="false" CommandName="Cancel" Text="Cancel"></asp:LinkButton>
</EditItemTemplate>
<ItemTemplate>
<asp:LinkButton ID="linkbt1" runat="server" CausesValidation="false" CommandName="Edit" Text="Edit">
</asp:LinkButton>
</ItemTemplate>
<FooterTemplate>
<asp:LinkButton ID="linkbt2" runat="server" CausesValidation="false" CommandName="AddNew" Text="AddNew">
</asp:LinkButton>
</FooterTemplate>
</asp:TemplateField>
<asp:CommandField HeaderText="Delete" ShowDeleteButton="true" ShowHeader="true" />
</Columns>
</asp:GridView>
Can anyone provide a relevant solution to this?
you put the ddl no in the footer template but you're only checking for that in the (e.Row.RowType == DataControlRowType.DataRow) Change that to FooterRow and it should find it
change the above line to
if(e.Row.RowType == DataControlRowType.Footer && e.Row.DataItem != null)
<%--DataKeyNames="Environment_ID"--%>
' />
<%--
'>
' />
--%>
' />
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblapp" runat="server" Text='<%# Eval("AppName") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="ddlAppFooter" class="SearchBoxRTD" runat="server" DataTextField="AppName"
DataValueField="SNO">
</asp:DropDownList>
</FooterTemplate>
<HeaderStyle HorizontalAlign="Left" />
</asp:TemplateField>
<asp:TemplateField HeaderText="TestType" HeaderStyle-Width="50">
<EditItemTemplate>
<asp:DropDownList ID="ddlTestTypeDataRow" class="SearchBoxRTD" runat="server" DataTextField="Testingtypedescription"
DataValueField="TestingTypeID">
</asp:DropDownList>
<asp:HiddenField ID="hdnTesttype" runat="server" Value='<%# Bind("Testtype") %>' />
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblTestType" runat="server" Text='<%# Eval("Testingtypedescription") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="ddlTestTypeFooter" class="SearchBoxRTD" runat="server" DataTextField="Testingtypedescription"
DataValueField="TestingTypeID">
</asp:DropDownList>
</FooterTemplate>
<HeaderStyle HorizontalAlign="Left" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Scenario Type" HeaderStyle-Width="50">
<EditItemTemplate>
<asp:DropDownList ID="ddlScenarioTypeDataRow" class="SearchBoxRTD" runat="server" DataTextField="ScenarioTypedescription"
DataValueField="ScenarioTypeID">
</asp:DropDownList>
<asp:HiddenField ID="hdnScenariotype" runat="server" Value='<%# Bind("ScenarioTypeID") %>' />
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblScenarioType" runat="server" Text='<%# Eval("ScenarioTypedescription") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="ddlScenarioTypeFooter" class="SearchBoxRTD" runat="server"
DataTextField="ScenarioTypedescription" DataValueField="ScenarioTypeID">
</asp:DropDownList>
</FooterTemplate>
<HeaderStyle HorizontalAlign="Left" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Scenario">
<EditItemTemplate>
<asp:TextBox ID="txtScenario" runat="server" Text='<%# Bind("Scenario") %>'></asp:TextBox>
<asp:RequiredFieldValidator ID="RFDGEdUI" runat="server" Font-Bold="true" ErrorMessage="*"
ControlToValidate="txtScenario" ValidationGroup="EditScenario"></asp:RequiredFieldValidator>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="LblScenario" runat="server" Text='<%# Bind("Scenario") %>' />
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtfootScenario" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RFDGScenario" runat="server" Font-Bold="true" ErrorMessage="*"
ControlToValidate="txtfootScenario" ValidationGroup="AddScenario"></asp:RequiredFieldValidator>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Status">
<EditItemTemplate>
<asp:CheckBox ID="chkScenariostatus" runat="server" Checked='<%# Bind("Status") %>' />
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="LblScenarioStatus" runat="server" Text='<%# Bind("Status") %>' />
</ItemTemplate>
<FooterTemplate>
<asp:CheckBox ID="chkScenariofooter" runat="server" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Edit" ShowHeader="False">
<EditItemTemplate>
<asp:LinkButton ID="lbkUpdateScenario" runat="server" ValidationGroup="EditScenario"
CausesValidation="True" CommandName="Update" Text="Update"></asp:LinkButton>
<asp:LinkButton ID="lnkCancelScenario" runat="server" CausesValidation="False" CommandName="Cancel"
Text="Cancel"></asp:LinkButton>
</EditItemTemplate>
<FooterTemplate>
<asp:LinkButton ID="lnkAddScenario" runat="server" ValidationGroup="AddScenario"
CausesValidation="True" CommandName="Insert" Text="Insert"></asp:LinkButton>
</FooterTemplate>
<ItemTemplate>
<asp:LinkButton ID="lnkEditScenario" runat="server" CausesValidation="False" CommandName="Edit"
Text="Edit"></asp:LinkButton>
</ItemTemplate>
<HeaderStyle HorizontalAlign="Left" />
</asp:TemplateField>
</Columns>
<EmptyDataTemplate>
No Applications available!
</EmptyDataTemplate>
</asp:GridView>
--------------------------------CS--------------------------------------------------------
protected void grdScenario_RowDataBound(object sender, GridViewRowEventArgs e)
{
try
{
DataSet ds = GetData("qtp_getservice");
if (e.Row.RowType == DataControlRowType.DataRow)
{
//services,environment,platform
// bool flag = false;
if (e.Row.RowType == DataControlRowType.DataRow && (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate))
{
Label lbl = (Label)e.Row.FindControl("LblScenarioStatus");
if (null != lbl && lbl.Text != "")
{
lbl.Text = (string.Compare(lbl.Text, "True", true) == 0) ? "Enabled" : "Disabled";
}
}
if (e.Row.RowType == DataControlRowType.DataRow && (e.Row.RowState & DataControlRowState.Edit) == DataControlRowState.Edit)
// {
// for (int i = 0; i < grdScenario.Rows.Count ; i++)
// {
// foreach (GridViewRow gvRow in grdScenario.Rows)
{
DropDownList ddlApp = (DropDownList)e.Row.FindControl("ddlAppDataRow");
DropDownList ddlTT = (DropDownList)e.Row.FindControl("ddlTestTypeDataRow");
DropDownList ddlST = (DropDownList)e.Row.FindControl("ddlScenarioTypeDataRow");
//Label lblapp = ((Label)e.Row.Cells[2].FindControl("lblapp"));
//Label lblTestType = ((Label)e.Row.Cells[3].FindControl("lblTestType"));
//Label lblScenarioType = ((Label)e.Row.Cells[4].FindControl("lblScenarioType"));
// Label lblapp = ((Label)grdScenario.Rows[i].Cells[2].FindControl("lblapp"));
//Label lblTestType = ((Label)grdScenario.Rows[i].Cells[3].FindControl("lblTestType"));
// Label lblScenarioType = ((Label)grdScenario.Rows[i].Cells[4].FindControl("lblScenarioType"));
if (ddlApp != null)
{
HiddenField hdnAppid = (HiddenField)e.Row.FindControl("hdnAppid");
ddlApp.DataSource = ds.Tables[4].DefaultView;
ddlApp.DataTextField = "AppName";
ddlApp.DataValueField = "SNO";
ddlApp.DataBind();
//ddlApp.SelectedIndex = ddlApp.Items.IndexOf(ddlApp.Items.FindByText(grdScenario.DataKeys[e.Row.RowIndex].Values[1].ToString()));
//ddlApp.SelectedItem.Text = lblapp.Text.ToString();
ddlApp.SelectedValue = hdnAppid.Value;
}
if (ddlTT != null)
{
HiddenField hdnTesttype = (HiddenField)e.Row.FindControl("hdnTesttype");
ddlTT.DataSource = ds.Tables[5].DefaultView;
ddlTT.DataTextField = "Testingtypedescription";
ddlTT.DataValueField = "TestingTypeID";
ddlTT.DataBind();
//ddlTT.SelectedItem.Text = lblTestType.Text.ToString();
ddlTT.SelectedValue = hdnTesttype.Value;
//ddlTT.SelectedIndex = ddlTT.Items.IndexOf(ddlTT.Items.FindByText(grdservices.DataKeys[e.Row.RowIndex].Values[2].ToString()));
}
if (ddlST != null)
{
HiddenField hdnScenariotype = (HiddenField)e.Row.FindControl("hdnScenariotype");
ddlST.DataSource = ds.Tables[6].DefaultView;
ddlST.DataTextField = "ScenarioTypeDescription";
ddlST.DataValueField = "ScenarioTypeID";
ddlST.DataBind();
ddlST.SelectedValue = hdnScenariotype.Value;
//ddlST.SelectedItem.Text = lblScenarioType.Text.ToString();
//ddlST.SelectedIndex = ddlST.Items.IndexOf(ddlST.Items.FindByText(grdScenario.DataKeys[e.Row.RowIndex].Values[3].ToString()));
}
}
// }
//// }
}
if (e.Row.RowType == DataControlRowType.Footer)
{
//Bind environment,platform at footer.
DropDownList cmbNewType = (DropDownList)e.Row.FindControl("ddlAppFooter");
cmbNewType.DataSource = ds.Tables[4].DefaultView;
cmbNewType.DataBind();
DropDownList cmbNewType1 = (DropDownList)e.Row.FindControl("ddlTestTypeFooter");
cmbNewType1.DataSource = ds.Tables[5].DefaultView;
cmbNewType1.DataBind();
DropDownList cmbNewType2 = (DropDownList)e.Row.FindControl("ddlScenarioTypeFooter");
cmbNewType2.DataSource = ds.Tables[6].DefaultView;
cmbNewType2.DataBind();
}
}
catch (System.Exception) { }
}

Categories