如何触发datagrid中的dropdownlist的SelectedIndexChanged事件,用VB怎么写代码?谢谢!
datagrid中的编辑模板列中有个dropdownlist控件,并且已经绑定了数据,我想选择下拉框中某项数据时,能够触发SelectedIndexChanged事件,请问前台和后台的代码怎么写?谢谢! --------------------编程问答-------------------- 在GridView的DataBound事件中初始化DropDownList的SelectedIndexChanged事件 --------------------编程问答-------------------- 这个很麻烦的.
UP --------------------编程问答--------------------
你说的是itemdatabound事件吗
具体怎么初始化?
谢谢了 --------------------编程问答-------------------- <asp:TemplateColumn HeaderText="发布周期">
<HeaderStyle Wrap="False"></HeaderStyle>
<ItemTemplate>
<asp:Label id=Label2 runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.发布周期") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList id="zqddl" runat="server" AutoPostBack="True" OnSelectedIndexChanged="zqddl_SelectedIndexChanged"></asp:DropDownList>
</EditItemTemplate>
</asp:TemplateColumn>
--------------------编程问答-------------------- 如果 SelectedIndexChanged 只是做一个简单的查询
public void zqddl_SelectedIndexChanged(object sender, System.EventArgs e)
{
DropDownList ggddl =(DropDownList)this.datagrid.Items[datagrid.EditItemIndex].FindControl("zqddl");
//........
}
建议用ajax 这样避免 datagrid的再次绑定和页面的刷新~ --------------------编程问答-------------------- 用到FindControl方法,同时要用到事件发生源(DropDownlist)sender --------------------编程问答-------------------- 简单的一个事例,数据库引用northwind,主要功能是在gridview_category表中有dropdownlist模板列,其中表示产品的分类。第二个表是dropdownlist选中的分类的产品清单。具体代码如下,已通过测试,可安全使用,无毒无副作用:
<%@ Page Language="C#" AutoEventWireup="true" 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:GridView ID="GridView_category" AutoGenerateColumns="false" Width="200" runat="server"
OnRowDataBound="GridView_category_RowDataBound">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:DropDownList ID="DropDownList_category" Width="150" AutoPostBack="true" OnSelectedIndexChanged="DropDownList_category_SelectedIndexChanged" runat="server">
</asp:DropDownList></ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:GridView ID="GridView_product" AutoGenerateColumns="false" Width="200" runat="server">
<Columns>
<asp:BoundField HeaderText="产品编号" DataField="ProductId" />
<asp:BoundField HeaderText="产品名称" DataField="ProductName" />
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
--------------------编程问答-------------------- 顶4楼
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 System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
this.GridView_category.DataSource = GetCategory();
this.GridView_category.DataBind();
}
}
protected void GridView_category_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList DropDownList_category = e.Row.Cells[0].FindControl("DropDownList_category") as DropDownList;
if (DropDownList_category != null)
{
DropDownList_category.DataValueField = "CategoryId";
DropDownList_category.DataTextField = "CategoryName";
DropDownList_category.DataSource = GetCategory();
DropDownList_category.DataBind();
}
}
}
protected void DropDownList_category_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList DropDownList_category = sender as DropDownList;
string categoryId = DropDownList_category.SelectedValue;
if (!string.IsNullOrEmpty(categoryId))
{
this.GridView_product.DataSource = GetProduct(categoryId);
this.GridView_product.DataBind();
}
}
private DataTable GetCategory()
{
string strConnection = "Data Source=(local);Initial Catalog=Northwind;Persist Security Info=True;User ID=sa";
string strSelect = "select CategoryId,CategoryName from Categories";
SqlDataAdapter da = new SqlDataAdapter(strSelect, strConnection);
DataTable table = new DataTable();
try
{
da.Fill(table);
}
catch (Exception e)
{
throw new Exception(e.Message);
}
return table;
}
private DataTable GetProduct(string categoryId)
{
string strConnection = "Data Source=(local);Initial Catalog=Northwind;Persist Security Info=True;User ID=sa";
string strSelect = "select ProductId,ProductName from Products where CategoryId="+categoryId;
SqlDataAdapter da = new SqlDataAdapter(strSelect, strConnection);
DataTable table = new DataTable();
try
{
da.Fill(table);
}
catch (Exception e)
{
throw new Exception(e.Message);
}
return table;
}
}
补充:.NET技术 , ASP.NET