GridView will not update underlying data source - asp.net

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.

Related

in ASPX/VB using a gridview, how do I allow a checkbox on the row to be checked/unchecked without hitting some edit button?

I'm on an extreme time crunch, and its been years since doing any of this. I'm trying to take an old project, and enable a checkbox within the gridview.
My aspx gridview is:
<asp:GridView ID="grdReg" runat="server" AutoGenerateColumns="False" AllowSorting="True" EnableSortingAndPagingCallbacks="True" CssClass="table table-hover" GridLines="None">
<HeaderStyle CssClass="thead-dark" />
<Columns>
<asp:BoundField HeaderText="" DataField="pkID" ReadOnly="True"></asp:BoundField>
<asp:BoundField HeaderText="YourID" DataField="yourID" ReadOnly="True"></asp:BoundField>
<asp:BoundField HeaderText="Name" DataField="fullname" ReadOnly="True"></asp:BoundField>
<asp:BoundField HeaderText="Program" DataField="program" ReadOnly="True"></asp:BoundField>
<asp:BoundField HeaderText="Status" DataField="eStatus" ReadOnly="True"></asp:BoundField>
<asp:BoundField HeaderText="Recen" DataField="recent" ReadOnly="True"></asp:BoundField>
<asp:BoundField HeaderText="Enabled?" DataField="enabled" ReadOnly="False"></asp:BoundField>
</Columns>
</asp:GridView>
Now - the last column "Enabled?" is bound to a SQL Server bit field. Right now, when you display the aspx page - it shows 'True' or 'False' or blank if null.
Right now, I don't care if the field is a checkbox or shows the True/False - but when you click on it, I want it to toggle from T -> F or checked to unchecked. It's been too long since I've been in VB, and I need a simple way to accomplish this.
This will be a short term form, which is why I'm not terribly worried about "neatness" or polish. I just need it to work, and work quickly. If you give code snippets, PLEASE give them in context with the above code.
I finally solved by just clicking on the row to toggle the value. Not elegant, but works for now

Gridview Autosort Without allowing users to sort

Hello everyone here is the problem, and I hope you can help me
I use a Gridview in my asp.net page to show the data from my sql database.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1">
<Columns>
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name"></asp:BoundField>
<asp:BoundField DataField="Email" HeaderText="Email" SortExpression="Email"></asp:BoundField>
<asp:BoundField DataField="College" HeaderText="College" SortExpression="College"></asp:BoundField>
</Columns>
</asp:GridView>
The gridview itself is working fine but i want to auto sort by College but I DONT want the users to be able to sort anything on click. Hope you can help thanks.
In your Data-Source SqlDataSource1 add order by College.
And change the bound filed
<asp:BoundField DataField="Name" HeaderText="Name">
......
Set property of gridview AllowSorting = false

why selectedrowstyle of a gridview is not shown when doing asyncpostback?

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

Data Displayed Twice In Gridview (ASP.NET)

I'm trying to make a page where information from the database are displayed on a page. For this, I'm using a Gridview control. The data displays fine, but it displays the same information twice. So basically, two tables are drawn by ASP and placed side by side.
Heres the code I'm using:
<asp:GridView ID="PackagesGV" runat="server" Width="520px">
<Columns>
<asp:BoundField DataField="ID" HeaderText="Package ID"/>
<asp:BoundField DataField="PackageName" HeaderText="Package Name"/>
<asp:BoundField DataField="PackageText" HeaderText="Package Text"/>
<asp:BoundField DataField="PackageImageID" HeaderText="Package Image"/>
<asp:BoundField DataField="PageID" HeaderText="Page ID"/>
</Columns>
</asp:GridView>
Also, the SQL Stored Procedure is pulling all of the fields required by the Gridview. The SQL is basically
"SELECT [ID], [PackageName], [PackageText], [PackageImageID], [PageID] FROM [Packages]"
So I'm not requesting the information twice using the Stored Procedure.
I've started ASP.NET in July, so I apologise now if this is something really simple.
Thanks!
Michael
You need to either set the GridView.AutoGenerateColumns Property to false or not set up the columns.
If you choose the former method your grid definition will become:
<asp:GridView ID="PackagesGV" runat="server" Width="520px" AutoGenerateColumns="False">

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?

Resources