Creating Edit Page in ASP.NET - asp.net

Im a newbie in asp.net and im about to do a edit page for my news and announcement page.
This is my sample code
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="news_id" DataSourceID="SqlDataSource1">
<Columns>
<asp:BoundField DataField="news_id" HeaderText="news_id" InsertVisible="False"
ReadOnly="True" SortExpression="news_id" />
<asp:BoundField DataField="news" HeaderText="news" SortExpression="news" />
<asp:BoundField DataField="news_date" HeaderText="news_date"
SortExpression="news_date" />
<asp:BoundField DataField="news_data" HeaderText="news_data"
SortExpression="news_data" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:TDBSConnectionString %>"
SelectCommand="SELECT [news_id], [news], [news_date], [news_data] FROM [ibsi.news]">
</asp:SqlDataSource>
I put the data into a gridview to show all the data from the database.
how can i use this dataview to edit my content or can what can i use in the toolbar to create one edit module.

The link http://msdn.microsoft.com/en-us/library/ms972948.aspx might be able to help you out.

The ASP.NET GridView allows for a row of data to be edited by setting the EditIndex property of the GridView, placing the entire row in edit mode....
To implement this,go throught following article of codeproject:
http://www.codeproject.com/Articles/18136/Edit-Individual-GridView-Cells-in-ASP-NET

Related

click event to update the gridview in ASP

It convenient to do the query by :
<asp: AccessDataSource >
I wish to update the query string and update the result by a click event.
<asp:Button id="Button1"
Text="To Inquiry"
OnClick="doInquiry"
runat="server"/>
How I can achieve to update the query string and do the query in in the "doInquiry" function; BUT, still using the following useful code.
<asp:AccessDataSource ID="AccessDataSource1" runat="server" DataFile="~/App_Data/Northwind.MDB" SelectCommand="SELECT * FROM [Categories]"></asp:AccessDataSource>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="CategoryID" DataSourceID="AccessDataSource1" EnableModelValidation="True">
<Columns>
<asp:BoundField DataField="CategoryID" HeaderText="CategoryID" InsertVisible="False" ReadOnly="True" SortExpression="CategoryID" />
<asp:BoundField DataField="CategoryName" HeaderText="CategoryName" SortExpression="CategoryName" />
<asp:BoundField DataField="Description" HeaderText="Description" SortExpression="Description" />
</Columns>
</asp:GridView>
Whenever you generate the HTML via the markup and the behavior is different from what you expect, check the generated HTML in your browser. You intended to set the onclick attribute of an HTML tag, however, OnClick is a property pointing to the server-side method, which manages the server-side click event. You intended to use the OnClientClick property.

Working with ASP.Net GridView sorting and paging

We set up an ASP.Net GridView and included sorting and paging. When the user clicks on the GridView column header links for sorting the data or if the user clicks on the numbers links at the bottom of the GridView for paging the data, nothing happens.
Here is a cut down version of the markup for the GridView:
<asp:UpdatePanel
ID="UpdatePanelSummary"
runat="server"
UpdateMode="Always">
<ContentTemplate>
<h1>Maintenance</h1>
<% '-- GridView (Grid) for summary. -- %>
<% '-- The user chooses a summary row from here and details are shown in a DetailsView. -- %>
<% '--------------------------------------------------------------------------------------- %>
<asp:GridView
ID="GridViewSummary"
runat="server"
AllowSorting="True"
AutoGenerateColumns="False"
DataKeyNames="ID"
Width="224px"
AllowPaging="True"
PageSize="7">
<Columns>
<asp:BoundField DataField="Unit" HeaderText="Unit"
SortExpression="Unit" />
<asp:BoundField DataField="TheName" HeaderText="Name"
SortExpression="TheName" />
<asp:BoundField DataField="ID"
HeaderText="ID" SortExpression="ID" InsertVisible="False" ReadOnly="True"
Visible="False" />
<asp:CommandField ButtonType="Button" SelectText="Select Unit Details"
ShowSelectButton="True" />
</Columns>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
Since nothing is happening, we are assuming we need to write some coding in the code-behind file. Can you show us what coding is needed to wake up the sorting and paging?
Check out this post
sorting and paging with gridview asp.net
Basically you need to add server side event handlers for sorting and paging.
Here is an example - you can copy/paste most of it.
http://www.dotnetspider.com/resources/1249-Grid-View-Paging-Sorting.aspx

Gridview ASP.NET is not shown when loading webpage

well I'm so new to ASP.NET I've been working on this project for about two weeks now, but I can't get a GridView with an objectdatasource to show when webpage loads, I can see all columns in design time.
The method returns a List and in design time I can see all columns with the right column name but when loading the service and the webpage afterwards nothing shows up. Any help will be appreciated. Regards to all readers.
The method receives a string value I've set a default to 'q'.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataSourceID="ObjectDataSource1">
<Columns>
<asp:BoundField DataField="Contraseña" HeaderText="Contraseña"
SortExpression="Contraseña" />
<asp:BoundField DataField="Id_User" HeaderText="Id_User"
SortExpression="Id_User" />
<asp:BoundField DataField="Nombre" HeaderText="Nombre"
SortExpression="Nombre" />
<asp:BoundField DataField="Tipo" HeaderText="Tipo" SortExpression="Tipo" />
<asp:BoundField DataField="Usuario" HeaderText="Usuario"
SortExpression="Usuario" />
</Columns>
</asp:GridView>
Add the following to your GridView:
<asp:GridView ID="GridView1" runat="server"
EmptyDataText="DOH! No Data!"
ShowHeaderWhenEmpty="True"
The code looks fine. You don't actually need to call databind() with Data Source Control, instead perhaps you need to check the return value in SelectMethod of objectdatasource.

