我正在尝试在我的网络应用中使用GridView,但它让我发疯。在任何PostBack之后总是在页脚中丢失DropDownList项。一切顺利的唯一方法是在任何情况下从数据库中获取国家列表并将Countries_List绑定到页脚。这是正常的吗?
我试图调试我的代码,调试器执行RowDataBound中的行并绑定DropDownList但DropDownList仍然返回给我空!!
我的守则背后:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BL;
using DL;
namespace BulkShop.admin
{
public partial class ManageCities : System.Web.UI.Page
{
protected void Page_Init(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
GrdVw.DataSource = CitiesBL.GetAll();
GrdVw.DataBind();
DropDownList countryListNew = (DropDownList)GrdVw.FooterRow.FindControl("countries_DrpDwnLst");
countryListNew.DataSource = CountriesBL.GetAll();
countryListNew.DataBind();
//if (CitiesBL.GetAll().Count == 0)
//{
// Country cnt = new Country();
// City c = new City();
// c.ID = 1;
// c.Name = "Empty";
// cnt.ID = 1;
// cnt.Name = "Empty";
// c.Country = cnt;
// List<City> cities = new List<City>();
// cities.Add(c);
// GrdVw.DataSource = cities;
// GrdVw.DataBind();
//}
}
}
protected void GrdVw_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Footer)
{
if (IsPostBack)
{
DropDownList countryListNew = (DropDownList)GrdVw.FooterRow.FindControl("countries_DrpDwnLst");
countryListNew.DataSource = CountriesBL.GetAll();
countryListNew.DataBind();
}
}
if (e.Row.RowType == DataControlRowType.DataRow)
{
if ((e.Row.RowState & DataControlRowState.Edit) > 0)
{
//do the binding for the normal rows
DropDownList countryList = (DropDownList)e.Row.FindControl("countries_DrpDwnLst");
//bind dropdownlist
countryList.DataSource = CountriesBL.GetAll();
countryList.DataBind();
City a = (City)e.Row.DataItem;
countryList.SelectedValue = a.Country.ID.ToString();
}
}
}
protected void GrdVw_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
e.Cancel = true;
GrdVw.EditIndex = -1;
GrdVw.DataSource = CitiesBL.GetAll();
GrdVw.DataBind();
}
protected void Delete(object sender, EventArgs e)
{
string Msg = "";
bool success = false;
LinkButton lnkRemove = (LinkButton)sender;
int cityId = Convert.ToInt32(lnkRemove.CommandArgument);
GrdVw.EditIndex = -1;
//UPDATE DL METHOD
success = CitiesBL.Delete(cityId, out Msg);
//Display Msg
grdVwMsg_Lbl.Text = Msg;
if (success)
{
//GridView1.DataBind();
GrdVw.DataSource = CitiesBL.GetAll();
GrdVw.DataBind();
}
}
protected void GrdVw_RowEditing(object sender, GridViewEditEventArgs e)
{
grdVwMsg_Lbl.Text = "";
GrdVw.EditIndex = e.NewEditIndex;
GrdVw.DataSource = CitiesBL.GetAll();
GrdVw.DataBind();
}
protected void GrdVw_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string Msg = "";
bool success = false;
int cityId = Convert.ToInt32(GrdVw.DataKeys[e.RowIndex].Value.ToString());
GridViewRow row = (GridViewRow)GrdVw.Rows[e.RowIndex];
TextBox cityName = (TextBox)row.FindControl("name_TxtBx");
DropDownList countries = (DropDownList)row.FindControl("countries_DrpDwnLst");
City c = new City();
Country cnt = new Country();
c.ID = cityId;
c.Name = cityName.Text;
cnt.ID = Convert.ToInt32(countries.SelectedValue);
c.Country = cnt;
//UPDATE DL METHOD
success = CitiesBL.Update(c, out Msg);
//Display Msg
grdVwMsg_Lbl.Text = Msg;
if (success)
{
cityName.Text = "";
countries.SelectedIndex = 0;
GrdVw.EditIndex = -1;
//GridView1.DataBind();
GrdVw.DataSource = CitiesBL.GetAll();
GrdVw.DataBind();
}
}
protected void AddNew(object sender, EventArgs e)
{
//Get Data From The Form
TextBox newCity = (TextBox)GrdVw.FooterRow.FindControl("newCityName_TxtBx");
DropDownList countries = (DropDownList)GrdVw.FooterRow.FindControl("countries_DrpDwnLst");
City c = new City();
Country cnt = new Country();
c.Name = newCity.Text.Trim();
cnt.ID = Convert.ToInt32(countries.SelectedValue);
c.Country = cnt;
//Create & Initiate Local Variables
string msg = "";
bool success = false;
success = CitiesBL.Add(c, out msg);
grdVwMsg_Lbl.Text = msg;
if (success)
{
newCity.Text = "";
countries.SelectedIndex = 0;
//GridView1.DataBind();
GrdVw.DataSource = CitiesBL.GetAll();
GrdVw.DataBind();
}
}
}
}
我的ASPX页面:
<%@ Page Title="" Language="C#" MasterPageFile="~/admin/admin.Master" AutoEventWireup="true" CodeBehind="ManageCities.aspx.cs" Inherits="BulkShop.admin.ManageCities" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<asp:Label ID="grdVwMsg_Lbl" runat="server" Visible="true"></asp:Label>
<asp:GridView ID="GrdVw" ShowFooter="True" EnableViewState="true" runat="server" AutoGenerateColumns="False" DataKeyNames="ID" OnRowCancelingEdit="GrdVw_RowCancelingEdit" OnRowDataBound="GrdVw_RowDataBound" OnRowEditing="GrdVw_RowEditing" OnRowUpdating="GrdVw_RowUpdating">
<Columns>
<asp:TemplateField HeaderText="ID" InsertVisible="False" SortExpression="ID" Visible="false">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("ID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="City Name">
<EditItemTemplate>
<asp:TextBox ID="name_TxtBx" runat="server" Text='<%# Bind("Name") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="name_Lbl" runat="server" Text='<%# Bind("Name") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="newCityName_TxtBx" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Country">
<EditItemTemplate>
<asp:DropDownList ID="countries_DrpDwnLst" runat="server" DataTextField="Name" DataValueField="ID"></asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="countryName_Lbl" runat="server" Text='<%# Bind("Country.Name") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="countries_DrpDwnLst" runat="server" DataTextField="Name" DataValueField="ID"></asp:DropDownList>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lnkRemove" runat="server" CommandArgument = '<%# Eval("ID")%>' OnClientClick = "return confirm('Do you want to delete?')" Text = "Delete" OnClick = "Delete"></asp:LinkButton>
</ItemTemplate>
<FooterTemplate>
<asp:Button ID="btnAdd" runat="server" Text="Add" OnClick = "AddNew" />
</FooterTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="True" />
</Columns>
</asp:GridView>
</asp:Content>