473,394 Members | 1,718 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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="testgrid.aspx.cs" Inherits="testgrid" %>

<!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 id="Head1" runat="server">
<title>Grid View Add Update Delete</title>
</head>
<body>
<form id="form1" runat="server">
<div>
&nbsp;&nbsp;&nbsp;&nbsp;
<asp:GridView ID="GridView1" runat="server"
AutoGenerateColumns="False" DataKeyNames="CategoryID"
DataSourceID="SqlDataSource1" ShowFooter="true"
AllowPaging="True" AllowSorting="True"
OnRowCommand="GridView1_RowCommand">
<Columns>

<asp:CommandField ShowDeleteButton="True"
ShowEditButton="True"/>
<asp:TemplateField HeaderText="DDLB"
InsertVisible="True">
<EditItemTemplate>

</EditItemTemplate>
<ItemTemplate>

</ItemTemplate>
<FooterTemplate>
<asp:DropDownList 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:DropDownList>

</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="CategoryID"
InsertVisible="False" SortExpression="CategoryID">
<EditItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<
%# Eval("CategoryID") %>'></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<
%# Bind("CategoryID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="CategoryName"
SortExpression="CategoryName">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server"
Text='<%# Bind("CategoryName") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<
%# Bind("CategoryName") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="CategoryNameTextBox"
Runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Description"
SortExpression="Description">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server"
Text='<%# Bind("Description") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<
%# Bind("Description") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="DescriptionTextBox"
Runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:templatefield>
<footertemplate>
<asp:linkbutton id="btnNew"
runat="server" commandname="New" text="New" />
</footertemplate>
</asp:templatefield>

</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="Data Source=(local);Initial
Catalog=Northwind;Integrated Security=True"
DeleteCommand="DELETE FROM [Categories] WHERE [CategoryID]
= @CategoryID" InsertCommand="INSERT INTO [Categories]
([CategoryName], [Description]) VALUES (@CategoryName, @Description)"
ProviderName="System.Data.SqlClient" SelectCommand="SELECT
[CategoryID], [CategoryName], [Description] FROM [Categories]"
UpdateCommand="UPDATE [Categories] SET [CategoryName] =
@CategoryName, [Description] = @Description WHERE [CategoryID] =
@CategoryID">
<DeleteParameters>
<asp:Parameter Name="CategoryID" Type="Int32" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="CategoryName" Type="String" />
<asp:Parameter Name="Description" Type="String" />
<asp:Parameter Name="CategoryID" Type="Int32" />
</UpdateParameters>
<InsertParameters>
<asp:Parameter Name="CategoryName" Type="String" />
<asp:Parameter Name="Description" Type="String" />
</InsertParameters>
</asp:SqlDataSource>

</div>
</form>
</body>
</html>
--------------------- .aspx.cs page -----------------------------
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlClient;
using System.Collections.Generic;

public partial class testgrid : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
SetDefault();
}
protected void Page_LoadComplete(object sender, EventArgs e)
{
SetDefault();
}

protected void SetDefault()
{
DropDownList DDLB = GridView1.FooterRow.FindControl("DDLB") as
DropDownList;
DDLB.ClearSelection();
DDLB.SelectedValue = "Top Menu";
DDLB.DataBind();
}

protected void GridView1_RowCommand(object sender,
GridViewCommandEventArgs e)
{
SqlConnection conn = new SqlConnection("Data
Source=(local);Initial Catalog=Northwind;Integrated Security=True");
try
{
if (e.CommandName.Equals("New"))
{
LinkButton btnNew = e.CommandSource as LinkButton;
GridViewRow row = btnNew.NamingContainer as
GridViewRow;
if (row == null)
{
return;
}
TextBox txtCatName =
row.FindControl("CategoryNameTextBox") as TextBox;
TextBox txtDescription =
row.FindControl("DescriptionTextBox") as TextBox;
SqlCommand cmd = new SqlCommand(
"INSERT INTO [Categories] ([CategoryName],
[Description]) VALUES (@CategoryName, @Description)",
conn);
cmd.Parameters.AddWithValue("CategoryName",
txtCatName.Text);
cmd.Parameters.AddWithValue("Description",
txtDescription.Text);
conn.Open();
if (cmd.ExecuteNonQuery() == 1)
{
GridView1.DataBind();
}
}
}
catch (Exception ex)
{

}
finally
{
conn.Close();
}
}

}
Jun 27 '08 #1
0 1508

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

Similar topics

1
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...
10
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
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...
3
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...
0
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...
0
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...
2
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,...
3
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...
0
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...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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,...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.