包含在DataGrid下的DropDownList如何声明事件?
前台是这样的,我想对DropDownList声明事件应该怎么写?
<ASP:DataGrid
id="MyDataGrid"
runat="server"
Width="800"
CellPadding=3
CellSpacing="0"
OnEditCommand="MyDataGrid_Edit"
OnCancelCommand="MyDataGrid_Cancel"
OnUpdateCommand="MyDataGrid_Update"
DataKeyField="SupplierID"
BorderColor="Tan"
ShowFooter="false"
Font-Name="Verdana"
Font-Size="8pt"
HeaderStyle-Font-Bold="True"
HeaderStyle-BackColor="Maroon"
AutoGenerateColumns="False"
HeaderStyle-ForeColor="Tan" >
<columns>
<asp:EditCommandColumn
EditText="Edit"
CancelText="Cancel"
UpdateText="Update"
ItemStyle-VerticalAlign="top" />
<asp:BoundColumn
HeaderText="Product ID"
ReadOnly="True"
DataField="SupplierID"
ItemStyle-Wrap="false"
ItemStyle-VerticalAlign="top" />
<asp:BoundColumn
HeaderText="Company Name"
ReadOnly="True"
DataField="CompanyName"
ItemStyle-Wrap="false"
ItemStyle-VerticalAlign="top" />
<asp:TemplateColumn HeaderText="Products" >
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "ProductCount") %> Products
<TABLE Width="100%" CellPadding="0" CellSpacing="0" Border="0">
<TR>
<TD Style="font-size:8" Width="20%">
<b>CHANGE -</b>
</TD>
<TD>
<asp:DropDownList
Runat="server"
Id="edit_Product"
DataSource='<%# GetProducts((int)DataBinder.Eval(Container.DataItem, "SupplierID")) %>'
DataTextField="ProductName"
DataValueField="ProductID"
Width="200" />
</TD>
</TR>
<TR>
<TD Style="font-size:8" Width="20%">
<b>TO - </b>
</TD>
<TD>
<asp:TextBox
id="NewProductName"
runat="Server"
Width="200" />
</TD>
</TR>
</TABLE>
</ItemTemplate>
<EditItemTemplate>
<TABLE Width="100%" CellPadding="0" CellSpacing="0" Border="0">
<TR>
<TD Style="font-size:8" Width="20%">
<b>CHANGE -</b>
</TD>
<TD>
<asp:DropDownList
Runat="server"
Id="edit_Product"
DataSource='<%# GetProducts((int)DataBinder.Eval(Container.DataItem, "SupplierID")) %>'
DataTextField="ProductName"
DataValueField="ProductID"
Width="200" />
</TD>
</TR>
<TR>
<TD Style="font-size:8" Width="20%">
<b>TO - </b>
</TD>
<TD>
<asp:TextBox
id="NewProductName"
runat="Server"
Width="200" />
</TD>
</TR>
</TABLE>
</EditItemTemplate>
</asp:TemplateColumn>
</columns>
</asp:DataGrid>
问题点数:100、回复次数:17Top
1 楼mapy(大鹏)回复于 2005-04-04 08:52:34 得分 0
自己顶一下Top
2 楼mapy(大鹏)回复于 2005-04-04 09:03:49 得分 0
帮忙呀Top
3 楼NetCode(灵光)回复于 2005-04-04 09:09:34 得分 5
直接在aspx文件里面控件申明的地方写,OnSelectedIndexChange = "函数名"Top
4 楼hchxxzx(NET?摸到一点门槛)回复于 2005-04-04 09:10:41 得分 5
你这个什么意思,没搞清楚,叫人如何帮忙?
在DataGrid下的下拉框是没有事件的。Top
5 楼NetCode(灵光)回复于 2005-04-04 09:10:46 得分 0
还要申明AutoPostBack = "true"
或者在DataGrid的属性对话框下面单击编辑模版,操作里面的控件Top
6 楼lovelxj(伊斯人,吾谁与归)回复于 2005-04-04 09:12:27 得分 5
看看能否在DATAGRID 中的 itemDataBound 的方法上增加 这个下拉框的代理Top
7 楼qzb0818(漂来漂去)回复于 2005-04-04 09:19:40 得分 5
直接在aspx文件里面控件申明的地方写,OnSelectedIndexChanged="DropDownList_SelectedIndexChanged"
然后在cs中
public void DropDownList_SelectedIndexChanged(object sender, System.EventArgs e)
{
...
}
注意一定要是public的
Top
8 楼mapy(大鹏)回复于 2005-04-04 09:51:19 得分 0
这个不行的,你把代码拿回去看看就知道了Top
9 楼zxjuana(宝宝粥)回复于 2005-04-04 09:54:53 得分 20
DataGrid设置ItemDataBound事件
private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if(e.Item.ItemType == ListItemType.Item ||e.Item.ItemType == ListItemType.AlternatingItem)
{
DropDownList ddl = (DropDownList)e.Item.FindControl("DropDownListID");
DataRowView drv =(DataRowView)e.Item.DataItem; //这行在数据源中对应的行
ListItem item = ddl.Items.FindByValue(drv["字段名"].ToString());
if(item != null)
item.Selected = true;
}
}Top
10 楼mapy(大鹏)回复于 2005-04-04 09:55:24 得分 0
我发的这段代码,意思是:
数据窗的某个编辑项是下拉列表,如何对这个下拉列表如何声明事件的问题。Top
11 楼mapy(大鹏)回复于 2005-04-04 09:58:28 得分 0
完整代码:
<%@ Page Language="C#" Debug="true" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>
<script language="c#" runat="server">
OleDbDataAdapter OleDbDa;
OleDbConnection SqlCon;
DataView dvProducts;
DataSet ds;
protected void Page_Load(Object sender, EventArgs e){
string path = Request.MapPath("20010724T0201.Mdb");
SqlCon = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;data source=" + path);
if (! IsPostBack){
BindGrid();
}
}
protected void BindGrid(){
OleDbDa = new OleDbDataAdapter("SELECT S.*, (SELECT COUNT(*) FROM Products AS P WHERE P.SupplierID = S.SupplierID) AS [ProductCount] FROM Suppliers AS S", SqlCon);
ds = new DataSet();
OleDbDa.Fill(ds, "Suppliers");
OleDbDa.SelectCommand = new OleDbCommand("SELECT ProductName, ProductID, SupplierID FROM Products", SqlCon);
OleDbDa.Fill(ds, "Products");
dvProducts = ds.Tables["Products"].DefaultView;
MyDataGrid.DataSource = ds.Tables["Suppliers"];
Page.DataBind();
}
protected DataView GetProducts(int SupplierID){
dvProducts.RowFilter = "SupplierID = " + SupplierID;
return dvProducts;
}
protected void MyDataGrid_Edit(Object sender, DataGridCommandEventArgs e) {
MyDataGrid.EditItemIndex = e.Item.ItemIndex;
BindGrid();
}
protected void MyDataGrid_Cancel(Object sender, DataGridCommandEventArgs e) {
MyDataGrid.EditItemIndex = -1;
BindGrid();
}
protected void MyDataGrid_Update(Object sender, DataGridCommandEventArgs e) {
string ChangeProductNameFrom = (string)((DropDownList)(e.Item.FindControl("edit_Product"))).SelectedItem.Text;
string ChangeProductNameTo = (string)((TextBox)(e.Item.FindControl("NewProductName"))).Text;
int ProductID = int.Parse((string)((DropDownList)(e.Item.FindControl("edit_Product"))).SelectedItem.Value);
OleDbCommand odbcommand = new OleDbCommand("UPDATE Products SET ProductName = @ProductName WHERE ProductID = @ProductID", SqlCon);
odbcommand.Parameters.Add(new OleDbParameter("@Productname", OleDbType.VarChar, 500));
odbcommand.Parameters["@ProductName"].Value = ChangeProductNameTo;
odbcommand.Parameters.Add(new OleDbParameter("@ProductID", OleDbType.Integer));
odbcommand.Parameters["@ProductID"].Value = ProductID;
if (ChangeProductNameTo.Length > 0) {
try {
SqlCon.Open();
odbcommand.ExecuteNonQuery();
SqlCon.Close();
Message.Text = ChangeProductNameFrom + " <B>HAS BEEN CHANGED TO</B> " + ChangeProductNameTo;
} catch (OleDbException OleDbEx) {
Message.Text = "An Exception Has Occured: " + OleDbEx.Message.ToString();
} finally {
MyDataGrid.EditItemIndex = -1;
BindGrid();
}
} else {
Message.Text = "You must specify a new product name or hit cancel!";
}
}
</script>
<html>
<body>
<form runat="server">
<center>
<asp:Label id="Message" MaintainState="false" ForeColor="#CC3300" Font-Size="11pt" runat="server"/><p>
<ASP:DataGrid
id="MyDataGrid"
runat="server"
Width="800"
CellPadding=3
CellSpacing="0"
OnEditCommand="MyDataGrid_Edit"
OnCancelCommand="MyDataGrid_Cancel"
OnUpdateCommand="MyDataGrid_Update"
DataKeyField="SupplierID"
BorderColor="Tan"
ShowFooter="false"
Font-Name="Verdana"
Font-Size="8pt"
HeaderStyle-Font-Bold="True"
HeaderStyle-BackColor="Maroon"
AutoGenerateColumns="False"
HeaderStyle-ForeColor="Tan" >
<columns>
<asp:EditCommandColumn
EditText="Edit"
CancelText="Cancel"
UpdateText="Update"
ItemStyle-VerticalAlign="top" />
<asp:BoundColumn
HeaderText="Product ID"
ReadOnly="True"
DataField="SupplierID"
ItemStyle-Wrap="false"
ItemStyle-VerticalAlign="top" />
<asp:BoundColumn
HeaderText="Company Name"
ReadOnly="True"
DataField="CompanyName"
ItemStyle-Wrap="false"
ItemStyle-VerticalAlign="top" />
<asp:TemplateColumn HeaderText="Products" >
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "ProductCount") %> Products
<TABLE Width="100%" CellPadding="0" CellSpacing="0" Border="0">
<TR>
<TD Style="font-size:8" Width="20%">
<b>CHANGE -</b>
</TD>
<TD>
<asp:DropDownList
Runat="server"
Id="edit_Product"
DataSource='<%# GetProducts((int)DataBinder.Eval(Container.DataItem, "SupplierID")) %>'
DataTextField="ProductName"
DataValueField="ProductID"
Width="200" />
</TD>
</TR>
<TR>
<TD Style="font-size:8" Width="20%">
<b>TO - </b>
</TD>
<TD>
<asp:TextBox
id="NewProductName"
runat="Server"
Width="200" />
</TD>
</TR>
</TABLE>
</ItemTemplate>
<EditItemTemplate>
<TABLE Width="100%" CellPadding="0" CellSpacing="0" Border="0">
<TR>
<TD Style="font-size:8" Width="20%">
<b>CHANGE -</b>
</TD>
<TD>
<asp:DropDownList
Runat="server"
Id="edit_Product"
DataSource='<%# GetProducts((int)DataBinder.Eval(Container.DataItem, "SupplierID")) %>'
DataTextField="ProductName"
DataValueField="ProductID"
Width="200" />
</TD>
</TR>
<TR>
<TD Style="font-size:8" Width="20%">
<b>TO - </b>
</TD>
<TD>
<asp:TextBox
id="NewProductName"
runat="Server"
Width="200" />
</TD>
</TR>
</TABLE>
</EditItemTemplate>
</asp:TemplateColumn>
</columns>
</asp:DataGrid>
</center>
</form>
</body>
</html>Top
12 楼mapy(大鹏)回复于 2005-04-04 10:00:56 得分 0
zxjuana(宝宝粥)发的不是给DropDownList声明事件。Top
13 楼wbj02(J^情之弦^J)回复于 2005-04-04 10:02:54 得分 30
在dataGrid的ItemCommand事件中,
设置DropDownList的CommandName属性(就跟设置ID名差不多,为了识别,如:Name1等),而且AutoPostBack设为true.
接着,在dataGrid的ItemCommand事件中,写
if (e.CommandName == "Name1") //等于DropDownList的CommandName名
{
..... //DropDownList选择改变时的代码
}
Top
14 楼wbj02(J^情之弦^J)回复于 2005-04-04 10:04:20 得分 0
不好意思,上面写错了一点,下面是正确的:
设置DropDownList的CommandName属性(就跟设置ID名差不多,为了识别,如:Name1等),而且AutoPostBack设为true.
接着,在dataGrid的ItemCommand事件中,写
if (e.CommandName == "Name1") //等于DropDownList的CommandName名
{
..... //DropDownList选择改变时的代码
}
Top
15 楼baobei7758(陵少)回复于 2005-04-04 10:04:22 得分 30
ItemDataBound中申明
DropDownList ddl = (DropDownList)e.Item.FindControl("DropDownListID") as DropDownList;
protected void DropDownListID_selectindex(object sender,EventArgs e)
{
}Top
16 楼zxjuana(宝宝粥)回复于 2005-04-04 10:07:05 得分 0
不好意思,我发的是绑定DataGrid里的Dropdownlist
wbj02(J^情之弦^J)是设置DataGrid里的Dropdownlist事件
你要先设置DropDownList的CommandName属性
Top
17 楼mapy(大鹏)回复于 2005-04-04 10:09:39 得分 0
正在试,好象有点道理,呵呵,先给分,不明白我再问!Top
相关问题
- 关于捕获datagrid的模板列中dropdownlist控件的事件(C#),如何声明事件?在线等待…………
- datagrid中dropdownlist的selectedchange事件问题
- dropdownlist事件
- 如何让DataGrid 的EditItemTemplate中的DropDownList控件触发事件?
- 如何让DataGrid 的EditItemTemplate中的DropDownList控件触发事件?
- 如何触发DataGrid中模板列的dropdownlist的selectedindexchanged事件?
- DropDownList的SelectedIndexChanged事件
- DataGrid中有dropdownlist控件,如何写这个控件的触发事件?
- 关于DataGrid内 帮定 DropDownList 的 OnSelectedIndexChanged事件(点击两次生效)
- DATAGRID中模板列中使用dropdownlist控件,当dropdownlist改变选项时触发的事件怎么写???




