如何触发datagrid中的dropdownlist的SelectedIndexChanged事件,用VB怎么写代码?谢谢!

snowshaken 2009-01-06 02:01:49
datagrid中的编辑模板列中有个dropdownlist控件,并且已经绑定了数据,我想选择下拉框中某项数据时,能够触发SelectedIndexChanged事件,请问前台和后台的代码怎么写?
谢谢!
...全文
290 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
ReyZhang 2009-01-07
  • 打赏
  • 举报
回复
顶4楼
showlie 2009-01-07
  • 打赏
  • 举报
回复
简单的一个事例,数据库引用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>


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;
}
}
阿彪兄 2009-01-06
  • 打赏
  • 举报
回复
用到FindControl方法,同时要用到事件发生源(DropDownlist)sender
baobei7758 2009-01-06
  • 打赏
  • 举报
回复
如果 SelectedIndexChanged 只是做一个简单的查询

建议用ajax 这样避免 datagrid的再次绑定和页面的刷新~
baobei7758 2009-01-06
  • 打赏
  • 举报
回复
<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>


public void zqddl_SelectedIndexChanged(object sender, System.EventArgs e)
{
DropDownList ggddl =(DropDownList)this.datagrid.Items[datagrid.EditItemIndex].FindControl("zqddl");
//........
}
snowshaken 2009-01-06
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 showlie 的回复:]
在GridView的DataBound事件中初始化DropDownList的SelectedIndexChanged事件
[/Quote]

你说的是itemdatabound事件吗
具体怎么初始化?
谢谢了
xiaoxiang_lying 2009-01-06
  • 打赏
  • 举报
回复
这个很麻烦的.
UP
showlie 2009-01-06
  • 打赏
  • 举报
回复
在GridView的DataBound事件中初始化DropDownList的SelectedIndexChanged事件

62,074

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