I cannot make the headers of my gridview click able for sorting

I have a gridview that I populate based on a query to a db. I'm trying to add sorting on my gridview but I cannot make my column headers clickable. I have allow sorting set to true, and I have my OnSorting event set. My columns are of a few different types. I know the code I need to have in my code behind, but I cannot click on the headers for some reason. Any help on what I'm missing would be appreciated.
<asp:GridView ID="Grid1" runat="server"
AutoGenerateColumns="False"
OnSelectedIndexChanging="Selected_Row_Changing"
DataKeyNames="ApplicationId"
AllowPaging="True"
OnPageIndexChanging="Grid1_PageIndexChanging"
AllowSorting="True"
OnSorting="Grid1_Sorting"
OnRowCreated="OnRowCreated"
OnRowCommand="Grid1_RowCommand"
OnRowDataBound="Grid1_RowDataBound">
<Columns>
<asp:templatefield ...>
<itemtemplate>
<asp:linkbutton .../>
</itemtemplate>
</asp:templatefield>
<asp:BoundField ... />
<asp:HyperLinkField ... />
<asp:ButtonField ... />
</Columns>
</asp:GridView>
You don't have set the SortExpression, have you?
For example:
<asp:boundfield datafield="CompanyName"
headertext="CompanyName"
headerstyle-wrap="false"
sortexpression="CompanyName"/>
Make sure you're not setting the Header Template but rather set the HeaderText attribute for the TemplateField

Filter gridview

I have a gridview which I am binding through code behind, I want to filter the gridview based on the value given by the user in textbox.
It would be great if I'll be able to filter the gridview without any postback.
PLease help!
Thanks in advance
you could run a filter expression
<asp:sqldatasource id="articles" runat="server"
connectionstring="<%$ ConnectionStrings:aspa %>"
selectcommand="SELECT title, url, added, updated FROM aspx_articles ORDER BY title"
filterexpression="title LIKE '%{0}%' or url LIKE '%{0}%'">
<filterparameters>
<asp:controlparameter controlid="searchBox" propertyname="Text" />
</filterparameters>
</asp:sqldatasource>
or this way
Do you have a TextBox outside the GridView, and when you enter data into it and press a button, the GirdView should be filter on that data?
If so, make sure your select command can taka a parameter with the value you want to filter on. Add a ControlParameter to the SelectParameters colelction of the DataSource control (if you use a DataSource control).
Here is an example that uses the Northwind database, maybe this will help you:
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" />
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False"
DataKeyNames="ProductID" DataSourceID="SqlDataSource1" ShowFooter="True">
<Columns>
<asp:CommandField ShowSelectButton="True" />
<asp:BoundField DataField="ProductID" HeaderText="ProductID" InsertVisible="False"
ReadOnly="True" SortExpression="ProductID" />
<asp:BoundField DataField="ProductName" HeaderText="ProductName" SortExpression="ProductName" />
<asp:BoundField DataField="UnitsInStock" HeaderText="UnitsInStock" SortExpression="UnitsInStock" />
<asp:BoundField DataField="UnitPrice" HeaderText="UnitPrice" SortExpression="UnitPrice" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
SelectCommand="SELECT [ProductID], [ProductName], [UnitsInStock], [UnitPrice] FROM [Alphabetical list of products] WHERE ([ProductName] LIKE '%' + #ProductName + '%')">
<SelectParameters>
<asp:ControlParameter ControlID="TextBox1" DefaultValue="%" Name="ProductName" PropertyName="Text"
Type="String" />
</SelectParameters>
</asp:SqlDataSource>
codes found here http://forums.asp.net/p/1034014/2904713.aspx
If you're using paging, I would recommend using something else (like the Microsoft Ajax Library's dataView). Because gridview paging and client-side filtering wouldn't mesh too well. But if you're not doing paging, you could do something similar to this or this.
The grid view is made for manipulation during post back. If you were to do this entirely on the client side, you'd use a JavaScript suite that would work on any table, not confined to the grid view. If it were me, I would simply use AJAX by wrapping the grid view and text box in an Update Panel. To the end user, the behavior is the same.
EDIT to include Sample code:
<asp:ScriptManager ID="ScriptManager1" AllowCustomErrorsRedirect="false" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:TextBox runat="server" ID="txtFilterString" ></asp:TextBox>
<asp:Button runat="server" ID="btnFilter" Text="FilterResults" onclick="btnFilter_Click" />
<asp:GridView runat="server" ID="grdResults"
DataKeyNames="Id"
AllowSorting="true" AllowPaging="true" PageSize="20"
PagerSettings-Mode="NumericFirstLast">
<Columns>
.....
</Columns>
</asp:GridView>
</asp:UpdatePanel>

Resources