why selectedrowstyle of a gridview is not shown when doing asyncpostback? - asp.net

I have a gridview which contains a select commandfield.
I also have an updatepanel which contains a details view.
The gridview is outside update panel.
I want when selected button is pressed the selected row of the gridview to change color and the details to be shown in the details view without page refreshing.
Here is my code:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="Column1" DataSourceID="SqlDataSource1">
<Columns>
<asp:CommandField HeaderText="details" ShowSelectButton="True" />
<asp:BoundField DataField="Column1" HeaderText="Column1" SortExpression="Column1" />
</Columns>
<SelectedRowStyle BackColor="#FFFFFF" />
</asp:GridView>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" DataKeyNames="Column1" DataSourceID="SqlDataSource2" Height="50px" Width="125px">
<Fields>
<asp:BoundField DataField="Column1" HeaderText="Column1" InsertVisible="False" ReadOnly="True" SortExpression="Column1" />
<asp:BoundField DataField="Column2" HeaderText="Column2" SortExpression="Column2" />
</Fields>
</asp:DetailsView>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="GridView1" />
</Triggers>
</asp:UpdatePanel>
The problem is that gridview does not change the color of the selected row when it is outside the update panel.
I know that when i move it inside the updatepanel it will work but I want to know why this happens.
Thanks.
Forgot the most important thing:
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:MyConnectionString %>"
SelectCommand="SELECT * FROM Products WHERE (Column1=#Column1)">
<SelectParameters>
<asp:ControlParameter ControlID="GridView1" Name="Column1" PropertyName="SelectedValue" />
</SelectParameters>
</asp:SqlDataSource>
I think this is where the source of misbehavior lies...but still i am not sure why...

