473,626 Members | 3,389 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

gridview defaults in footer row

This should be REALLY straightforward . I have a gridview that allows
editing, deleting and inserting (from the footer row). There is a Drop
Down ListBox in the one column, which needs to ALWAYS set to a default
value (but not always the same value, so I can't set it declaritively.
It will ultimately depend on a session variable as to which default to
use).

However, the point is that the DDL in the footer (insert) row must
ALWAYS be set, even after inserts, edits & deletes.

I dont think this is possible, though. I have tried everything!!!

Here is the simplified code, using Northwind, so easy to set up:

--------------------- .aspx page -----------------------------
<%@ Page Language="C#" AutoEventWireup ="true"
CodeFile="testg rid.aspx.cs" Inherits="testg rid" %>

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

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>Grid View Add Update Delete</title>
</head>
<body>
<form id="form1" runat="server">
<div>
&nbsp;&nbsp;&nb sp;&nbsp;
<asp:GridView ID="GridView1" runat="server"
AutoGenerateCol umns="False" DataKeyNames="C ategoryID"
DataSourceID="S qlDataSource1" ShowFooter="tru e"
AllowPaging="Tr ue" AllowSorting="T rue"
OnRowCommand="G ridView1_RowCom mand">
<Columns>

<asp:CommandFie ld ShowDeleteButto n="True"
ShowEditButton= "True"/>
<asp:TemplateFi eld HeaderText="DDL B"
InsertVisible=" True">
<EditItemTempla te>

</EditItemTemplat e>
<ItemTemplate >

</ItemTemplate>
<FooterTemplate >
<asp:DropDownLi st ID="DDLB" runat="server">
<asp:ListItem Text="" Value=""></asp:ListItem>
<asp:ListItem Text="Top Menu" Value="Top
Menu"></asp:ListItem>
<asp:ListItem Text="Side Menu" Value="Side
Menu"></asp:ListItem>
</asp:DropDownLis t>

</FooterTemplate>
</asp:TemplateFie ld>
<asp:TemplateFi eld HeaderText="Cat egoryID"
InsertVisible=" False" SortExpression= "CategoryID ">
<EditItemTempla te>
<asp:Label ID="Label1" runat="server" Text='<
%# Eval("CategoryI D") %>'></asp:Label>
</EditItemTemplat e>
<ItemTemplate >
<asp:Label ID="Label1" runat="server" Text='<
%# Bind("CategoryI D") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateFie ld>
<asp:TemplateFi eld HeaderText="Cat egoryName"
SortExpression= "CategoryNa me">
<EditItemTempla te>
<asp:TextBox ID="TextBox1" runat="server"
Text='<%# Bind("CategoryN ame") %>'></asp:TextBox>
</EditItemTemplat e>
<ItemTemplate >
<asp:Label ID="Label2" runat="server" Text='<
%# Bind("CategoryN ame") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate >
<asp:TextBox ID="CategoryNam eTextBox"
Runat="server"> </asp:TextBox>
</FooterTemplate>
</asp:TemplateFie ld>
<asp:TemplateFi eld HeaderText="Des cription"
SortExpression= "Descriptio n">
<EditItemTempla te>
<asp:TextBox ID="TextBox2" runat="server"
Text='<%# Bind("Descripti on") %>'></asp:TextBox>
</EditItemTemplat e>
<ItemTemplate >
<asp:Label ID="Label3" runat="server" Text='<
%# Bind("Descripti on") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate >
<asp:TextBox ID="Description TextBox"
Runat="server"> </asp:TextBox>
</FooterTemplate>
</asp:TemplateFie ld>
<asp:templatefi eld>
<footertemplate >
<asp:linkbutt on id="btnNew"
runat="server" commandname="Ne w" text="New" />
</footertemplate>
</asp:templatefie ld>

</Columns>
</asp:GridView>
<asp:SqlDataSou rce ID="SqlDataSour ce1" runat="server"
ConnectionStrin g="Data Source=(local); Initial
Catalog=Northwi nd;Integrated Security=True"
DeleteCommand=" DELETE FROM [Categories] WHERE [CategoryID]
= @CategoryID" InsertCommand=" INSERT INTO [Categories]
([CategoryName], [Description]) VALUES (@CategoryName, @Description)"
ProviderName="S ystem.Data.SqlC lient" SelectCommand=" SELECT
[CategoryID], [CategoryName], [Description] FROM [Categories]"
UpdateCommand=" UPDATE [Categories] SET [CategoryName] =
@CategoryName, [Description] = @Description WHERE [CategoryID] =
@CategoryID">
<DeleteParamete rs>
<asp:Paramete r Name="CategoryI D" Type="Int32" />
</DeleteParameter s>
<UpdateParamete rs>
<asp:Paramete r Name="CategoryN ame" Type="String" />
<asp:Paramete r Name="Descripti on" Type="String" />
<asp:Paramete r Name="CategoryI D" Type="Int32" />
</UpdateParameter s>
<InsertParamete rs>
<asp:Paramete r Name="CategoryN ame" Type="String" />
<asp:Paramete r Name="Descripti on" Type="String" />
</InsertParameter s>
</asp:SqlDataSour ce>

