PDA

View Full Version : Adding Functionality to Edit/Delete buttons in CodeBehind file?


Punkcrib
09-10-2007, 07:47 PM
Hello everyone. I have a pretty simple site so far that generates a GridView based on a user's selection. The GridView is created dynamically, and thus I cannot use the "Design Time" functions for Edit and Delete (as well as Sorting and Paging).

I have enabled Sorting, Paging, Edit, and Delete links in the code behind file, but I do not have any functions to handle these events. I am pretty new at ASP.net and VB (I'm a Java guy), so I'm not too sure how to go about adding the code for these functions to work properly. Everything I have seen is relating to the design-time enabling of these features. Obviously this is no help to me.

When I click on "edit" right now, the GridView simply disappears. I don't even know where to beging to make this function correctly.


So what I'm wondering is can anyone post sample code or point me to a site that has code for Editing, Deleting, Paging, and Sorting of a dynamcially created GridView??

Thanks alot! I have no idea where to even start! I've spent most of the day googling, and I can't find anytihng!

nikkiH
09-10-2007, 09:34 PM
I think you're doing this the hard way; you'll find that a totally dynamic gridview won't save view state, either.
What are you trying to accomplish?

Punkcrib
09-10-2007, 11:21 PM
Basically I am trying to make an "Admin" page, where the user (who has to log in w/ administrator password) can maintain the database (add, edit, or delete Categories, Subcategories, or individual Items). I guess this is your basic database management via webpage application.

Here is an image I quickly made to illustrate the functionality of the page:

http://www.codingforums.com/attachment.php?attachmentid=5640&stc=1&d=1189456343

The first dropdownlist (VIEW) will have 3 choices (hard coded): Categories, Subcategories, and Items. Depending on which the user selects, the other elements appear and are populated. Here are the actions taken when each is selected:

1) CATEGORIES - Only the GridView (table in the diagram) will display on the page, which will be populated with info from the Categories Table (CategoryID and CategoryName).

2) SUBCATEGORIES - The words "Filter Results" as well as "By Category" and it's DropDownList are displayed along with the GridView. If the user "filters" the Grid by selecting from the DDL, the SELECT command is changed for the GridView's current DataSource, showing only the Subcategories listed in the Category selected.

3) ITEMS - Everything shown above is displayed, and the user can filter the results similar to (2) as well as choose a Subcategory to filter (displaying on the items in this subcategory).


So far, everything works like it should (as far as displaying goes). I am not worried about viewstate because on each postback, the instance of the grid is created again. But now I want to make it functional in that the user can edit/delete items in the GridView (either Categories, Subcategories, or Items, depending on which level the user is looking at).


So what other easier ways would there be to do this? I tried to just have 3 different GridViews, and just make them invisible when not called, but I was getting a ViewState error when trying to replace the GridViews.

Thanks for your assistance.

nikkiH
09-10-2007, 11:59 PM
This is just a little demo to give you the idea.
I'm out of time for tonight, but if you want me to add some basic editing etc let me know.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default7.aspx.cs" Inherits="Default7" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<asp:SqlDataSource ID="sqlda_categories" runat="server" ConnectionString="<%$ ConnectionStrings:NORTHWIND %>"
OldValuesParameterFormatString="original_{0}" SelectCommand="SELECT [CategoryID], [CategoryName], [Description] FROM [Categories]">
</asp:SqlDataSource>
<asp:SqlDataSource ID="sqlda_products" runat="server" ConnectionString="<%$ ConnectionStrings:NORTHWIND %>"
OldValuesParameterFormatString="original_{0}" SelectCommand="SELECT [ProductID], [ProductName], [CategoryID] FROM [Products] WHERE ([CategoryID] = @CategoryID)">
<SelectParameters>
<asp:ControlParameter ControlID="ddlCategories" Name="CategoryID" PropertyName="SelectedValue"
Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
<asp:SqlDataSource ID="sqlda_invoices" runat="server" ConnectionString="<%$ ConnectionStrings:NORTHWIND %>"
OldValuesParameterFormatString="original_{0}" SelectCommand="SELECT [ProductID], [ProductName], [UnitPrice], [Quantity], [ShipName], [ShipAddress] FROM [Invoices]">
</asp:SqlDataSource>
<br />
<asp:DropDownList ID="DropDownList1" runat="server" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" AutoPostBack="true">
<asp:ListItem>Choose one</asp:ListItem>
<asp:ListItem>Categories</asp:ListItem>
<asp:ListItem>Products</asp:ListItem>
<asp:ListItem>Invoices</asp:ListItem>
</asp:DropDownList><br />
<asp:PlaceHolder ID="PlaceHolder1" runat="server" Visible="false">
<asp:Label runat="server" ID="lblFilter" Text="Filter by category: " />
<asp:DropDownList ID="ddlCategories" runat="server" DataSourceID="sqlda_categories"
DataTextField="CategoryName" DataValueField="CategoryID" AutoPostBack="true" OnSelectedIndexChanged="ddlCategories_SelectedIndexChanged">
</asp:DropDownList>
</asp:PlaceHolder>
<br />
<br />
<asp:GridView ID="gvMain" runat="server" AllowPaging="True" AllowSorting="True">
</asp:GridView>
</form>
</body>
</html>


using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Default7 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
ddlCategories.DataBind();
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
if (DropDownList1.SelectedIndex > 0)
{
switch (DropDownList1.SelectedValue)
{
case "Products":
gvMain.DataSourceID = "sqlda_products";
PlaceHolder1.Visible = true;
break;
case "Categories":
gvMain.DataSourceID = "sqlda_categories";
PlaceHolder1.Visible = false;
break;
case "Invoices":
gvMain.DataSourceID = "sqlda_invoices";
PlaceHolder1.Visible = true;
break;
}
gvMain.DataBind();

}
}

protected void ddlCategories_SelectedIndexChanged(object sender, EventArgs e)
{
sqlda_products.DataBind();
}
}