注册 登录
编程论坛 ASP.NET技术论坛

救助:aspx页面A怎么执行ascx用户控件B中的方法

coolsys 发布于 2010-03-05 14:22, 1416 次点击
aspx页面A:

  这里怎么执行ascx用户控件B中的BindGH(gh)以便绑定用户控件中的GridView1?

ascx用户控件B:

void BindGH(string Gong)
    {
        string sqlstr = "Select * From LHB_USER_DATA where LHB_Group=4 and LHB_GongHao='" + Gong + "'";
        SqlConnection sqlcon = new SqlConnection(connStr);
        SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon);
        DataSet myds = new DataSet();
        sqlcon.Open();
        myda.Fill(myds);
        DataView view = myds.Tables[0].DefaultView;
        GridView1.DataSource = view;
        GridView1.DataBind();
        sqlcon.Close();
    }
3 回复
#2
coolsys2010-03-05 14:32
有高手帮忙回答一下吗?
#3
happy8862010-03-05 14:57
我也在学习中! 你先参考一下  

要看你的控件是在那里注册,然后方法什么方法,以及在那里调用。   
   
   
  1,你在aspx中注册了控件,那么在aspx中可以直接   用你的       注册的id.方法名     调用。   
                                                    如果在.cs中调用,那就和aspx没有关系了,必须重新实例化这个控件   
   
  2,如果在.cs中动态注册的,那末在cs中直接可以用,如果声明成   public的,则在aspx中也可以调用   
   
  3,如果方法是静态的,就不用注册和实例化都可以。   


我们都先试一下 !
#4
happy8862010-03-05 16:21
  仅供参考


[Default。aspx]

<%@ Register src="messageBox.ascx" tagname="messageBox" tagprefix="uc1" %>

。。。。。。。

<uc1:messageBox ID="myControl" runat ="server" />



[Default。aspx。cs]

 protected void Button4_Click(object sender, EventArgs e)
    {
        myControl.messageLook1("从本页面调用  用户控件中的的方法");
    }



[messageBox.ascx]

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="messageBox.ascx.cs" Inherits="messageBox" %>

<asp:Button ID="Button1" runat="server" Text="用户控件中的一个按钮"  onclick="Button1_Click" />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>




[messageBox.ascx.cs]

 protected void Button1_Click(object sender, EventArgs e)
    {
        messageLook("从用户控件调用  用户控件中的的方法");
    }
    public void messageLook(string str)
    {  //本页面传值
        Label1.Text = "";
        Label1.Text=str;
    }
    public void messageLook1(string str)
    {
       //从另一个页面传值
        Label1.Text = "";
        Label1.Text = str;
    }
1