zoukankan      html  css  js  c++  java
  • 自定义控件传值

            我们知道,如果传一个不固定的值给自定义控件是比较困难的,以下方法希望能大家有所帮助。思路很简单,就是定义一个公共方法,然后在使用它的页面调用它。现在页面上有两个下拉框,drp1,drp2
     1//UserControl1.aspx
     2<%@ Control Language="c#" AutoEventWireup="false" Codebehind="UserControl1.ascx.cs" Inherits="Test.UserControl1.ascx.cs" TargetSchema="http://schemas.microsoft.com/intellisense/ie5"%>
     3<asp:DropDownList id="drp1" AutoPostBack="True" runat="server"></asp:DropDownList>
     4<asp:DropDownList id="drp2" AutoPostBack="True" runat="server"></asp:DropDownList>
     5
     6//UserControl1.ascx.cs
     7using System;
     8using System.Collections;
     9using System.Data;
    10using System.Web.UI;
    11using System.Web.UI.WebControls;
    12
    13using namespace Test
    14public class UserContorl1:UserControl
    15{
    16protected DropDownList drp1;
    17protected DropDownList drp2;
    18
    19public void OnInit(int num1,int num2)
    20{
    21  BindDrp1(num1);
    22  BindDrp2(num2);
    23}

    24//绑定drp1
    25private void BindDrp1(int num)
    26{
    27    ArrayList al=new ArrayList();
    28    ListItem lst;
    29    for(int i=1;i<=num;i++)
    30    {
    31        lst=new ListItem(""+i.ToString()+"",i.ToString());
    32        al.Add(lst);
    33    }

    34    drp1.DataSource=al; 
               drp1.DataTextField
    ="Text";
              drp1.DataValueField
    ="Value";
    35    drp1.DataBind(); 
               drp1.SelectedValue
    =num.ToString();
    36}

    37//绑定drp2
    38private void BindDrp2(int num)
    39{
    40    ArrayList al=new ArrayList();
    41    ListItem lst;
    42    for(int i=1;i<=num;i++)
    43    {
    44       lst=new ListItem(""+i.ToString()+"",i.ToString());
    45       al.Add(lst);
    46    }

    47    drp2.DataSource=al;          
              drp2.DataTextField
    ="Text";    
              drp2.DataValueField
    ="Value";
    48   drp2.DataBind(); 
              drp2.SelectedValue
    =num.ToString();
    49  }

    50 }

    51}
    52

    前面很简单,现在看我们在页面中怎样子调用前面的这个自定义控件了。
    <%@ Page language="c#" Codebehind="WebForm2.aspx.cs" AutoEventWireup="false" Inherits="WebForm2" %>
    <%@ Register TagPrefix="uc1" TagName="UserControl11" Src="UserControl1.ascx" %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
    <HTML>
        
    <HEAD>
            
    <title>WebForm2</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">                
                
    <uc1:UserControl1 id="UserControl11" runat="server"></uc1:UserControl1>        
            
    </form>
        
    </body>
    </HTML>

    //部分后台代码
    public class WebForm2 : Page
    protected UserControl1 UserControl11;
    //这句是重点,默认我们在添加的时候是不会添加的,
    //要自己手动来添加,而且变量名要跟前面页面中一样
    private void Page_Load(object sender, System.EventArgs e)
            
    {
                
    // 在此处放置用户代码以初始化页面

                
    if(!Page.IsPostBack)
                
    {
                UserControl11.OnInit(
    1020);
    }

    //这样就可以给他传值了
    //下面还可以获得自定义控件上的控件
                DropDownList drp1=UserControl11.FindControl("drp1"as DropDownList;
  • 相关阅读:
    Google 商店:您的应用静态链接到的 OpenSSL 版本有多个安全漏洞。建议您尽快更新 OpenSSL
    android控件库(2)-仿Google Camera 的对焦效果
    新浪微博客户端(54)-代码重构-将宏定义修改为常量
    android自定义控件(1)-自定义控件属性
    新浪微博客户端(53)-记录用户最近点击表情
    android-解决EditText的inputType为Password时, 字体不一致的问题
    新浪微博客户端(52)-长按或滑动显示表情
    iOS- Terminating app due to uncaught exception 'NSRangeException'
    新浪微博客户端(51)-将表情图片转换为文本
    新浪微博客户端(50)-解决输入Emotion表情逐渐变小的问题
  • 原文地址:https://www.cnblogs.com/Oceanchip/p/172736.html
Copyright © 2011-2022 走看看