Yes my bad, I just replicated your scenario and the reason I found is really simple actually.
First, Asynchronous postbacks using the UpdatePanel will only refresh its child controls (that's why they are called partial render posts). Controls outside the UpdatePanel won't be refreshed.
As you mention:
I know that when i move it inside the updatepanel it will work but I want to know why this happens.
At first sight I thought that the GridView should do a full postback but nope, that's because you added this control as a trigger for the UpdatePanel
The reason is that you are registering all the events from the GridView as triggers of the UpdatePanel, and you are registering it using this <asp:AsyncPostBackTrigger ControlID="GridView1" /> which is causing the desired effect, firing the GridView events asynchronously to refresh the UpdatePanel, and since the GridView is outside, it is not refreshed
You could change this behavior, fixing the problem but your page will do a full postback:
<asp:PostBackTrigger ControlID="GridView1" />
As a reminder (for you and for me =p), we need to remember than whenever we register a control as a trigger of an UpdatePanel, its events will be caught in order to be sent asynchronously, causing sometimes the undesired effect of not rendering the source control if it's outside of the UpdatePanel

Related

Dropdown event stops firing after specific selection

I have 2 asp dropdown controls and an asp grid control in an Updatepanel. Update Panel is set properties as UpdateMode="Conditional" ChildrenAsTriggers="true".
On change event of both dropdown I am calling a method which fetches data from SQL Table and binds to grid control. This works fine till data is less. Whenever I select a value from dropdown for which data is around 900+ records, it binds data to grid without any error. But doesn't trigger any other dropdown change event.
<asp:UpdatePanel ID="uPanel" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="true">
<ContentTemplate>
<asp:DropDownList ID="ddldept" runat="server" OnSelectedIndexChanged="ddldept_SelectedIndexChanged"
AutoPostBack="True"/>
<asp:DropDownList ID="ddlYr" runat="server"OnSelectedIndexChanged="ddlYr_SelectedIndexChanged"
AutoPostBack="True"/>
<asp:DataList ID="gvData" runat="server" OnItemDataBound="gvData_ItemDataBound">
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="ddldept" EventName="SelectedIndexChanged" />
</Triggers>
</asp:UpdatePanel>
What could be the reason of event stops triggering?
EDIT 1:
Found error in console:
POST http://devserver:1111/mysite/SitePages/Main.aspx 500 (Internal Server Error)
ScriptResource.axd?d=FcwEEmnie6xhah_BvAg_MQP-cTp24dyFdRx9c2UxylFp5s8-W18rfLHBOC-uoS-F5J3jgyRFMZWkZS…:4803
Found the answer.
Just had to add following entry to web.config.
<appSettings>
<add key="aspnet:MaxHttpCollectionKeys" value="3001" />
</appSettings>
Started working

ASP.Net gridview always enters edit mode after an upgrade from platform 3.5 to 4

I have a long-term project (approx 9 years of continual use so far) that uses gridviews in several pages. The gridviews are used to display abbreviated summary details only, each with separate editing forms (hidden until called) being used.
I'm not using in-grid editing as the editing is too complex. The editing form is called and revealed either by the separate Add button or by a row's Edit linkbutton (using a ButtonField).
This approach has worked perfectly for several years upto and including .net 3.5. However, after a recent attempt to upgrade the project platform to .net 4 I found the grids entered in-line edit mode when:
The gridview Edit link button is clicked, and then
The edit form is dismissed by either the cancel button or save button methods
The gridview does not enter the editmode until after the actual editing form is dismissed. If the Save method is invoked, then a full re-bind of the gridview is involved, but it still enters editmode!
I have tried setting the grid's EditIndex to -1, but this doesn't make any difference.
If I reset the compilation target to 3.5 the grids all behave as expected again. I don't have this problem with projects originally started as .net 4 projects.
Partial UI code:
<asp:Button ID="cmdAdd" runat="server" Text=" Add " />
<asp:GridView ID="gvWPHA" runat="server" AutoGenerateColumns="false"
DataKeyNames="PORLID" AllowSorting="false"
EmptyDataText="No records yet" EmptyDataRowStyle-CssClass="qansYes">
<HeaderStyle CssClass="gvhead" />
<RowStyle CssClass="TRPrime" />
<AlternatingRowStyle CssClass="TRAlt" />
<SelectedRowStyle CssClass="TRSelect" />
<Columns>
<asp:ButtonField ButtonType="Link" Text="Edit" CommandName="Edit" />
<asp:BoundField DataField="PORLID" HeaderText="Record #" />
<asp:BoundField DataField="DateOfAppt" SortExpression="DateOfAppt" HeaderText="Appt Date" DataFormatString="{0:dd/MM/yyyy}" />
<asp:BoundField DataField="OutcomeDesc" HeaderText="Outcome" />
<asp:ButtonField ButtonType="Link" Text="Delete" CommandName="Delete" />
</Columns>
</asp:GridView>
<asp:UpdatePanel ID="upEdit" runat="server">
<ContentTemplate>
... Editing Form markup and validation, cancel button, save button, etc ...
</ContentTemplate>
</asp:UpdatePanel>
Has anyone else experienced this problem?
There was a change in the GridView's behavior in .NET 4.0. The Microsoft Connect bug report is here:
https://connect.microsoft.com/VisualStudio/feedback/details/554166/gridview-sets-editindex-property-when-in-previous-net-versions-it-didnt

Dynamic Data GridView Pager missing DynamicHyperlink Edit parameters

Edit: Feb 7, 2012 - Turns out the key parameter disappears from the GridView Edit link after a Sort as well, so it doesn't appear to be the Pager after all, but the problem persists... any ideas very welcome.
I have a Asp.Net Dynamic Data app. It uses the standard GridViewPager.aspx in Custom List.aspx which is marked up like this:
<asp:GridView ID="GridView1" runat="server" DataSourceID="GridDataSource" EnablePersistedSelection="True" CssClass="DDGridView" HeaderStyle-CssClass="th" RowStyle-CssClass="td" CellPadding="6" AllowSorting="True" AllowPaging="True" PageSize="3"
OnRowDataBound="GridView_OnRowDataBound" OnRowDeleting="GridView_OnRowDeleting">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:LoginView ID="LoginView1" runat="server">
<RoleGroups>
<asp:RoleGroup Roles="admin">
<ContentTemplate>
<asp:DynamicHyperLink ID="EditLink" runat="server" Action="Edit" Text="edit" />
<asp:LinkButton ID="LinkButton1" runat="server" CommandName="Delete" Text="delete" OnClientClick='return confirm("Are you sure you want to delete this item?");' />
</ContentTemplate>
</asp:RoleGroup>
</RoleGroups>
</asp:LoginView>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<PagerStyle CssClass="DDFooter" />
<PagerTemplate>
<asp:GridViewPager runat="server" />
</PagerTemplate>
<EmptyDataTemplate>
There are currently no items in this table.
</EmptyDataTemplate>
</asp:GridView>
On page 1 of the List all is well. The Edit Link rendered includes ?key=xxxxx and the Edit form opens to the correct record.
Once we page off of Page 1 however, the Edit Link rendered on each row has no 'key' parameter and the Edit form always opens to the first row in the database.
I am at a loss to explain this or even where to look. There is no custom code attached to any event associated with the Edit DynamicHyperlink or the GridviewPager.
Has anyone experienced this or have any suggestions as to what might be the issue?
Found the answer here:
LinkButton CommandArgument is empty when it is inside LoginView in a GridView
Turns out good 'ol Microsoft forgot to wire LoginView to fire row-level databind events inside a GridView. No databind, no link parameters!
In deference to a positive attitude, I'll make no comment on the level of organization it takes to allow that out the door....

Gridview buttonfield works LinkButton doesn't

I've been fighting this problem for many hours now and could really use some help :-)
This is the grid
<asp:GridView ID="annonceView" runat="server" AutoGenerateColumns="False" DataKeyNames="Id" DataSourceID="dataSourceAnnoncer">
<Columns>
<asp:BoundField DataField="Productname" HeaderText="Productname" />
<asp:buttonfield buttontype="Link" commandname="Delete" text="Delete"/>
<asp:TemplateField HeaderText="Administration">
<ItemTemplate>
<asp:LinkButton ID="lnkBtnDelete" runat="server" Text="Delete" CausesValidation="False" CommandName="Delete" OnClientClick="return confirm('Delete?')" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:LinqDataSource ID="dataSourceAnnoncer" runat="server" ContextTypeName="Namespcae.TheContext"
EnableDelete="True" TableName="Annoncer">
</asp:LinqDataSource>
Clicking the buttonfield deletes the record just fine. Clicking the LinkButton doesn't work. I get a postback and the grid is shown as empty and no record is deleted. Seems like an empty databinding.
I have tried to create a custom OnClick, OnCommand event for the LinkButton, but neither are fired. The OnRowCommand isn't fired either.
I don't manually DataBind in the codebehind.
The problem wasn't with Asp.net but with Sitecore (A CMS), the simple solution is described here, http://www.cassidy.dk/blog/sitecore/2009/01/typesthatshouldnotbeexpanded.html.
I feel like Sitecore has stolen several hours of my life, well the problem is solved, so I'm happy :-)
Your code looks fine and should work. Make sure that you aren't using the lnkBtnDelete Id somewhere else. Do you have both the buttonField and the TemplateField present at the same time? What happens if you remove the buttonfield?

GridView will not update underlying data source

So I'm been pounding on this problem all day. I've got a LinqDataSource that points to my model and a GridView that consumes it. When I attempt to do an update on the GridView, it does not update the underlying data source. I thought it might have to do with the LinqDataSource, so I added a SqlDataSource and the same thing happens. The aspx is as follows (the code-behind page is empty):
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="Data Source=devsql32;Initial Catalog=Steam;Persist Security Info=True;"
ProviderName="System.Data.SqlClient"
SelectCommand="SELECT [LangID], [Code], [Name] FROM [Languages]" UpdateCommand="UPDATE [Languages] SET [Code]=#Code WHERE [LangID]=#LangId">
</asp:SqlDataSource>
<asp:GridView ID="_languageGridView" runat="server" AllowPaging="True"
AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="LangId"
DataSourceID="SqlDataSource1">
<Columns>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
<asp:BoundField DataField="LangId" HeaderText="Id" ReadOnly="True" />
<asp:BoundField DataField="Code" HeaderText="Code" />
<asp:BoundField DataField="Name" HeaderText="Name" />
</Columns>
</asp:GridView>
<asp:LinqDataSource ID="_languageDataSource" ContextTypeName="GeneseeSurvey.SteamDatabaseDataContext" runat="server" TableName="Languages" EnableInsert="True" EnableUpdate="true" EnableDelete="true">
</asp:LinqDataSource>
What in the world am I missing here? This problem is driving me insane.
You are missing the <UpdateParameters> sections of your DataSources.
LinqDataSource.UpdateParameters
SqlDataSource.UpdateParameters
It turns out that we had a DataBind() call in the Page_Load of the master page of the aspx file that was probably causing the state of the GridView to get tossed out on every page load.
As a note - update parameters for a LINQ query are not required unless you want to set them some non-null default.
This is a total shot in the dark since I haven't used ASP at all.
I've been just learning XAML and WPF, which appears to be very similar to what you've posted above and I know that for some UI controls you need to specify the binding mode to two-way in order to get updates in both directions.

Resources