I am trying to implement GO TO page.Means when i enter any page number in text box "txtGoToPage" and clicking button "btnGo" it should take me to that page.
<asp:Label ID="lblGoToPage" runat="server" Text="Go To Page : "></asp:Label>
<asp:TextBox ID="txtGoToPage" runat="server" Width="47px"></asp:TextBox>
<asp:Button ID="btnGo" runat="server" Text="Go" OnClick="btnGo_Click" />
<asp:GridView ID="gv" runat="server" AutoGenerateColumns="false" OnRowDataBound="gv_RowDataBound" OnRowDeleting="gv_RowDeleting" OnRowEditing="gv_RowEditing" CellPadding="4" ForeColor="#333333" OnRowCreated="gv_RowCreated" OnRowCommand="gv_RowCommand" OnRowUpdating="gv_RowUpdating" AllowPaging="true" OnPageIndexChanging="gv_PageIndexChanging" GridLines="Both" CssClass="GridViewStyle" class="ui-widget-content" Style="width: 100%; padding: 2px" OnSelectedIndexChanged="gv_SelectedIndexChanged">
<EditRowStyle CssClass="GridViewEditRow" />
<RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
<PagerStyle CssClass="pager" />
<Columns>
<asp:TemplateField Visible="false" HeaderText="ID">
<ItemTemplate>
<asp:Label ID="lblID" runat="server" Text='<%#Eval("id") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Correct">
<ItemTemplate>
<asp:Label ID="lblCorrect" runat="server" Text='<%#Eval("correct") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtCorrect" runat="server" Width="40px" Text='<%#Eval("correct") %>' />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Comments">
<ItemTemplate>
<asp:Label ID="lblComments" runat="server" Text='<%#Eval("comments") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtComments" runat="server" TextMode="multiline" Width="50px" Columns="50" Rows="5" Text='<%#Eval("comments") %>' />
</EditItemTemplate>
</asp:TemplateField>
</Columns>
<PagerSettings FirstPageText="First" LastPageText="Last"
Mode="Numeric" PageButtonCount="20" PreviousPageText="Previous" />
</asp:GridView>
Thanks in advance.
Sample answer:
protected void btnGo_Click(object sender, EventArgs e)
{
GridView1.PageIndex = Convert.ToInt16(txtGoToPage.Text) -1; //since PageIndex starts from 0 by default.
txtGoToPage.Text = "";
GridView1.DataBind()
}
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 need to populate a GridView with list and I want to add a footer row so the user can add a new row.
This is what my front end looks like.
<asp:GridView ID="GridView1" AllowPaging="true" ShowFooter="true" PageSize="5" OnPageIndexChanging="GridView1_PageIndexChanging" OnRowEditing="GridView1_RowEditing"
runat="server" CellPadding="3" GridLines="None" CssClass="mGrid" PagerStyle-CssClass="pgr" AlternatingRowStyle-CssClass="alt"
OnRowCancelingEdit="GridView1_RowCancelingEdit">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton ImageUrl="~/Images/edit.png" runat="server" CommandName="Edit" ToolTip="Edit" Width="20px" Height="20px"/>
<asp:ImageButton ImageUrl="~/Images/delete.png" runat="server" CommandName="Delete" ToolTip="Delete" Width="20px" Height="20px"/>
</ItemTemplate>
<EditItemTemplate>
<asp:ImageButton ImageUrl="~/Images/save.png" runat="server" CommandName="Update" ToolTip="Update" Width="20px" Height="20px"/>
<asp:ImageButton ImageUrl="~/Images/cancel.png" runat="server" CommandName="Cancel" ToolTip="Cancel" Width="20px" Height="20px"/>
</EditItemTemplate>
<FooterTemplate>
<asp:ImageButton ImageUrl="~/Images/addnew.png" runat="server" CommandName="AddNew" ToolTip="Add New" Width="20px" Height="20px"/>
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
This is how I populate the GridView.
protected List<Emp> GetEmpList()
{
List<Emp> lEmp = new List<Emp>();
Emp oemp = new Emp(1234, "Upendra", "Noida");
lEmp.Add(oemp);
oemp = new Emp(1934, "Rahul", "Noida");
lEmp.Add(oemp);
//.......
return lEmp;
}
protected void BindGridList()
{
GridView1.DataSource = GetEmpList();
GridView1.DataBind();
}
Here is a picture of my grid.
My question is how can I add a footer row thank you for your help.
We need to modify the following to achieve Footer controls in GridView.
1) GridView - Change to AutoGenerateColumns="False"
2) Add all the columns to the Grid in the design view with the TemplateField
3) Describe EditItemTemplate, ItemTemplate, FooterTemplate for the columns
4) In the code behind file, access the values using GridView1.FooterRow.FindControl
Sample ASPX Code
<asp:GridView ID="GridView1" AllowPaging="True" ShowFooter="True" PageSize="5" OnPageIndexChanging="GridView1_PageIndexChanging" OnRowEditing="GridView1_RowEditing"
runat="server" CellPadding="3" GridLines="None" CssClass="mGrid" PagerStyle-CssClass="pgr" AlternatingRowStyle-CssClass="alt"
OnRowCancelingEdit="GridView1_RowCancelingEdit" AutoGenerateColumns="False">
<AlternatingRowStyle CssClass="alt"></AlternatingRowStyle>
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton ImageUrl="~/Images/edit.png" runat="server" CommandName="Edit" ToolTip="Edit" Width="20px" Height="20px" />
<asp:ImageButton ImageUrl="~/Images/delete.png" runat="server" CommandName="Delete" ToolTip="Delete" Width="20px" Height="20px" />
</ItemTemplate>
<EditItemTemplate>
<asp:ImageButton ImageUrl="~/Images/save.png" runat="server" CommandName="Update" ToolTip="Update" Width="20px" Height="20px" />
<asp:ImageButton ImageUrl="~/Images/cancel.png" runat="server" CommandName="Cancel" ToolTip="Cancel" Width="20px" Height="20px" />
</EditItemTemplate>
<FooterTemplate>
<asp:ImageButton ImageUrl="~/Images/addnew.png" runat="server" CommandName="AddNew" ToolTip="Add New" Width="20px" Height="20px" OnClick="btnAddNew_Click" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Id">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Id") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("Id") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Name") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("Name") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtNameFooter" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="City">
<EditItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("City") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("City") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtCityFooter" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Sample Code Behind Code:
protected void btnAddNew_Click(object sender, EventArgs e)
{
TextBox txtName = GridView2.FooterRow.FindControl("txtNameFooter") as TextBox;
TextBox txtCity = GridView2.FooterRow.FindControl("txtCityFooter") as TextBox;
cmd.CommandText = $"INSERT INTO tblLocation(Name,City) VALUES('{txtName.Text}','{txtCity.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");
}
}
I have a grid view within a grid view. When I click on edit of parent grid view the child grid view displays a checkbox. When I click on next page button of child grid view the controls that were made visible become invisible again. This is not the behaviour I require. I would like the controls that are made visible by clicking edit to remain visible through paging of the child grid view while parent grid view is in edit mode.
My markup:
<asp:GridView
ID="grdImages"
runat="server"
AllowPaging="true"
ShowFooter="true"
PageSize="5"
AutoGenerateColumns="false"
OnPageIndexChanging="grdImages_PageIndexChanging"
OnRowCancelingEdit="grdImages_RowCancelingEdit"
OnRowCommand="grdImages_RowCommand"
OnRowEditing="grdImages_RowEditing"
OnRowUpdating="grdImages_RowUpdating"
OnRowDeleting="grdImages_RowDeleting"
EmptyDataText="No Data Available at this Time"
OnRowDataBound="grdImages_RowDataBound"
DataKeyNames="productID" RowStyle-VerticalAlign="Top" RowStyle-HorizontalAlign="Center">
<AlternatingRowStyle BackColor="White" ForeColor="#284775"></AlternatingRowStyle>
<Columns>
<asp:TemplateField AccessibleHeaderText="Product ID" HeaderText="Product ID" FooterText="Product ID">
<ItemTemplate>
<asp:Label ID="lblProdId" runat="server" Text='<%# Eval("ProductId") %>' ></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="lstAddProdId" runat="server" AppendDataBoundItems="true" >
<asp:ListItem>Select a product</asp:ListItem>
</asp:DropDownList>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField AccessibleHeaderText="Product Main Image" FooterText="Product Main Image" HeaderText="Product Main Image">
<ItemTemplate>
<asp:Label ID="lblMainImgId" runat="server" Text='<%# Eval("ImageId") %>' ></asp:Label>
<asp:Label ID="lblMainImgName" runat="server" Text='<%# Eval("ImageName") %>' ></asp:Label> <br />
<asp:Image ID="imgMain" runat="server" Height="250" Width="250" ImageUrl='<%# Eval("ImagePath") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:Label runat="server" Font-Bold="true" Text="Current main image" ></asp:Label> <br />
<asp:Image ID="imgMain" runat="server" Height="250" Width="250" ImageUrl='<%# Eval("ImagePath") %>' /> <br />
<asp:Label runat="server" Font-Bold="true" Text="Upload a new image to replace the current main image." ></asp:Label> <br />
<asp:FileUpload ID="flupEditMain" runat="server" />
</EditItemTemplate>
<FooterTemplate>
<asp:FileUpload ID="flupMain" runat="server" AllowMultiple="false" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField AccessibleHeaderText="Supporting Images" FooterText="Supporting Images" HeaderText="Supporting Images">
<ItemTemplate>
<asp:Label ID="lblSupImages" runat="server" Visible="false" Font-Bold="true" Text="Select images to delete"></asp:Label><br />
<asp:GridView
ID="grdSupImages"
runat="server"
ShowHeader="false"
CellPadding="4"
ForeColor="#333333"
GridLines="None"
AutoGenerateColumns="False"
AllowPaging="true"
PageSize="4"
OnPageIndexChanging="grdSupImages_PageIndexChanging"
OnRowEditing="grdSupImages_RowEditing"
EnableViewState="true"
DataKeyNames="productID"
RowStyle-VerticalAlign="Top"
RowStyle-HorizontalAlign="Center"
EmptyDataText="No Supporting Images Found">
<AlternatingRowStyle BackColor="White" ForeColor="#284775"></AlternatingRowStyle>
<Columns>
<asp:TemplateField >
<ItemTemplate>
<asp:Image ID="imgSup" runat="server" ToolTip='<%# Eval("ImgId") %>' AlternateText='<%# Eval("ImageName") %>' ImageUrl='<%# Eval("ImagePath") %>' Height="125" Width="125" />
<asp:Label ID="imgSupName" runat="server" Text='<%# Eval("ImageName") %>' AssociatedControlID="imgSup"></asp:Label>
<asp:CheckBox ID="chkSupImages" runat="server" Visible="false" Text="Select Image" CommandName="Select" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EditRowStyle BackColor="#999999"></EditRowStyle>
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White"></FooterStyle>
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White"></HeaderStyle>
<PagerStyle HorizontalAlign="Center" BackColor="#284775" ForeColor="White"></PagerStyle>
<RowStyle BackColor="#F7F6F3" ForeColor="#333333"></RowStyle>
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333"></SelectedRowStyle>
<SortedAscendingCellStyle BackColor="#E9E7E2"></SortedAscendingCellStyle>
<SortedAscendingHeaderStyle BackColor="#506C8C"></SortedAscendingHeaderStyle>
<SortedDescendingCellStyle BackColor="#FFFDF8"></SortedDescendingCellStyle>
<SortedDescendingHeaderStyle BackColor="#6F8DAE"></SortedDescendingHeaderStyle>
</asp:GridView>
<div style="clear:both;"> </div>
<asp:Label ID="lblFlupSupImages" runat="server" Font-Bold="true" Text="Add extra images" Visible="false" />
<br />
<asp:FileUpload ID="flupSupImages" runat="server" AllowMultiple="true" Visible="false" />
</ItemTemplate>
<FooterTemplate>
<asp:FileUpload ID="flupExtra" runat="server" AllowMultiple="true" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Edit">
<ItemTemplate>
<asp:LinkButton ID="btnEdit" Text="Edit" runat="server" CommandName="Edit" />
<br />
<span onclick="return confirm('Are you sure you want to delete these images?')">
<asp:LinkButton ID="btnDelete" Text="Delete" runat="server" CommandName="Delete" />
</span>
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton ID="btnUpdate" Text="Update" runat="server" CommandName="Update" />
<br />
<asp:LinkButton ID="btnCancel" Text="Cancel" runat="server" CommandName="Cancel" />
</EditItemTemplate>
<FooterTemplate>
<asp:Button ID="btnAddRecord" runat="server" Text="Add" CommandName="Add"></asp:Button>
</FooterTemplate>
</asp:TemplateField>
</Columns>
<EditRowStyle BackColor="#999999"></EditRowStyle>
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White"></FooterStyle>
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White"></HeaderStyle>
<PagerStyle HorizontalAlign="Center" BackColor="#284775" ForeColor="White"></PagerStyle>
<RowStyle BackColor="#F7F6F3" ForeColor="#333333"></RowStyle>
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333"></SelectedRowStyle>
<SortedAscendingCellStyle BackColor="#E9E7E2"></SortedAscendingCellStyle>
<SortedAscendingHeaderStyle BackColor="#506C8C"></SortedAscendingHeaderStyle>
<SortedDescendingCellStyle BackColor="#FFFDF8"></SortedDescendingCellStyle>
<SortedDescendingHeaderStyle BackColor="#6F8DAE"></SortedDescendingHeaderStyle>
</asp:GridView>
My code behind:
protected void grdImages_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
GridView grd = (GridView)e.Row.FindControl("grdSupImages"); // find controls
Label prodId = (Label)e.Row.FindControl("lblProdId");
grd.ToolTip = prodId.Text;
int product = Convert.ToInt32(prodId.Text); // assign values to variables.
BindNestedGrid(product, grd); // call the function.
}
if (e.Row.RowType == DataControlRowType.DataRow && e.Row.RowIndex == grdImages.EditIndex)
{
//Find Child GridView
GridView supImagesGrd = (GridView)e.Row.Cells[2].FindControl("grdSupImages");
if (supImagesGrd != null)
{
// find grid header label and make visible
Label gridHead = (Label)e.Row.Cells[2].FindControl("lblSupImages");
gridHead.Visible = true;
// find fileupload header label and make visible.
Label fileUpHead = (Label)e.Row.Cells[2].FindControl("lblFlupSupImages");
fileUpHead.Visible = true;
// find fileupload control and make it visible.
FileUpload flup = (FileUpload)e.Row.Cells[2].FindControl("flupSupImages");
flup.Visible = true;
//Loop through the GridView
foreach (GridViewRow row in supImagesGrd.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
//Find the CheckBox
CheckBox chb = (CheckBox)row.Cells[0].FindControl("chkSupImages");
if (chb != null)
{
chb.Visible = true;
}
}
}
}
}
}
protected void grdSupImages_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView gv = (GridView)sender;
gv.PageIndex = e.NewPageIndex;
BindNestedGrid(Convert.ToInt32(gv.ToolTip), gv);
}
If any more information or code is required please let me know.
For anyone who wants to know, I disabled paging of the child grid when edit button is clicked so all check boxes show on all rows of child grid now.
<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