</div>
</form>
</body>
</html>
--------------------- .aspx.cs page -----------------------------
using System;
using System.Collecti ons;
using System.Configur ation;
using System.Data;
using System.Web;
using System.Web.Secu rity;
using System.Web.UI;
using System.Web.UI.H tmlControls;
using System.Web.UI.W ebControls;
using System.Web.UI.W ebControls.WebP arts;
using System.Data.Sql Client;
using System.Collecti ons.Generic;

public partial class testgrid : System.Web.UI.P age
{
protected void Page_Load(objec t sender, EventArgs e)
{
SetDefault();
}
protected void Page_LoadComple te(object sender, EventArgs e)
{
SetDefault();
}

protected void SetDefault()
{
DropDownList DDLB = GridView1.Foote rRow.FindContro l("DDLB") as
DropDownList;
DDLB.ClearSelec tion();
DDLB.SelectedVa lue = "Top Menu";
DDLB.DataBind() ;
}

protected void GridView1_RowCo mmand(object sender,
GridViewCommand EventArgs e)
{
SqlConnection conn = new SqlConnection(" Data
Source=(local); Initial Catalog=Northwi nd;Integrated Security=True") ;
try
{
if (e.CommandName. Equals("New"))
{
LinkButton btnNew = e.CommandSource as LinkButton;
GridViewRow row = btnNew.NamingCo ntainer as
GridViewRow;
if (row == null)
{
return;
}
TextBox txtCatName =
row.FindControl ("CategoryNameT extBox") as TextBox;
TextBox txtDescription =
row.FindControl ("DescriptionTe xtBox") as TextBox;
SqlCommand cmd = new SqlCommand(
"INSERT INTO [Categories] ([CategoryName],
[Description]) VALUES (@CategoryName, @Description)",
conn);
cmd.Parameters. AddWithValue("C ategoryName",
txtCatName.Text );
cmd.Parameters. AddWithValue("D escription",
txtDescription. Text);
conn.Open();
if (cmd.ExecuteNon Query() == 1)
{
GridView1.DataB ind();
}
}
}
catch (Exception ex)
{

}
finally
{
conn.Close();
}
}

}
Jun 27 '08 #1
0 1535

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
9349
by: Miguel Dias Moura | last post by:
Hello, I have a GridView in my page which is created in runtime. It works fine. My page has 2 Asp Buttons: - The HIDE button makes GridView.Visible = False; - The SHOW button makes GridView.Visible = True. I press HIDE and the GridView disappears as expected. After it I press SHOW and the GridView doesn't show.
10
5298
by: NH | last post by:
I have a girdview with paging enabled. How can I add a message in the footer to say "Viewing records 1-15 of 45" etc Thanks
0
7822
by: =?Utf-8?B?Vmlua2k=?= | last post by:
Hello Everyone, I have to display total time in the footer of my gridview so I have a columnn a. I followed micrososft example to create the footer and display the total time and it is not displaying anything in the footer of the gridview. Below is my code colA
3
1556
by: Toni | last post by:
In DataGrid and GridView I can't use paging and sorting. I am connected to database with code, not visual. Can I use paging and sorting if I populate data to grid with code, or I need to drag and drop connection, dataset, datatable etc.
0
1785
by: =?Utf-8?B?Vmlua2k=?= | last post by:
Hello Everyone, I have a gridview. I calculated some values in the gridview for the footer of the gridview so basically I am adding all the values that are displayed in the gridview for each column and displaying the total in the footer of the gridview. Now I export the gridview to excel spreadsheet with the code below. Everything is exported well except the footer value become zero in the excel spreadsheet. Can anyone tell me how can i...
0
2672
by: gnewsgroup | last post by:
Well, I am trying to use the footer row of a GridView for insertion purpose. There are some articles about this, for example, the gridviewguy.com has an example, which always displays the footer row. I would like to display the footer row, only when a LinkButton "Add New" is clicked. And after the new record is saved to the database, let the footer row disappear. In the LinkButton's click event handler, I simply say:
2
10841
by: shapper | last post by:
Hello, I am working with a ListView but I suppose that with a GridView might be the same. Instead of having an Insert Button on each GridView row I would like to have only one Insert button, for example, in the GridView footer. When this button is clicked it should put the GridView in Insert Mode and the EditTemplate should be showed, maybe (?), in the last GridView
3
9812
by: tshad | last post by:
I am building a GridView that is displaying some money values in 3 columns. I want to put the totals of each column in a label field (one for each column) in the footer. I was trying to figure out which was the faster way or more efficient way. I originally thought about getting SQL to do it, but that would entail either a second Select or some type of subquery or self join, which I am not sure is the best way.
0
135
by: sean_walsh | last post by:
This should be REALLY straightforward. I have a gridview that allows editing, deleting and inserting (from the footer row). There is a Drop Down ListBox in the one column, which needs to ALWAYS set to a default value (but not always the same value, so I can't set it declaritively. It will ultimately depend on a session variable as to which default to use). However, the point is that the DDL in the footer (insert) row must ALWAYS be set,...
0
8268
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8202
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8707
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8641
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7199
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6125
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5575
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4202
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1512
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.