Failed to load viewstate - dynamic controls in ASP.NET using nested GridView - asp.net

In my project I am working on showing Task Group and Task (i.e. Parent-child) in one screen with the ability to Add/Edit/Delete the parent (Task Group) record and also to Add/Edit/Delete associated Task. For this I am using nested Grid View (parent-child).
I am implementing the sample application mentioned in the below link to my ASP.NET web based project.
http://www.codeproject.com/Articles/20047/Editable-Nested-GridView-All-in-One
my .ASPX looks as mentioned below:
<%# Page Language="C#" AutoEventWireup="true" MasterPageFile="~/Unicorn.Master" CodeBehind="TaskAndTaskGroup.aspx.cs" Inherits="Vipassi.Web.TaskGroup.TaskAndTaskGroup"%>
<asp:Content ID="Content1" ContentPlaceHolderID="phContent" runat="server">
<script language="javascript" type="text/javascript">
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 = "../../Resources/images/minus.gif";
}
else {
img.src = "../../Resources/images/minus.gif";
}
img.alt = "Close to view other TaskGroups";
}
else {
div.style.display = "none";
if (row == 'alt') {
img.src = "../../Resources/images/plus.gif";
}
else {
img.src = "../../Resources/images/plus.gif";
}
img.alt = "Expand to show Tasks";
}
}
</script>
<div>
<asp:GridView ID="GridView1" AllowPaging="True" BackColor="#f1f1f1"
AutoGenerateColumns="false" DataSourceID="ObjectDataSource1" DataKeyNames="task_group_id"
ShowFooter="true" Font-Size="Small"
Font-Names="Verdana" runat="server" GridLines="None" OnRowDataBound="GridView1_RowDataBound"
OnRowCommand="GridView1_RowCommand" OnRowUpdating="GridView1_RowUpdating" BorderStyle=Outset
OnRowDeleting="GridView1_RowDeleting" OnRowDeleted="GridView1_RowDeleted"
OnRowUpdated="GridView1_RowUpdated" AllowSorting="true">
<RowStyle BackColor="Gainsboro" />
<AlternatingRowStyle BackColor="White" />
<HeaderStyle BackColor="#0083C1" ForeColor="White" />
<FooterStyle BackColor="White" />
<Columns>
<asp:TemplateField>
<ItemTemplate>
<a href="javascript:expandcollapse('div<%# Eval("task_group_id") %>', 'one');">
<img id="imgdiv<%# Eval("task_group_id") %>" alt="Click to show/hide Tasks for TaskGroup <%# Eval("task_group_id") %>" width="9px" border="0" src="../../Resources/images/plus.gif" />
</a>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="TaskGroup ID" SortExpression="task_group_id">
<ItemTemplate>
<asp:Label ID="lblTaskGroupID" Text='<%# Eval("task_group_id") %>' runat="server"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:Label ID="lblTaskGroupID" Text='<%# Eval("task_group_id") %>' runat="server"></asp:Label>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtTaskGroupID" Text='' runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Task Group" SortExpression="task_group">
<ItemTemplate><%# Eval("task_group") %></ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtTaskGroup" Text='<%# Eval("task_group") %>' runat="server"></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtTaskGroup" Text='' runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Fiscal Year" SortExpression="Fiscal_Year">
<ItemTemplate><%# Eval("Fiscal_Year") %></ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtFiscalYear" Text='<%# Eval("Fiscal_Year") %>' runat="server"></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtFiscalYear" Text='' runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Is Active" SortExpression="Active_Flag">
<ItemTemplate><%# Eval("Active_Flag")%></ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtActiveFlag" Text='<%# Eval("Active_Flag") %>' runat="server"></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtActiveFlag" Text='' runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:CommandField HeaderText="Edit" ShowEditButton="True" />
<asp:TemplateField HeaderText="Delete">
<ItemTemplate>
<asp:LinkButton ID="linkDeleteTaskGroup" CommandName="Delete" runat="server">Delete</asp:LinkButton>
</ItemTemplate>
<FooterTemplate>
<asp:LinkButton ID="linkAddTaskGroup" CommandName="AddTaskGroup" runat="server">Add</asp:LinkButton>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<tr>
<td colspan="100%">
<div id="div<%# Eval("task_group_id") %>" style="display: none; position: relative; left: 15px; overflow: auto; width: 97%">
<asp:GridView ID="GridView2" AllowPaging="True" AllowSorting="true" BackColor="White" Width="100%" Font-Size="X-Small"
AutoGenerateColumns="false" Font-Names="Verdana" runat="server" DataKeyNames="task_group_id" ShowFooter="true"
OnPageIndexChanging="GridView2_PageIndexChanging" OnRowUpdating="GridView2_RowUpdating"
OnRowCommand="GridView2_RowCommand" OnRowEditing="GridView2_RowEditing" GridLines="None"
OnRowUpdated="GridView2_RowUpdated" OnRowCancelingEdit="GridView2_CancelingEdit" OnRowDataBound="GridView2_RowDataBound"
OnRowDeleting="GridView2_RowDeleting" OnRowDeleted="GridView2_RowDeleted" OnSorting="GridView2_Sorting"
BorderStyle="Double" BorderColor="#0083C1">
<RowStyle BackColor="Gainsboro" />
<AlternatingRowStyle BackColor="White" />
<HeaderStyle BackColor="#0083C1" ForeColor="White" />
<FooterStyle BackColor="White" />
<Columns>
<asp:TemplateField HeaderText="Task ID" SortExpression="task_id">
<ItemTemplate>
<asp:Label ID="lblTaskID" Text='<%# Eval("task_id") %>' runat="server"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:Label ID="lblTaskID" Text='<%# Eval("task_id") %>' runat="server"></asp:Label>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Task" SortExpression="task_name">
<ItemTemplate><%# Eval("task_name")%></ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtTask" Text='<%# Eval("task_name")%>' runat="server"></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtTask" Text='' runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Fiscal Year" SortExpression="fiscal_year">
<ItemTemplate><%# Eval("fiscal_year")%></ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtTaskFiscalYear" Text='<%# Eval("fiscal_year")%>' runat="server"></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtTaskFiscalYear" Text='' runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Is Active" SortExpression="Active_Flag">
<ItemTemplate><%# Eval("Active_Flag")%></ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtTaskActiveFlag" Text='<%# Eval("Active_Flag")%>' runat="server"></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtTaskActiveFlag" Text='' runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:CommandField HeaderText="Edit" ShowEditButton="True" />
<asp:TemplateField HeaderText="Delete">
<ItemTemplate>
<asp:LinkButton ID="linkDeleteTaskGroup" CommandName="Delete" runat="server">Delete</asp:LinkButton>
</ItemTemplate>
<FooterTemplate>
<asp:LinkButton ID="linkAddTask" CommandName="AddTask" runat="server">Add</asp:LinkButton>
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</td>
</tr>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:ObjectDataSource SortParameterName="sortExpression" EnablePaging="true" ID="ObjectDataSource1"
MaximumRowsParameterName="maxRecords" TypeName="Vipassi.BLL.Classes.TaskGroup.TaskGroupBLL"
SelectMethod="ListforTaskGroup" SelectCountMethod="ListCountforTaskGroup" StartRowIndexParameterName="startIndex"
runat="server">
<SelectParameters>
<asp:SessionParameter SessionField="orgId" Name="orgId" Type="Int32" />
</SelectParameters>
</asp:ObjectDataSource>
</div>

