问题一:
protected void grdVoucher_RowDataBound(object sender, GridViewRowEventArgs e)
{
DropDownList dropList = (DropDownList)e.Row.FindControl("dropZT");
if (dropList != null)
{
ListItem item1 = new ListItem("1", "1");
ListItem item2 = new ListItem("2", "2");
ListItem item3 = new ListItem("3", "3");
//DropDownList dropList = (DropDownList)e.Row.Cells[5].FindControl("dropZT");
dropList.Items.Add(item1);
dropList.Items.Add(item2);
dropList.Items.Add(item3);
dropList.DataBind();
}
}
为什么不做判断就会出错。怎么运行的。
是这样的:当窗体开始加载的时候,GridView绑定数据——引发行绑定事件——这时就像一个for循环,每个行、每个列挨着就行绑定,不可能一下子就找到DropDownList;再说只有点击了编辑项之后,DropDownList才会出现,即才会new出来,你找啥啊找。
所以必须判断之。
问题二:
我选择了DropDownList的项之后,然后点更新,为什么数据总是更新成为DropDownList的第一条呢?
那是因为不论是点击了更新还是编辑,他们都会自动回发,即引发窗体加载事件,而你有没有在窗体回发事件中做判断:if(!IsPostBack),而是又重新绑定了GridView的数据源,即又重新刷新了一次页面,DropDownList当然会默认选择了第一项,而你以前所选的项就取消了。
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
好长时间不做,再做的话就花很多的时间,把样本代码复制下来当作备案吧。
aspx里面的代码:
<%@ Page Language="C#" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Style="z-index: 100; left: 223px; position: absolute;
top: 104px" Text="代理证"></asp:Label>
<asp:TextBox ID="txtPermit" runat="server" Style="z-index: 101; left: 341px; position: absolute;
top: 102px"></asp:TextBox>
<asp:Label ID="Label2" runat="server" Style="z-index: 102; left: 625px; position: absolute;
top: 104px" Text="暂住证"></asp:Label>
<asp:TextBox ID="txtTemporary" runat="server" Style="z-index: 103; left: 732px; position: absolute;
top: 102px"></asp:TextBox>
<asp:GridView ID="grdVoucher" runat="server" BackColor="White"
BorderColor="White" BorderStyle="Ridge" BorderWidth="2px" CellPadding="3" CellSpacing="1"
GridLines="None" OnRowEditing="grdVoucher_RowEditing" Style="z-index: 104;
left: 36px; position: absolute; top: 175px; height: 131px; width: 1095px;"
OnRowCancelingEdit="grdVoucher_RowCancelingEdit"
OnRowUpdating="grdVoucher_RowUpdating" DataKeyNames="c_wzbh"
OnRowDataBound="grdVoucher_RowDataBound"
onrowdeleting="grdVoucher_RowDeleting">
<RowStyle BackColor="#DEDFDE" ForeColor="Black" />
<Columns>
<asp:BoundField DataField="c_wzbh" HeaderText="违章编号" ReadOnly="True" />
<asp:BoundField DataField="C_CPH" HeaderText="车牌号" ReadOnly="True" />
<asp:BoundField DataField="C_JSY" HeaderText="驾驶员" ReadOnly="True" />
<asp:BoundField DataField="C_WZDD" HeaderText="违章地点" ReadOnly="True" />
<asp:BoundField DataField="C_JCDW" HeaderText="检查单位" ReadOnly="True" />
<asp:TemplateField HeaderText="处理方式">
<EditItemTemplate>
<asp:DropDownList ID="dropZT" runat="server" Width="90px">
</asp:DropDownList>
<asp:HiddenField ID="HiddenField1" runat="server" Value='<%#eval_r("N_ZT") %>'/>
</EditItemTemplate>
<ItemTemplate>
<%# eval_r("N_ZT") %>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="c_ajh" HeaderText="安检号" ReadOnly="True" />
<asp:CommandField HeaderText="操作" ShowDeleteButton="True" ShowEditButton="True" />
</Columns>
<FooterStyle BackColor="#C6C3C6" ForeColor="Black" />
<PagerStyle BackColor="#C6C3C6" ForeColor="Black" HorizontalAlign="Right" />
<SelectedRowStyle BackColor="#9471DE" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#E7E7FF" />
</asp:GridView>
<asp:Button ID="btnSelect" runat="server" Style="z-index: 105; left: 1009px; position: absolute;
top: 101px" Text="查询" />
<asp:Button ID="Button1" runat="server" Style="z-index: 107; left: 989px; position: absolute;
top: 577px" Text="Button" />
</div>
</form>
</body>
</html>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
aspx.cs里面的代码:
using System;
using System.Data;
using System.Configuration;
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;
using System.Data;
using VoucherBLL;
public partial class _Default : System.Web.UI.Page
{
//Gridview的数据绑定
public void SetGridViewValue()
{
DataTable dt = VoucherManager.SelectTable();
grdVoucher.DataSource = dt;
grdVoucher.DataBind();
}
//窗体加载事件
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
SetGridViewValue();
}
}
protected void grdVoucher_RowEditing(object sender, GridViewEditEventArgs e)
{
grdVoucher.EditIndex = (int)e.NewEditIndex;
SetGridViewValue();
}
protected void grdVoucher_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string ID=grdVoucher.DataKeys[e.RowIndex].Value.ToString();
DropDownList dlist=(DropDownList)grdVoucher.Rows[e.RowIndex].Cells[5].FindControl("dropZT");
int value =Convert.ToInt32(dlist.SelectedValue);
int num = VoucherManager.UpdateZT(ID,value);
if (num > 0)
{
grdVoucher.EditIndex = -1;
Response.Write("<script>alert('更新成功');</script>");
SetGridViewValue();
}
else
{
grdVoucher.EditIndex = -1;
Response.Write("<script>alert('更新失败');</script>");
SetGridViewValue();
}
}
protected void grdVoucher_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
grdVoucher.EditIndex = -1;
SetGridViewValue();
}
protected void grdVoucher_RowDataBound(object sender, GridViewRowEventArgs e)
{
DropDownList dropList = (DropDownList)e.Row.FindControl("dropZT");
if (dropList != null)
{
ListItem item1 = new ListItem("1", "1");
ListItem item2 = new ListItem("2", "2");
ListItem item3 = new ListItem("3", "3");
dropList.Items.Add(item1);
dropList.Items.Add(item2);
dropList.Items.Add(item3);
dropList.DataBind();
//使DropList加载时自动选择原来数据的值。
HiddenField hid=(HiddenField)e.Row.Cells[5].FindControl("HiddenField1");
dropList.SelectedValue = hid.Value;
}
}
protected void grdVoucher_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string ID = grdVoucher.Rows[e.RowIndex].Cells[0].Text;
int num = VoucherManager.DelDateByID(ID);
}
}
~~~~~~~~~~~~~OK了。
也希望能帮助那些需要帮助的人们。