DropDownList列处于编辑模式的数据网格
本文关键字:模式 数据 数据网 网格 编辑 于编辑 DropDownList | 更新日期: 2023-09-27 18:26:17
我有一个带EditCommandColumn的可编辑DataGrid。最初,DataGrid显示从数据库中提取的正确值。问题是,当一行进入编辑模式时,具有DropDownLists的列会显示列表中的第一个值,而不是正确的值。是什么原因导致的?我该如何解决?
这是.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Kunde_drop.aspx.cs"Inherits ="Kunde_drop._Default" %>
<html>
<head id="Head1" runat="server">
<title></title>
</head>
<body>
<form id="Form1" runat="server">
<table id="Table1" style="height:176;width:344" cellspacing="1" cellpadding="1" border="0">
......
......
......
<asp:DataGrid id="DataGrid1" Runat="server" DataKeyField="kundennr"
AutoGenerateColumns="False" EditItemStyle-BackColor="#F7F7F7"
HeaderStyle-BackColor="#F7F7F7" CellPadding="4" HorizontalAlign="Left">
<FooterStyle BackColor="PaleGoldenrod"></FooterStyle>
<SelectedItemStyle BackColor="Control"></SelectedItemStyle>
<EditItemStyle BackColor="#F7F7F7"></EditItemStyle>
<ItemStyle BackColor="White"></ItemStyle>
<HeaderStyle BackColor="DarkKhaki"></HeaderStyle>
<Columns>
<asp:TemplateColumn HeaderText="Kundennr">
<HeaderStyle Width="30px"></HeaderStyle>
<ItemTemplate>
<asp:Literal ID="Label" Text='<%# DataBinder.Eval(Container.DataItem, "kundennr")%>' Runat="server" />
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Kundetyp">
<HeaderStyle Width="100px"></HeaderStyle>
<ItemTemplate>
<asp:Label ID="lblKtyp" Text='<%# DataBinder.Eval(Container.DataItem, "kundentyp")%>' runat="server"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:dropdownlist ID="Dropdownlist1" DataSource='<%# LoadKundentype() %>'
DataTextField="kundertype" runat="server"></asp:dropdownlist>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Firma">
<HeaderStyle Width="100px"></HeaderStyle>
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "firma") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="Textbox2" Text='<%# DataBinder.Eval(Container.DataItem, "firma") %>'Runat="server" />
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Anrede">
<HeaderStyle Width="100px"></HeaderStyle>
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "anrede") %>
</ItemTemplate>
<EditItemTemplate>
<asp:dropdownlist ID="Dropdownlist2" DataSource='<%# LoadAnrede() %>'
DataTextField="anrede" runat="server"></asp:dropdownlist>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Name">
<HeaderStyle Width="100px"></HeaderStyle>
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "name1") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="Textbox16" Text='<%# DataBinder.Eval(Container.DataItem, "name1") %>'Runat="server" />
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Ansprechperson">
<HeaderStyle Width="100px"></HeaderStyle>
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "answersprechperson") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="Textbox17" Text='<%# ataBinder.Eval(Container.DataItem,"answersprechperson") %>' Runat="server" />
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Strasse">
<HeaderStyle Width="100px"></HeaderStyle>
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "strasse") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="Textbox18" Text='<%# DataBinder.Eval(Container.DataItem, "strasse") %>' Runat="server" />
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Ort">
<HeaderStyle Width="100px"></HeaderStyle>
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "ort") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="Textbox19" Text='<%# DataBinder.Eval(Container.DataItem, "ort") %>' Runat="server" />
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="PLZ">
<HeaderStyle Width="100px"></HeaderStyle>
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "plz") %>
</ItemTemplate>
<EditItemTemplate>
<asp:Textbox ID="Textbox20" Text='<%# DataBinder.Eval(Container.DataItem, "plz") %>' Runat="server" />
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Email">
<HeaderStyle Width="100px"></HeaderStyle>
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "email") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="Textbox21" Text='<%# DataBinder.Eval(Container.DataItem, "email") %>' Runat="server" />
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Telefon 1">
<HeaderStyle Width="100px"></HeaderStyle>
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "telefon1") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="Textbox22" Text='<%# DataBinder.Eval(Container.DataItem, "telefon1") %>' Runat="server" />
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Telefon 2" ItemStyle-Wrap="False">
<HeaderStyle Width="100px"></HeaderStyle>
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "telefon2") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="Textbox23" Text='<%# DataBinder.Eval(Container.DataItem, "telefon2") %>' Runat="server" />
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Region">
<HeaderStyle Width="100px"></HeaderStyle>
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "region") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="Textbox24" Text='<%# DataBinder.Eval(Container.DataItem, "region") %>' Runat="server" />
</EditItemTemplate>
</asp:TemplateColumn>
<asp:EditCommandColumn ButtonType="LinkButton" UpdateText="Update" CancelText="Cancel" EditText="Edit"></asp:EditCommandColumn>
<asp:ButtonColumn Text="Delete" CommandName="Delete"></asp:ButtonColumn>
</Columns>
</asp:DataGrid></td>
</tr>
</table>
</form>
</body>
</html>
有.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web.SessionState;
using System.Web.UI.HtmlControls;
using MySql.Data;
using MySql.Data.MySqlClient;
using MySql.Data.Types;
namespace Kunde_drop
{
public partial class _Default : System.Web.UI.Page
{
private const string ConnStr = "SERVER=serv;DATABASE=dbk;UID=user;PASSWORD=pas;";
string strSort;
string strerrorMsg;
string strscriptString;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (strSort == "")
{
strSort = "IntegerValue";
}
BindDataGrid();
}
}
public ICollection LoadKundentype()
{
MySqlConnection con1 = new MySqlConnection(ConnStr);
con1.Open();
string cmd1 = "SELECT * FROM kunderetyp WHERE 1";
MySqlDataAdapter da = new MySqlDataAdapter(cmd1, con1);
DataSet ds = new DataSet();
da.Fill(ds, "kundertype");
DataTable dt = ds.Tables["kundertype"];
return dt.DefaultView;
}
public ICollection LoadAnrede()
{
MySqlConnection con2 = new MySqlConnection(ConnStr);
con2.Open();
string cmd2 = "SELECT * FROM anrede WHERE 1";
MySqlDataAdapter da = new MySqlDataAdapter(cmd2, con2);
DataSet ds = new DataSet();
da.Fill(ds, "anrede");
DataTable dt = ds.Tables["anrede"];
return dt.DefaultView;
}
private void BindDataGrid()
{
using (MySqlConnection con = new MySqlConnection(ConnStr))
using (MySqlCommand cmd = new MySqlCommand("SELECT * FROM kunde", con))
{
con.Open();
DataGrid1.DataSource = cmd.ExecuteReader(
CommandBehavior.CloseConnection |
CommandBehavior.SingleResult);
DataGrid1.DataBind();
}
}
private void UpdateInfo(int kundennr, string kundentyp, string firma, string anrede, string name1, string ansprechperson, string strasse, string ort, int plz, string email, string telefon1, string telefon2, string region)
{
using (MySqlConnection con = new MySqlConnection(ConnStr))
using (MySqlCommand cmd = new MySqlCommand("UPDATE kunde SET kundentyp = @kundentyp, firma = @firma, anrede = @anrede, name1 = @name1, ansprechperson = @ansprechperson, strasse = @strasse, ort = @ort, plz = @plz, email = @email, telefon1 = @telefon1, telefon2 = @telefon2, region = @region WHERE kundennr = @kundennr", con))
{
cmd.Parameters.Add("@kundentyp", MySqlDbType.VarChar, 255).Value = kundentyp;
cmd.Parameters.Add("@firma", MySqlDbType.VarChar, 255).Value = firma;
cmd.Parameters.Add("@anrede", MySqlDbType.VarChar, 255).Value = anrede;
cmd.Parameters.Add("@name1", MySqlDbType.VarChar, 255).Value = name1;
cmd.Parameters.Add("@ansprechperson", MySqlDbType.VarChar, 255).Value = ansprechperson;
cmd.Parameters.Add("@strasse", MySqlDbType.VarChar, 255).Value = strasse;
cmd.Parameters.Add("@ort", MySqlDbType.VarChar, 255).Value = ort;
cmd.Parameters.Add("@plz", MySqlDbType.Int32).Value = plz;
cmd.Parameters.Add("@email", MySqlDbType.VarChar, 255).Value = email;
cmd.Parameters.Add("@telefon1", MySqlDbType.VarChar, 20).Value = telefon1;
cmd.Parameters.Add("@telefon2", MySqlDbType.VarChar, 45).Value = telefon2;
cmd.Parameters.Add("@region", MySqlDbType.VarChar, 255).Value = region;
cmd.Parameters.Add("@kundennr", MySqlDbType.Int64).Value = kundennr;
con.Open();
cmd.ExecuteNonQuery();
}
}
private void DeleteInfo(int kundennr)
{
using (MySqlConnection con = new MySqlConnection(ConnStr))
using (MySqlCommand cmd = new MySqlCommand("DELETE FROM kunde WHERE kundennr = @kundennr", con))
{
cmd.Parameters.Add("@kundennr", MySqlDbType.Int64).Value = kundennr;
con.Open();
cmd.ExecuteNonQuery();
}
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.DataGrid1.CancelCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_CancelCommand);
this.DataGrid1.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_EditCommand);
this.DataGrid1.SortCommand += new System.Web.UI.WebControls.DataGridSortCommandEventHandler(this.DataGrid1_SortCommand);
this.DataGrid1.UpdateCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_UpdateCommand);
this.DataGrid1.DeleteCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_DeleteCommand);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void DataGrid1_SortCommand(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
{
}
private void DataGrid1_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
DataGrid1.EditItemIndex = (int)e.Item.ItemIndex;
BindDataGrid();
}
protected void DataGrid_RowEditing(object sender, GridViewEditEventArgs e)
{
DataGrid1.EditItemIndex = e.NewEditIndex;
BindDataGrid();
}
private void DataGrid1_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
try
{
int cUsrID;
string strKundentyp;
string strFirma;
string strAnrede;
string strName1;
string strAnsprechperson;
string strStrasse;
string strOrt;
string strEmail;
string strPlz;
int intPlz;
string strTelefon1;
string strTelefon2;
string strRegion;
Literal ltID;
TextBox txtTempFirma;
DropDownList drdTempAnrede;
TextBox txtTempName1;
TextBox txtTempAnsprechperson;
TextBox txtTempStrasse;
TextBox txtTempOrt;
TextBox txtTempPlz;
TextBox txtTempEmail;
TextBox txtTempTelefon1;
TextBox txtTempTelefon2;
TextBox txtTempRegion;
DropDownList drdList;
drdList = (System.Web.UI.WebControls.DropDownList)e.Item.Cells[1].FindControl("Dropdownlist1");
strKundentyp = drdList.Text;
ltID = (System.Web.UI.WebControls.Literal)e.Item.Cells[0].FindControl("Label");
cUsrID = Convert.ToInt32(ltID.Text);
txtTempFirma = (System.Web.UI.WebControls.TextBox)e.Item.Cells[2].FindControl("Textbox2");
strFirma = txtTempFirma.Text;
drdTempAnrede = (System.Web.UI.WebControls.DropDownList)e.Item.Cells[3].FindControl("Dropdownlist2");
strAnrede = drdTempAnrede.Text;
txtTempName1 = (System.Web.UI.WebControls.TextBox)e.Item.Cells[4].FindControl("Textbox16");
strName1 = txtTempName1.Text;
txtTempAnsprechperson = (System.Web.UI.WebControls.TextBox)e.Item.Cells[5].FindControl("Textbox17");
strAnsprechperson = txtTempAnsprechperson.Text;
txtTempStrasse = System.Web.UI.WebControls.TextBox)e.Item.Cells[6].FindControl("Textbox18");
strStrasse = txtTempStrasse.Text;
txtTempOrt = (System.Web.UI.WebControls.TextBox)e.Item.Cells[7].FindControl("Textbox19");
strOrt = txtTempOrt.Text;
txtTempPlz = (System.Web.UI.WebControls.TextBox)e.Item.Cells[8].FindControl("TextBox20");
strPlz = txtTempPlz.Text;
intPlz = Convert.ToInt32(strPlz);
txtTempEmail = (System.Web.UI.WebControls.TextBox)e.Item.Cells[9].FindControl("Textbox21");
strEmail = txtTempEmail.Text;
txtTempTelefon1 = (System.Web.UI.WebControls.TextBox)e.Item.Cells[10].FindControl("Textbox22");
strTelefon1 = txtTempTelefon1.Text;
txtTempTelefon2 = (System.Web.UI.WebControls.TextBox)e.Item.Cells[11].FindControl("Textbox23");
strTelefon2 = txtTempTelefon2.Text;
txtTempRegion = (System.Web.UI.WebControls.TextBox)e.Item.Cells[12].FindControl("Textbox24");
strRegion = txtTempRegion.Text;
UpdateInfo(cUsrID, strKundentyp, strFirma, strAnrede, strName1, strAnsprechperson, strStrasse, strOrt, intPlz, strEmail, strTelefon1, strTelefon2, strRegion);
DataGrid1.EditItemIndex = -1;
BindDataGrid();
}
catch (Exception ex)
{
strerrorMsg = ex.Message.Replace("'", @"""");
strscriptString = "<script language = Javascript>";
strscriptString += "window.status = '" + strerrorMsg + "';";
strscriptString += "</script>";
RegisterStartupScript("clientScript", strscriptString);
}
}
private void DataGrid1_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
try
{
int cID;
Literal ltID = null;
string ss = string.Empty;
ltID = (System.Web.UI.WebControls.Literal)e.Item.Cells[0].FindControl("Label");
cID = Convert.ToInt32(ltID.Text);
DeleteInfo(cID);
BindDataGrid();
}
catch (Exception ex)
{
strerrorMsg = ex.Message.Replace("'", @"""");
strscriptString = "<script language = Javascript>";
strscriptString += "window.status = '" + strerrorMsg + "';";
strscriptString += "</script>";
RegisterStartupScript("clientScript", strscriptString);
}
}
private void DataGrid1_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
DataGrid1.EditItemIndex = -1;
BindDataGrid();
}
}
}
首先,将DataValueField
添加到您的DropDownLists:
<asp:dropdownlist
ID="Dropdownlist1"
DataValueField="kundertype"
...
<asp:dropdownlist
ID="Dropdownlist2"
DataValueField="anrede"
...
其次,将onitemdatabound
事件添加到DataGrid:
<asp:DataGrid
onitemdatabound="DataGrid1_ItemDataBound"
...
第三,添加此方法来处理ItemDataBound事件:
protected void DataGrid1_ItemDataBound(object sender, DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.EditItem)
{
DropDownList dropDownList1 = (DropDownList)e.Item.FindControl("Dropdownlist1");
DataRowView dataItem1 = (DataRowView)e.Item.DataItem;
dropDownList1.SelectedValue = (string)dataItem1.Row["kundertype"];
DropDownList dropDownList2 = (DropDownList)e.Item.FindControl("Dropdownlist2");
DataRowView dataItem2 = (DataRowView)e.Item.DataItem;
dropDownList2.SelectedValue = (string)dataItem2.Row["anrede"];
}
}
还要注意,GridView已经成功地实现了DataGrid控件。你可能不值得改变,但你可以在这里查看比较。