Option 1:
You can disable the VIEWSTATE of ASPX page /User Control :
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="~/TaskAndTaskGroup.aspx.cs" Inherits="Vipassi.Web.TaskGroup.TaskAndTaskGroup" EnableViewState="false"%>
Option 2:
You can also change your source code to make sure the controls added during a post-back must match the type and position of the controls added during the initial request.
Hope this will help you.

Related

Get current GridView

I've got a page with 3 GridViews.
<div id="Export" runat="server" class="headeropt noprint">
<asp:LinkButton runat="server" ID="buttonexport" OnClick="buttonexport_Click" OnClientClick="Javascript:Noshow();" CssClass="btn btn-default">
<asp:Image runat="server" ImageUrl="/images/Excel-16.gif" />
<asp:Label runat="server" Text="Export" />
</asp:LinkButton>
</div>
<div class="col-md-12">
<asp:GridView ID="grid1" runat="server" CellPadding="0" CssClass="table" CellSpacing="0" OnRowDataBound="grid1_RowDataBound" AutoGenerateColumns="false" GridLines="Vertical" BorderStyle="Solid" ShowFooter="true" OnRowEditing="grid1_RowEditing" datakeynames="grid1">
<Columns>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:LinkButton ID="lnkname" runat="server" CausesValidation="false" CommandName="Edit" CommandArgument='<%# Eval("name") %>' Text='<%# Eval("name") %>'></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Company">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("company") %>'></asp:Label>
</ItemTemplate>
<ItemStyle CssClass="alinha-direita" />
<FooterStyle CssClass="alinha-direita" />
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:GridView ID="grid2" runat="server" CellPadding="0" CssClass="table" CellSpacing="0" OnRowDataBound="grid2_RowDataBound" AutoGenerateColumns="false" GridLines="Vertical" BorderStyle="Solid" ShowFooter="true" OnRowEditing="grid2_RowEditing" datakeynames="grid2">
<Columns>
<asp:TemplateField HeaderText="Product Family">
<ItemTemplate>
<asp:LinkButton ID="lnkprdfamily" runat="server" CausesValidation="false" CommandName="Edit" CommandArgument='<%# Eval("prdfamily") %>' Text='<%# Eval("prdfamily") %>'></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("quantity") %>'></asp:Label>
</ItemTemplate>
<ItemStyle CssClass="alinha-direita" />
<FooterStyle CssClass="alinha-direita" />
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:GridView ID="grid3" runat="server" CellPadding="0" CssClass="table" CellSpacing="0" OnRowDataBound="grid3_RowDataBound" AutoGenerateColumns="false" GridLines="Vertical" BorderStyle="Solid" ShowFooter="true" datakeynames="grid3">
<Columns >
<asp:TemplateField HeaderText="Product">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("product") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Price">
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Eval("price") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
as you can see, the first grid links to the second, and the second to the third.
What I want to know, is at any given time to get the grid I'm viewing at the moment.
Just edited adding the linkButton that exports the grid to excel. This is why I need the gridview that I'm currently on, so that the export command works at any time.
Here's the export code:
protected void buttonexport_Click(object sender, EventArgs c)
{
Master.Page.Form.Attributes.Remove("onsubmit");
CB.ChangeControlsToValue(grid1);
string attachment = "attachment; filename=File.xls";
//Response.ClearContent();
Response.AddHeader("content-disposition", attachment);
Response.ContentType = "application/ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
grid1.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
}
Thanks.
Nevemind, got a solution.
I've just checked the one that is visible at the moment and work with it.
bool list = grid1.Visible;
if (list)
{ CB.ChangeControlsToValue(grid1); }
and so on....

