请教:2个用户控件之间,如何实现信息、数据传递?

zrq827 2006-06-23 03:20:34
在工作中遇到个问题,特此请教大家,请各位不吝赐教!!
我在页面中用到2个用户控件,称之A、B
A中有个DropDownList,B中有个DataGrid
当A中的DropDownList的选择改变后,自动刷新B中DataGrid

在线等,盼复!!谢谢大家...
...全文
263 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
nanxi0532 2006-06-23
  • 打赏
  • 举报
回复
来参观的
IndexChanged
事件编写就可以
里面用
DataGrid dg=b.findcontrol("dg");
................
................
................

j__jake 2006-06-23
  • 打赏
  • 举报
回复
你可以给用户控件加属性啊
llainn 2006-06-23
  • 打赏
  • 举报
回复
孟子那钻石漂亮……
dgrwang 2006-06-23
  • 打赏
  • 举报
回复
DropDownList创建SelectedIndexChanged事件
DataGrid应该是根据DropDownList中的值改变其内容吧
将DataGrid的绑定内容写成函数,如ofBind,在SelectedIndexChanged加入如ofBind(DropDownList1.SelectedIndex)
GSXiaoXiao 2006-06-23
  • 打赏
  • 举报
回复
也可以在A的AutoPostBack="true"后,在IndexChanged事件里进行操作

------------------------------------------

不是讲得很清楚吗?
在Dropdownlist控件的SelectedIndexChanged事件里加入更新DataGrid代码就可以了.
WeekZero 2006-06-23
  • 打赏
  • 举报
回复
test01.aspx代码:

<%@ Page language="c#" Codebehind="test01.aspx.cs" AutoEventWireup="false" Inherits="localhost.test01" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>test01</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:DropDownList id="DropDownList1" runat="server" AutoPostBack="True">
<asp:ListItem Value="1">1</asp:ListItem>
<asp:ListItem Value="2">2</asp:ListItem>
<asp:ListItem Value="3">3</asp:ListItem>
<asp:ListItem Value="4">4</asp:ListItem>
<asp:ListItem Value="5">5</asp:ListItem>
</asp:DropDownList>
<asp:DataGrid id="DataGrid1" runat="server"></asp:DataGrid>
</form>
</body>
</HTML>



test01.aspx.cs代码:

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Configuration;

namespace localhost
{
/// <summary>
/// test01 的摘要说明。
/// </summary>
public class test01 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DropDownList DropDownList1;
protected System.Web.UI.WebControls.DataGrid DataGrid1;

private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
}

#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.DropDownList1.SelectedIndexChanged += new System.EventHandler(this.DropDownList1_SelectedIndexChanged);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void DropDownList1_SelectedIndexChanged(object sender, System.EventArgs e)
{
string selectvalue = DropDownList1.SelectedValue;

GridBind(selectvalue);
}

private void GridBind(string selectvalue)
{
string SqlStr;
DataSet ds=new DataSet();
SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"].ToString());

SqlStr = "select * from test where pid="+selectvalue;
conn.Open();
SqlDataAdapter da = new SqlDataAdapter(SqlStr,conn);
da.Fill(ds);
conn.Close();

DataGrid1.DataSource = ds;
DataGrid1.DataBind();
}
}
}


直接将代码复制过去就可以运行
WeekZero 2006-06-23
  • 打赏
  • 举报
回复
==,写一个
疯狂秀才2018 2006-06-23
  • 打赏
  • 举报
回复
using B控件的命名空间

在A控件中的CHANAGE事件中加入

B b=new B();
DataGrid dg=b.findcontrol("Datagrid1");

dg.datasoruce=xxxxxx;
dg.DataBind();


zrq827 2006-06-23
  • 打赏
  • 举报
回复
也许大家对于低级的问题,不屑于细说
但对于我这样一个入门级人员,还是希望大家能说详细点,如果能给出简单代码更好.
谢谢
gaoranaa 2006-06-23
  • 打赏
  • 举报
回复
关注一下
怎么现在网友都喜欢说些废话呢?没一个人拿出具体解决方案,照这样下去,以后就直接回答"用C#就能解决你的问题"好了
WeekZero 2006-06-23
  • 打赏
  • 举报
回复
up
孟子E章 2006-06-23
  • 打赏
  • 举报
回复
也可以用Ajax,也很方便。
孟子E章 2006-06-23
  • 打赏
  • 举报
回复
也可以在A的AutoPostBack="true"后,在IndexChanged事件里进行操作
limingguang7297 2006-06-23
  • 打赏
  • 举报
回复
你用控件的事件就可以实现的啊
孟子E章 2006-06-23
  • 打赏
  • 举报
回复
你可以用asp.net 2.0 的 Webpart功能。

62,039

社区成员

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

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

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

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