Template column automatically hides during postback - asp.net

On the initial load of this page my template column, in an asp data grid, displays fine.
When I click any controls that force a post back the data grid reloads with the template column hidden. There is no visible attribute being set in the html. There is no code changing the visible property of the column in the code-behind.
I tried to hard-code the column's visibility to true and the property reads true in the code-behind but the column still gets hidden.
When trying to hide/show other columns in the data grid I'm unable to change them via the code-behind.
Any ideas on what is causing this column to auto-hide or why I can't effectively change the visible property in the code-behind or html?
<asp:DataGrid ID="dg" runat="server" AllowSorting="True" AutoGenerateColumns="False" CssClass="datagridPWQ">
<Columns>
<asp:BoundColumn DataField="ID" HeaderText="ID" />
<asp:TemplateColumn HeaderText="Add" >
<ItemTemplate>
<asp:Button ID="btnAdd" runat="server" Text="Add" CommandName="Add"
CssClass="buttonQ" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "ID")%>' />
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
Side note: I have thought about using a ButtonColumn instead but I would still like to know why this doesn't work. I remember seeing a similar error months ago but I don't remember how it was solved.

Related

Enable a LinkButton nested inside a disabled GridView

I have a Gridview that has Enabled="False" (it contains many controls and in this circumstance i need them all to be disabled, easiest way is to set Enabled="False" on the Gridview).
The issue is I want to enable one LinkButton on each row within the disabled Gridview. I've tried finding the LinkButton on RowDataBound and enabling it there but it's still disabled. Can this be done? Can you have an enabled Button within a disabled GridView?
Simplified example of the code (real one is thousands of lines long)
<asp:GridView runat="server" enabled="false" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton runat="server" id="button-to-be-enabled" text="press me"/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

ASP.NET: How to re-flow GridView cells over multiple lines

I have an application which contains a table with two columns: messageid and messageDesc . Inside the messageDesc column there is a message description. When I try to bind the GridView to the messageDesc column, it renders the entire column on a single line, requiring the user to scroll horizontally to view the complete text. I want to prevent this by having the description flow over multiple lines. How do I do this?
I have tried using the ItemStyle attribute but it's not working. Please suggest a solution. In addition to this, I can't change the database message content.
Thanks in advance.
Warp label insdie div element and than try out this will surely work for you
<asp:TemplateField>
<ItemTemplate>
<div style="width:100px;">
<asp:Label ID="Label2" runat="server" Text="<%# Eval("description") %>"></asp:Label>
</div>
</ItemTemplate>
</asp:TemplateField>
or
<ItemStyle Wrap="true" Width="100px" />
You can display data in textbox using template field instead of bound field. See below, textbox code. Your TemplateField would look like this:
<asp:TemplateField>
<ItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" ReadOnly="True" Text='<%# Eval("MessageDesc") %>'
TextMode="MultiLine"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
The ReadOnly setting keeps users from editing the data while having it still enabled for them. In Edit mode, of course you would set ReadOnly to false.

how to add checked event to checkbox in databound datagrid in asp.net?

I have datagridview with multiple columns in my asp.net website. And I am displaying a backend sql stored procedure output into this grid using page OnLoad event. First column in the grid contains a checkbox. I have added this checkbox through ItemTemplate, so that all rows will have a checkbox for selecting the row. I want user able to select the checkbox and based on this selection I would like to perform a DB operation.
currently i am using like below, but couldn't able to trigger the event.
<asp:GridView ID="resultGridView" runat="server" >
<Columns>
<asp:TemplateField HeaderText="Processed">
<ItemTemplate>
<asp:CheckBox ID="CheckBoxProcess" runat="server" OnCheckedChanged="resultgrid_CellContentClick"
Checked="false" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
on my code behind, I have method resultgrid_CellContentClick() for checkbox selection change event. But this code, never executed when select checkbox on/off.
You didn't set AutoPostBack="true" in your checkbox, that's why your checkbox event handler did not work. Just set it...
<asp:CheckBox ID="CheckBoxProcess" AutoPostBack="true" runat="server"
OnCheckedChanged="resultgrid_CellContentClick" Checked="false" />

How to display a large amount of data in gridview

I am trying to show a large amount of data in gridview but the problem is that everytime data increase the gridview row size increase automatically.
is there any possible way that the data which is stored in my MS access Database display in multi line instead of one single long line.
If you are populating the GridView using AutoGenerate="true" make if AutoGenerate="false"
Then use asp:TemplateField to populate the GridView.
Now give an ItemStyle-Width and ItemStyle-Wrap.
<asp:TemplateField ItemStyle-Width="50px" ItemStyle-Wrap="true">
<ItemTemplate>
<asp:Label ID="ShipNameLabel" runat="server" Text='<%# Eval("ShipName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
The telerik gridview control supports multi line rows http://www.telerik.com/products/aspnet-ajax/grid.aspx.
However, this does cost. You may try displaying only a few columns, then the user has to click on the row to see the other information in a form view.
Don't understand your question fully but it sounds as a Repeater might work better for you where you have more control of the layout of the rendering.
If it is the amount of rows that are the problem I do recommend to introduce paging to limit amount of row displayed at once.
Update:
1) Set up a CssClass for the GridView itself and include the table-layout:fixed style. This tells the browser that you're going to specify the width of each cell. You may also want to include the overall width of the grid here as I mention in (3).
2) The first row of the table sets the width for each cell, and that's usually the HEADER row, not the item row, so use either HeaderStyle-CssClass or HeaderStyle-Width to set the width of the cell.
3) Make certain the table itself is wide enough to hold all of the cells. I added up all of my cell widths and used that to set the width via the CssClass attribute on the GridView.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns='false'>
<Columns>
<asp:BoundField DataField='Name' />
<asp:TemplateField>
<ItemTemplate>
<asp:TextBox ID="TextBox1" runat='server' ReadOnly="true" BorderStyle="None"
TextMode="MultiLine" Text='<%# Bind("Description") %>'
>
</asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
You may have to add some styling to gridview via .skin or css

Gridview loses ItemTemplate after columns are removed

I'm trying to bind a datatable to a gridview where I've removed some of the autogenerated columns in the code behind.
I've got two template columns and it seems that when I alter the gridview in code behind and remove the non-templated columns that the templates loose the controls that are in them.
Using the following as a sample, "Header A" will continue to be visible but "Header B" will dissapear after removing any columsn that are located at index 2 and above. I'm creating columns in my codebehind for the grid as a part of a reporting tool. If I don't remove the columns then there doesn't seem to be an issue.
<asp:GridView ID="DataGrid1" runat="server" AutoGenerateColumns="false" AllowPaging="True" PageSize="10" GridLines="Horizontal">
<Columns>
<asp:TemplateField HeaderText="Header A" >
<ItemTemplate >
Text A
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
Header B
</HeaderTemplate>
<ItemTemplate>
Text B
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
For i = 2 To DataGrid1.Columns.Count - 1
DataGrid1.Columns.RemoveAt(2)
Next
EDIT
So from what I've read this seems to be a problem that occurs when the grid is altered. Does anyone know of a good workaround to re-initialize the template columns or set them up again so that when the non-template columns are removed that hte templates don't get removed as well?
Do you need the GridView to have ViewState? Try turning off ViewState.
<asp:GridView ID="DataGrid1" runat="server" AutoGenerateColumns="false" AllowPaging="True" PageSize="10" GridLines="Horizontal" EnableViewState="false">
hi solved with visibile=false.
on databind .net will not associate values and don t create them on html page

Resources