gridview paging alignment issue in Chrome and Mozilla

I have a gridview control with paging. Everything is good in IE.
But in Firefox and Chrome, the design breaks because of paging.
I attach the screenshots from IE and Firefox here:
In IE:
In Firefox:
Here is my grid view:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BorderWidth="0"
AllowPaging="true" ShowFooter="true" PageSize="3" Width="100%" OnPageIndexChanging="GridView1_PageIndexChanging"
OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowEditing="GridView1_RowEditing"
OnRowUpdating="GridView1_RowUpdating"
OnRowDeleting="GridView1_RowDeleting" OnRowCommand="GridView1_RowCommand" HorizontalAlign="Center">
<AlternatingRowStyle CssClass="gridAlternateRow" />
<FooterStyle CssClass="gridFooter" />
<PagerSettings Mode="NumericFirstLast" />
<PagerStyle HorizontalAlign="Center" CssClass="GridPager" VerticalAlign="Middle" Wrap="False" />
<HeaderStyle CssClass="GridHeader" />
<RowStyle CssClass="gridRow" />
<Columns>
<asp:TemplateField HeaderText="ID" Visible="false">
<ItemTemplate>
<asp:Label ID="lblID" runat="server" Text='<%#Eval("ID") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:Label ID="lbladd" runat="server"></asp:Label>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="From Date" HeaderStyle-Width="10%">
<ItemTemplate>
<asp:Label ID="lblFromDate" runat="server" Text='<%#Eval("FromDate") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox CssClass="myCalCss" ID="txtFromDate" runat="server" Text='<%#Eval("FromDate") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox CssClass="myCalCss" ID="txtAddFromDate" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="reqFromDate" ValidationGroup="ValgrpConUNIAnesthesiaFeeSchedule" ControlToValidate="txtAddFromDate" runat="server" ErrorMessage="*"></asp:RequiredFieldValidator>
</FooterTemplate>
<HeaderStyle Width="15%"></HeaderStyle>
</asp:TemplateField>
<asp:TemplateField HeaderText="To Date" HeaderStyle-Width="15%">
<ItemTemplate>
<asp:Label ID="lblToDate" runat="server" Text='<%#Eval("ToDate") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox CssClass="myCalCss" ID="txtToDate" runat="server" Text='<%#Eval("ToDate") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox CssClass="myCalCss" ID="txtAddToDate" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="reqToDate" ValidationGroup="ValgrpConUNIAnesthesiaFeeSchedule" ControlToValidate="txtAddToDate" runat="server" ErrorMessage="*"></asp:RequiredFieldValidator>
</FooterTemplate>
<HeaderStyle Width="15%"></HeaderStyle>
</asp:TemplateField>
<asp:TemplateField HeaderText="Procedure Code" HeaderStyle-Width="15%">
<ItemTemplate>
<asp:Label ID="lblProcedureCode" runat="server" Text='<%#Eval("ProcedureCode") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtProcedureCode" runat="server" Text='<%#Eval("ProcedureCode") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtAddProcedureCode" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="reqProcedureCode" ValidationGroup="ValgrpConUNIAnesthesiaFeeSchedule" ControlToValidate="txtAddProcedureCode" runat="server" ErrorMessage="*"></asp:RequiredFieldValidator>
</FooterTemplate>
<HeaderStyle Width="15%"></HeaderStyle>
</asp:TemplateField>
<asp:TemplateField HeaderStyle-Width="15%">
<ItemTemplate>
<span onclick="return confirm('Are you sure want to delete?')">
<asp:LinkButton CssClass="aDelete" ID="btnDelete" Text="Delete" runat="server" CommandName="Delete" />
</span>
<asp:LinkButton ID="btnEdit" Text="Edit" runat="server" CommandName="Edit" CssClass="aEdit" />
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton ID="btnUpdate" Text="Update" runat="server" CommandName="Update" CssClass="aUpdate" />
<asp:LinkButton ID="btnCancel" Text="Cancel" runat="server" CommandName="Cancel" CssClass="aSave" />
</EditItemTemplate>
<FooterTemplate>
<asp:Button ID="btnInsertRecord" runat="server" Text="Add" ValidationGroup="ValgrpConUNIAnesthesiaFeeSchedule" CommandName="Insert" />
</FooterTemplate>
<HeaderStyle Width="15%"></HeaderStyle>
</asp:TemplateField>
</Columns>
</asp:GridView>
I take a look at Firebug and this is what I got in both IE and Firefox. Same HTML.
What am I missing?

