在DataGrid中有两个编辑摸版 商品名称 商品型号
EditItemTemplate 是DropDownList
点DataGrid行上的“编辑”时候
DropDownList1 商品名称 和 DropDownList2 商品型号 都绑定好的现在的问题是我现在选择DropDownList1 的商品名称时 对应的DropDownList2 商品型号发生改变
平时在页面上两个DropDownList实现联动很好实现,但现在在DataGrid的编辑状态下就不知道怎么下手了,请大家指教!!!!!!!!谢谢
EditItemTemplate 是DropDownList
点DataGrid行上的“编辑”时候
DropDownList1 商品名称 和 DropDownList2 商品型号 都绑定好的现在的问题是我现在选择DropDownList1 的商品名称时 对应的DropDownList2 商品型号发生改变
平时在页面上两个DropDownList实现联动很好实现,但现在在DataGrid的编辑状态下就不知道怎么下手了,请大家指教!!!!!!!!谢谢
.aspx 代码:public DataSet GetType(string strLaborguardName)
{
string strband="select typeid,typename from type,laborguard where laborguard.laborguardid=type.laborguardid and laborguardname='"+strLaborguardName+"'";
this.myclass.ConOpen();
OracleDataAdapter da=new OracleDataAdapter();
OracleCommand cmd=new OracleCommand(strband,this.myclass.cnn);
da.SelectCommand=cmd;
DataSet ds=new DataSet();
da.Fill(ds);
this.myclass.ConClose();
return ds;
} public DataSet GetSelectlb(string WorkTypeName,string strLaborguardName)
{
string strband="select selectid,selectname from LSS_VIEW where worktypename='"+WorkTypeName+"' and laborguardname='"+strLaborguardName+"'";
this.myclass.ConOpen();
OracleDataAdapter da=new OracleDataAdapter();
OracleCommand cmd=new OracleCommand(strband,this.myclass.cnn);
da.SelectCommand=cmd;
DataSet ds=new DataSet();
da.Fill(ds);
this.myclass.ConClose();
return ds;
}
.cs 代码:
<asp:TemplateColumn HeaderText="名称">
<HeaderStyle Width="0px"></HeaderStyle>
<ItemTemplate>
<asp:Label id="lblaborname" runat="server">
<%# DataBinder.Eval(Container, "DataItem.laborguardname") %>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList id=dplbselect runat="server" AutoPostBack="True" Width="100%" DataValueField="selectid" DataTextField="selectname" DataSource='<%# GetSelectlb(DataBinder.Eval(Container, "DataItem.worktype").ToString(),DataBinder.Eval(Container, "DataItem.laborguardname").ToString())%> ' >
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="型号">
<ItemTemplate>
<asp:Label id="Label1" runat="server">
<%# DataBinder.Eval(Container, "DataItem.typename") %>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList id=dpltype runat="server" Width="100%" Height="100%" DataSource='<%# GetType(DataBinder.Eval(Container, "DataItem.laborguardname").ToString())%> ' DataTextField="typename" DataValueField="typeid">
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateColumn>
你说的两个DropDownList联动,是用AJAX联动的呢,还是用后台事件联动的?
如果是后者的话, 那在事件里在设置一下DataGrid就好了呀
既处于编辑状态的一行中有两列,分别是DropDownList1和DropDownList2
相当于这样:DropDownList1绑定省,DropDownList2绑定市
在DataGrid处于编辑状态时,选择DropDownList1中省的名称,DropDownList2中绑定相应的市 不知道我这样说大家明白不?? 大家最好有这方面的代码!!谢谢
{ if(e.Item.ItemType==ListItemType.EditItem)
{ if(e.Item.FindControl("dplbselect")!=null)
((DropDownList)e.Item.FindControl("dplbselect")).SelectedIndexChanged+=new System.EventHandler(this.Changed);
}
}
protected void Changed(object sender, System.EventArgs e)
{
System.Web.UI.WebControls.DataGridItem item=(DataGridItem)((Control)sender).Parent.Parent;//获取当前Item
string strstaffid=((Label)(item.FindControl("Lbstaffid"))).Text;
string lbguardname=((Label)(item.FindControl("lblaborname"))).Text;
string strselect=((DropDownList)(item.FindControl("dplbselect"))).SelectedItem.Text.ToString();
DropDownList listtype;
listtype=(DropDownList)item.FindControl("dpltype"); string sql="update orderstandardlabor set lbselectname='"+list.SelectedItem.Text.ToString()+"' where staffid="+intstaffid+"";
this.myclass.Command(sql);
Band();
}
但现在的问题是调试的时候 :
string strstaffid=((Label)(item.FindControl("Lbstaffid"))).Text;
string lbguardname=((Label)(item.FindControl("lblaborname"))).Text;strstaffid 和lbguardname 都为空 ,郁闷 而
string strselect=((DropDownList)(item.FindControl("dplbselect"))).SelectedItem.Text.ToString();
的 strselect 有值
郁闷 ,检查半天都没有检查出来,大家说说
runat="server" AutoGenerateColumns="False" Width="304px" BorderColor="#3366CC" BorderStyle="None"
BorderWidth="1px" BackColor="White" CellPadding="4">
<SelectedItemStyle Font-Bold="True" ForeColor="#CCFF99" BackColor="#009999"></SelectedItemStyle>
<ItemStyle ForeColor="#003399" BackColor="White"></ItemStyle>
<HeaderStyle Font-Bold="True" ForeColor="#CCCCFF" BackColor="#003399"></HeaderStyle>
<FooterStyle ForeColor="#003399" BackColor="#99CCCC"></FooterStyle>
<Columns>
<asp:TemplateColumn HeaderText="订单号">
<ItemTemplate>
<asp:Label id=lblOrderID runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.OrderID") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList id=drpOrder runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownListChange" DataSource="<%# BindTheOrderID() %>" DataTextField="OrderID">
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="商品号">
<ItemTemplate>
<asp:Label id=lblproductID runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.ProductID") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList id=drpProduct runat="server" DataSource="<%# BindTheProductID(orderID) %>" DataTextField="ProductID">
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:EditCommandColumn ButtonType="LinkButton" UpdateText="更新" CancelText="取消" EditText="编辑"></asp:EditCommandColumn>
</Columns>
<PagerStyle HorizontalAlign="Left" ForeColor="#003399" BackColor="#99CCCC" Mode="NumericPages"></PagerStyle>
</asp:DataGrid>
public string orderID = "";
protected string productID = "";
SqlConnection conn = new SqlConnection();
SqlCommand cmd = new SqlCommand();
private void Page_Load(object sender, System.EventArgs e)
{
//定义数据连接对象,其中数据库连接字符串是在Web.Config文件中定义的
conn = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionSqlServer"].ToString());
//页面初试化时进行数据绑定
if(!IsPostBack)
DataGridDataBind();
}
//进行数据绑定
private void DataGridDataBind()
{
//创建数据适配器对象
SqlDataAdapter da = new SqlDataAdapter("select top 10 Orders.OrderID,ProductID from Orders,[Order Details] where Orders.OrderID = [Order Details].OrderID order by Orders.OrderID",conn);
//创建DataSet对象
DataSet ds = new DataSet();
try
{
//填充数据集
da.Fill(ds,"testTable");
//进行数据绑定
dgDoubleDrp.DataSource = ds.Tables["testTable"];
dgDoubleDrp.DataBind();
}
catch(SqlException error)
{
//输出异常信息
Response.Write(error.ToString());
}
}
public SqlDataReader BindTheOrderID()
{
//定义命令对象
cmd =new SqlCommand("select OrderID from Orders order by OrderID", conn);
//打开数据库联结
conn.Open();
//返回DataReader对象
return cmd.ExecuteReader(CommandBehavior.CloseConnection);
}
public SqlDataReader BindTheProductID(string OrderID)
{
string strSql = ""; if(OrderID != "" && OrderID != null)
{
strSql = "select ProductID from [Order Details] where OrderID = '"+OrderID+"'";
}
else
{
strSql = "select distinct ProductID from [Order Details]";
}
//定义命令对象
cmd =new SqlCommand(strSql, conn);
//打开数据库联结
conn.Open();
//返回DataReader对象
return cmd.ExecuteReader(CommandBehavior.CloseConnection);
} #region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.dgDoubleDrp.CancelCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgDoubleDrp_CancelCommand);
this.dgDoubleDrp.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgDoubleDrp_EditCommand);
this.dgDoubleDrp.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.dgDoubleDrp_ItemDataBound);
this.Load += new System.EventHandler(this.Page_Load); }
#endregion private void dgDoubleDrp_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
//取得当前行的订单号和商品号
orderID = ((Label) e.Item.FindControl("lblOrderID")).Text.Trim();
productID = ((Label) e.Item.FindControl("lblproductID")).Text.Trim();
//设置DataGrid控件的当前编辑项的索引
dgDoubleDrp.EditItemIndex = e.Item.ItemIndex;
//重新进行数据绑定
DataGridDataBind();
} public void DropDownListChange(object sender, System.EventArgs e)
{
//建立drpOrder下拉列表控件的引用对象
DropDownList OrderDrp = (DropDownList) sender;
//取得选择的订单号
orderID = OrderDrp.SelectedItem.Text;
//重新进行数据绑定
DataGridDataBind();
} private void dgDoubleDrp_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
//取消编辑
dgDoubleDrp.EditItemIndex = -1;
DataGridDataBind();
} private void dgDoubleDrp_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
//判断是否是编辑项
if (e.Item.ItemType == ListItemType.EditItem)
{
//找到编辑项中的drpOrder下拉列表控件
DropDownList drpOrderID = (DropDownList) e.Item.FindControl("drpOrder");
//设置在上个操作中drpOrder下拉列表中选中的项
drpOrderID.SelectedIndex = drpOrderID.Items.IndexOf(drpOrderID.Items.FindByText(orderID)); //找到编辑项中的drpProduct下拉列表控件
DropDownList drpProductID = (DropDownList) e.Item.FindControl("drpProduct");
//设置在上个操作中drpProduct下拉列表中选中的项
drpProductID.SelectedIndex = drpProductID.Items.IndexOf(drpProductID.Items.FindByText(productID));
}
}
还要用到e.Item中的其它两列的值,能否指教为什么strstaffid 和lbguardname 都为空?
代码见上面:string strstaffid=((Label)(item.FindControl("Lbstaffid"))).Text;
string lbguardname=((Label)(item.FindControl("lblaborname"))).Text;strstaffid 和lbguardname 都为空 ,郁闷 而
string strselect=((DropDownList)(item.FindControl("dplbselect"))).SelectedItem.Text.ToString();
的 strselect 有值多谢!!!
谢谢大家支持