FormView Layout

This has driven me crazy a few times in the past but I've always fudged it and moved on. Today I want to try and fix it!
I quite frequently create a GridView to display data and a FormView directly beneath in Insert mode with controls in the InsertItemTemplate corresponding to the columns in the GridView, so that it looks like the FormView is just an empty row of the GridView awaiting new data entry. This works great.
The problem is that I can never get the widths of the 'columns' (really just textboxes) in the FormView to correspond to the widths of the columns in the GridView above.
In the example below, as you can see, both the GridView and FormView have a width of 100% and, sure enough, when the page is rendered they're both exactly the same width (I gave the FormView a border briefly to check). However, even though the widths of the textboxes in the FormView are identical to the widths of the columns in the GridView, they don't display that way. The textboxes are slightly wider, and by the time you get to the right-most column the cumulative effect means that the alignment is way out.
I'm guessing the problem is something to do with border widths or some other hidden element which is being rendered, but I can't figure it out. I have to say, the amount by which the alignment is out seems more than the couple of pixels a border would affect things.
<asp:GridView ID="gvTPR" runat="server" DataSourceID="SQLTPR" AutoGenerateColumns="false" DataMember="DefaultView" DataKeyNames="TPRID" Width="100%" >
<RowStyle HorizontalAlign="Center" />
<EditRowStyle BackColor="#ccffff" />
<HeaderStyle BackColor="#013b82" ForeColor="White" />
<Columns>
<asp:BoundField DataField="TPREnteredAt" HeaderText="Entered At" ReadOnly="True" SortExpression="TPREnteredAt" ItemStyle-Width="24%" ControlStyle-Width="90%" />
<asp:BoundField DataField="TPRTemp" HeaderText="Temp" ReadOnly="True" SortExpression="TPRTemp" ItemStyle-Width="12%" ControlStyle-Width="90%"/>
<asp:BoundField DataField="TPRPulse" HeaderText="Pulse" ReadOnly="True" SortExpression="TPRPulse" ItemStyle-Width="12%" ControlStyle-Width="90%"/>
<asp:BoundField DataField="TPRRespiration" HeaderText="Respiration" ReadOnly="True" SortExpression="TPRRespiration" ItemStyle-Width="12%" ControlStyle-Width="90%"/>
<asp:BoundField DataField="TPRPCV" HeaderText="PCV" ItemStyle-Width="12%" ControlStyle-Width="90%"/>
<asp:BoundField DataField="TPRTP" HeaderText="TP" ItemStyle-Width="12%" ControlStyle-Width="90%" />
<asp:CommandField ButtonType="Button" InsertVisible="False" ShowEditButton="True" ItemStyle-Width="16%" UpdateText="Save" ControlStyle-Width="60px" />
</Columns>
<EmptyDataTemplate>
No TPR records exist
</EmptyDataTemplate>
</asp:GridView>
<asp:FormView ID="fvTPR" runat="server" DataSourceID="SQLTPR" DefaultMode="Insert" Width="100%" >
<InsertItemTemplate>
<asp:textbox ID="lblEnteredAt" runat="server" Text="Will be added automatically" Width="24%" />
<asp:TextBox ID="txtTemp" runat="server" Text='<%# Bind("TPRTemp")%>' Width="12%" />
<asp:TextBox ID="txtPulse" runat="server" Text='<%# Bind("TPRPulse")%>' Width="12%" />
<asp:TextBox ID="txtRespiration" runat="server" Text='<%# Bind("TPRRespiration")%>' Width="12%" />
<asp:TextBox ID="txtPCV" runat="server" Text='<%# Bind("TPRPCV")%>' Width="12%" />
<asp:TextBox ID="txtTP" runat="server" Text='<%# Bind("TPRTP")%>' Width="12%" />
<asp:Button ID="btnAddTPR" runat="server" Text="Save" Width="5%" />
</InsertItemTemplate>
</asp:FormView>
This renders like this:
How about using GridView footer for add funcionality? This way you will have all columns in same table and you'l get around your positioning problems.
Here is an example of how your gridview should look like:
<asp:GridView ID="gvTPR" runat="server" DataSourceID="SQLTPR" AutoGenerateColumns="False" ShowFooter="True" DataKeyNames="TPRID" Width="100%" EnableModelValidation="True">
<RowStyle HorizontalAlign="Center" />
<EditRowStyle BackColor="#ccffff" />
<HeaderStyle BackColor="#013b82" ForeColor="White" />
<Columns>
<asp:TemplateField HeaderText="Entered At" SortExpression="TPREnteredAt">
<EditItemTemplate>
<asp:TextBox ID="lblEnteredAt" runat="server" Text='<%# Eval("TPREnteredAt") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("TPREnteredAt") %>'></asp:Label>
</ItemTemplate>
<ControlStyle Width="90%" />
<ItemStyle Width="24%" />
<FooterTemplate>
<asp:TextBox ID="lblEnteredAt" runat="server" Text="Will be added automatically" Width="24%" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Temp" SortExpression="TPRTemp">
<EditItemTemplate>
<asp:TextBox ID="txtTemp" runat="server" Text='<%# Eval("TPRTemp") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("TPRTemp") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtTemp" runat="server" Text='<%# Bind("TPRTemp")%>' Width="12%" />
</FooterTemplate>
<ControlStyle Width="90%" />
<ItemStyle Width="12%" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Pulse" SortExpression="TPRPulse">
<EditItemTemplate>
<asp:TextBox ID="txtPulse" runat="server" Text='<%# Eval("TPRPulse") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("TPRPulse") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtPulse" runat="server" Text='<%# Bind("TPRPulse")%>' Width="12%" />
</FooterTemplate>
<ControlStyle Width="90%" />
<ItemStyle Width="12%" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Respiration" SortExpression="TPRRespiration">
<EditItemTemplate>
<asp:TextBox ID="txtRespiration" runat="server" Text='<%# Eval("TPRRespiration") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Bind("TPRRespiration") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtRespiration" runat="server" Text='<%# Bind("TPRRespiration")%>' Width="12%" />
</FooterTemplate>
<ControlStyle Width="90%" />
<ItemStyle Width="12%" />
</asp:TemplateField>
<asp:TemplateField HeaderText="PCV">
<EditItemTemplate>
<asp:TextBox ID="txtPCV" runat="server" Text='<%# Bind("TPRPCV") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label5" runat="server" Text='<%# Bind("TPRPCV") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtPCV" runat="server" Text='<%# Bind("TPRPCV")%>' Width="12%" />
</FooterTemplate>
<ControlStyle Width="90%" />
<ItemStyle Width="12%" />
</asp:TemplateField>
<asp:TemplateField HeaderText="TP">
<EditItemTemplate>
<asp:TextBox ID="txtTP" runat="server" Text='<%# Bind("TPRTP") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label6" runat="server" Text='<%# Bind("TPRTP") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtTP" runat="server" Text='<%# Bind("TPRTP")%>' Width="12%" />
</FooterTemplate>
<ControlStyle Width="90%" />
<ItemStyle Width="12%" />
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="lnkbtnEdit" runat="Server" Text="Edit" CommandName="Edit"
CausesValidation="false"></asp:Button>
</ItemTemplate>
<EditItemTemplate>
<asp:Button ID="lnkbtnUpdate" runat="Server" Text="Save" CommandName="Update"
CausesValidation="true"></asp:Button><br />
<asp:LinkButton ID="lnkbtnCancel" runat="Server" Text="Cancel" CommandName="Cancel"
CausesValidation="false"></asp:LinkButton>
</EditItemTemplate>
<FooterTemplate>
<asp:LinkButton ID="lnkbtnInsert" runat="Server" Text="Save" CommandName="Insert"
CausesValidation="true"></asp:LinkButton>
</FooterTemplate>
</asp:TemplateField>
</Columns>
<EmptyDataTemplate>
No TPR records exist
</EmptyDataTemplate>
</asp:GridView>
Hope this helps!
Regards,
Uros

ASP.NET how to hide template field control based on another template field value in GridView

here my code:
<asp:GridView ID="_gvPLCs" runat="server" AutoGenerateColumns="False"
BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px"
CellPadding="4" DataSourceID="_dsPLCs" ForeColor="Black" GridLines="Horizontal"
onrowcommand="_gvPLCs_RowCommand" onrowdatabound="_gvPLCs_RowDataBound">
<Columns>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="lbnView" runat="server" CausesValidation="false"
CommandName="view" Text="View manual"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="PLCName" SortExpression="PLCName">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("PLCName") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("PLCName") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="PLCManual" SortExpression="PLCManual">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("PLCManual") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("PLCManual") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="LabName" SortExpression="LabName">
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("LabName") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("LabName") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="MachineName" SortExpression="MachineName">
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Bind("MachineName") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("MachineName") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="MaxAllowedToResDay"
SortExpression="MaxAllowedToResDay">
<ItemTemplate>
<asp:Label ID="Label5" runat="server" Text='<%# Bind("MaxAllowedToResDay") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox5" runat="server"
Text='<%# Bind("MaxAllowedToResDay") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#CCCC99" ForeColor="Black" />
<PagerStyle BackColor="White" ForeColor="Black" HorizontalAlign="Right" />
<SelectedRowStyle BackColor="#CC3333" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#333333" Font-Bold="True" ForeColor="White" />
</asp:GridView>
code behind:
protected void _gvPLCs_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType != DataControlRowType.DataRow)
return;
if (e.Row.Cells[2].Text == " ")
{
LinkButton lbnview = (LinkButton)e.Row.FindControl("lbnView");
lbnview.Visible = false;
}
}
this doesn't work with me..plz help
what i need is : when there is no a manual;the view manual link disappear
i tested it using this code :
if (e.Row.Cells[2].Text == string.Empty)
{
LinkButton lbnview = (LinkButton)e.Row.FindControl("lbnView");
lbnview.Visible = false;
}
but it always give me "" and always manaul field disappears
try this:
<asp:LinkButton ID="lbnView" runat="server" CausesValidation="false" CommandName="view" Text="View manual" Visible='<%# !string.IsNullOrEmpty(Bind("PLCName").ToString()) %>'></asp:LinkButton>

wcCategory.ascx file use in Gridview or detailsView cell

Work on Asp.Net vs 08 C#,use northwind database . I want wcCategory.ascx file use on gridview cell
Below in my wcCategory.ascx file
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<cc1:PopupControlExtender ID="TextBox1_PopupControlExtender" runat="server"
DynamicServicePath="" Enabled="True" ExtenderControlID="" PopupControlID="pnlPopeUp"
TargetControlID="TextBox1">
</cc1:PopupControlExtender>
<asp:Panel ID="pnlPopeUp" runat="server">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="CategoryID" DataSourceID="SqlDataSource1"
onselectedindexchanged="GridView1_SelectedIndexChanged">
<Columns>
<asp:TemplateField HeaderText="CategoryID" InsertVisible="False"
SortExpression="CategoryID">
<EditItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("CategoryID") %>'></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblCategoryID" runat="server" Text='<%# Bind("CategoryID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="CategoryName" HeaderText="CategoryName"
SortExpression="CategoryName" />
<asp:BoundField DataField="Description" HeaderText="Description"
SortExpression="Description" />
<asp:CommandField ShowSelectButton="True" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
SelectCommand="SELECT [CategoryID], [CategoryName], [Description] FROM [Categories]">
</asp:SqlDataSource>
</asp:Panel>
</ContentTemplate>
</asp:UpdatePanel>
Bellow is my .aspx file
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="ProductID" DataSourceID="SqlDataSource2">
<Columns>
<asp:TemplateField HeaderText="ProductID" InsertVisible="False"
SortExpression="ProductID">
<EditItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("ProductID") %>'></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("ProductID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ProductName" SortExpression="ProductName">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("ProductName") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("ProductName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="SupplierID" SortExpression="SupplierID">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("SupplierID") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("SupplierID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="CategoryID" SortExpression="CategoryID">
<EditItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("CategoryID") %>'></asp:TextBox>
<br />
<br />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="QuantityPerUnit"
SortExpression="QuantityPerUnit">
<EditItemTemplate>
<asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("QuantityPerUnit") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label5" runat="server" Text='<%# Bind("QuantityPerUnit") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="True" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
SelectCommand="SELECT [ProductID], [ProductName], [SupplierID], [CategoryID], [QuantityPerUnit] FROM [Products]">
</asp:SqlDataSource>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
</div>
I want my .ascx file use on gridview Category cell
Use a placeholder and on rowdatabound use findcontrol to find the placeholder and use loadcontrol to add your usercontrol to the controls collection of the placeholder. Does that help?

Resources