ASP.Net Controls 用法大全
<form id="Form1" method="post" runat="server"> <asp:TextBox id="TextBox1" runat="server"></asp:TextBox> <asp:Button id="Button1" runat="server" Text="Button"></asp:Button></form>
private void Button1_Click(object sender, System.EventArgs e){ TextBox b = Page.FindControl("TextBox1") as TextBox; if(b != null) { Response.Write("Found TextBox1 on Button1_Click<br>"); } }
<form id="Form1" method="post" runat="server"> <asp:Panel id="Panel1" runat="server" Height="152px"> Panel <asp:TextBox id="TextBox1" runat="server"></asp:TextBox> <asp:Button id="Button1" runat="server" Text="Button"></asp:Button> </asp:Panel></form>
<div id="Panel1" style="height:152px;"> Panel <input name="TextBox1" type="text" id="TextBox1" /> <input type="submit" name="Button1" value="Button" id="Button1" /></div>
FindControl in a DataGrid
<form id="Form1" method="post" runat="server"> <asp:DataGrid id=DataGrid1 runat="server" DataSource="<%# employees1 %>" AutoGenerateColumns="False" OnSelectedIndexChanged="DataGrid1_SelectedIndexChanged" OnEditCommand="DataGrid1_EditCommand"> <Columns> <asp:BoundColumn DataField="emp_id" SortExpression="emp_id" HeaderText="emp_id"/> <asp:BoundColumn DataField="fname" SortExpression="fname" HeaderText="fname"/> <asp:BoundColumn DataField="lname" SortExpression="lname" HeaderText="lname"/> <asp:TemplateColumn> <ItemTemplate> <asp:TextBox Runat="server" ID="TextBox1" /> </ItemTemplate> </asp:TemplateColumn> <asp:ButtonColumn Text="Select" CommandName="Select"></asp:ButtonColumn> <asp:EditCommandColumn ButtonType="LinkButton" UpdateText="Update" CancelText="Cancel" EditText="Edit"> </asp:EditCommandColumn> </Columns> </asp:DataGrid></form>
<table cellspacing="0" rules="all" border="1" id="DataGrid1"> <tr> <td>emp_id</td><td>fname</td><td>lname</td><td> </td><td> </td><td> </td> </tr> <tr> <td>A-C71970F</td><td>Aria</td><td>Cruz</td><td> <input name="DataGrid1:_ctl2:TextBox1" type="text" id="DataGrid1__ctl2_TextBox1" /> </td><td> </tr> <tr> <td>A-R89858F</td><td>Annette</td><td>Roulet</td><td> <input name="DataGrid1:_ctl3:TextBox1" type="text" id="DataGrid1__ctl3_TextBox1" /> </td><td> </tr>
Control c = Page.FindControl(“TextBox1”)
private void DataGrid1_SelectedIndexChanged(object sender, System.EventArgs e){ TextBox b; b = DataGrid1.Items[DataGrid1.SelectedIndex].FindControl("TextBox1") as TextBox; if(b != null) { Response.Write("Sender = " + sender.GetType().ToString() + "<br>"); Response.Write("Found Textbox1 in SelectedIndexChanged event<br>"); Response.Write(FindUtil.DumpParent(b)); }}
public class FindUtil{ public static string DumpParents(Control c) { StringBuilder sb = new StringBuilder(); sb.Append(c.ID + " (" + c.GetType().ToString() + ")"); while(c.Parent != null) { c = c.Parent; sb.Append(" -><br>"); sb.Append(c.ID + " (" + c.GetType().ToString() + ")"); } return sb.ToString(); }}
protected void DataGrid1_EditCommand(object source, DataGridCommandEventArgs e){ TextBox b; b = e.Item.FindControl("TextBox1") as TextBox; if(b != null) { Response.Write("Found Textbox1 in EditCommand event<br>"); } }
Finding Controls In Headers and Footers
<asp:DataGrid id=DataGrid1 runat="server" DataSource="<%# employees1 %>" AutoGenerateColumns="False" ShowFooter="True"> <Columns> <asp:BoundColumn DataField="emp_id" SortExpression="emp_id" HeaderText="emp_id"/> <asp:BoundColumn DataField="fname" SortExpression="fname" HeaderText="fname"/> <asp:BoundColumn DataField="lname" SortExpression="lname" HeaderText="lname"/> <asp:TemplateColumn> <HeaderTemplate> <asp:DropDownList Runat="server" ID="DropDownList1"> <asp:ListItem Value="1">One</asp:ListItem> <asp:ListItem Value="2">Two</asp:ListItem> <asp:ListItem Value="3">Three</asp:ListItem> </asp:DropDownList> </HeaderTemplate> <ItemTemplate> <asp:TextBox Runat="server" ID="TextBox1" /> </ItemTemplate> <FooterTemplate> <asp:DropDownList Runat="server" ID="Dropdownlist1"> <asp:ListItem Value="1">One</asp:ListItem> <asp:ListItem Value="2">Two</asp:ListItem> <asp:ListItem Value="3">Three</asp:ListItem> </asp:DropDownList> </FooterTemplate> </asp:TemplateColumn> <asp:ButtonColumn Text="Select" CommandName="Select"></asp:ButtonColumn> </Columns></asp:DataGrid>
private void DataGrid1_SelectedIndexChanged(object sender, System.EventArgs e){ DropDownList d; d = DataGrid1.Controls[0].Controls[0].FindControl("DropDownList1") as DropDownList; if(d != null) { Response.Write("Found header DropDownList1 in SelectedIndexChanged event<br>"); } int footerIndex = DataGrid1.Controls[0].Controls.Count-1; d = DataGrid1.Controls[0].Controls[footerIndex].FindControl("DropDownList1") as DropDownList; if(d != null) { Response.Write("Found footer DropDownList1 in SelectedIndexChanged event<br>"); Response.Write(FindUtil.DumpParents(d)); }}
FindControl in Repeater controls
<asp:Repeater id="Repeater1" runat="server"> <ItemTemplate> <tr> <td><%#DataBinder.Eval(Container.DataItem, "au_id")%></td> <td><%#DataBinder.Eval(Container.DataItem, "au_lname")%></td> <td><%#DataBinder.Eval(Container.DataItem, "au_fname")%></td> <td><asp:TextBox Runat="server" ID="TextBox1" /></td> <td><asp:Button Runat="server" ID="Button1" OnClick="Button1_Click" Text="Click" /></td> </tr> </ItemTemplate></asp:Repeater>
protected void Button1_Click(object sender, System.EventArgs e){ Button btn = sender as Button; TextBox tb = btn.Parent.FindControl("TextBox1") as TextBox; if(tb != null) { Response.Write("Found TextBox1 in Button1_Click event<br>"); Response.Write(FindUtil.DumpParents(tb)); }}
private void Button2_Click(object sender, System.EventArgs e){ foreach(RepeaterItem item in Repeater1.Items) { TextBox b = item.FindControl("TextBox1") as TextBox; Response.Write(b.Text + "<br>"); }}
User Controls
<%@ Control Language="c#" AutoEventWireup="false" Codebehind="MyUserControl.ascx.cs" Inherits="aspnet.FindControls.MyUserControl" TargetSchema="http://schemas.microsoft.com/intellisense/ie5"%><asp:TextBox id="TextBox1" runat="server"></asp:TextBox><asp:Button id="Button1" runat="server" Text="Button"></asp:Button>
<input name="MyUserControl1:TextBox1" type="text" id="MyUserControl1_TextBox1" /> <input type="submit" name="MyUserControl1:Button1" value="Button" id="MyUserControl1_Button1" />
private void Button1_Click(object sender, System.EventArgs e){ TextBox b = this.FindControl("TextBox1") as TextBox; if(b != null) { Response.Write("Found TextBox1 on User Control Button1_Click<br>"); } }
private void Page_Load(object sender, System.EventArgs e){ MyUserControl u = FindControl("MyUserControl1") as MyUserControl; TextBox b = u.FindControl("TextBox1") as TextBox; if(b != null) { Response.Write("Found TextBox1 on Page_Load<br>"); } }
Conclusion
ASP.Net Controls 用法大全的更多相关文章
- MVC5 + EF6 + Bootstrap3 (9) HtmlHelper用法大全(下)
文章来源:Slark.NET-博客园 http://www.cnblogs.com/slark/p/mvc5-ef6-bs3-get-started-httphelper-part2.html 上一节 ...
- MVC5 + EF6 + Bootstrap3 (8) HtmlHelper用法大全(上)
文章来源:Slark.NET-博客园 http://www.cnblogs.com/slark/p/mvc5-ef6-bs3-get-started-httphelper-part1.html 上一节 ...
- asp.net 操作Excel大全
asp.net 操作Excel大全 转:http://www.cnblogs.com/zhangchenliang/archive/2011/07/21/2112430.html 我们在做excel资 ...
- MVC HtmlHelper用法大全
MVC HtmlHelper用法大全HtmlHelper用来在视图中呈现 HTML 控件.以下列表显示了当前可用的一些 HTML 帮助器. 本主题演示所列出的带有星号 (*) 的帮助器. ·Actio ...
- C# MessageBox 用法大全(转)
C# MessageBox 用法大全 http://www.cnblogs.com/Tammie/archive/2011/08/05/2128623.html 我们在程序中经常会用到MessageB ...
- MVC中HtmlHelper用法大全参考
MVC中HtmlHelper用法大全参考 解析MVC中HtmlHelper控件7个大类中各个控件的主要使用方法(1) 2012-02-27 16:25 HtmlHelper类在命令System.Web ...
- pandas用法大全
pandas用法大全 一.生成数据表 1.首先导入pandas库,一般都会用到numpy库,所以我们先导入备用: import numpy as np import pandas as pd12 2. ...
- 转帖: 一份超全超详细的 ADB 用法大全
增加一句 连接 网易mumu模拟器的方法 adb connect 127.0.0.1:7555 一份超全超详细的 ADB 用法大全 2016年08月28日 10:49:41 阅读数:35890 原文 ...
- python之pandas用法大全
python之pandas用法大全 更新时间:2018年03月13日 15:02:28 投稿:wdc 我要评论 本文讲解了python的pandas基本用法,大家可以参考下 一.生成数据表1.首先导入 ...
随机推荐
- 【NOIP2012】提高组初赛试题(个人错题解析+相关知识点扩展)C++版
初赛14号就要开始了,从今天到14号还有三天,已经请了两节的自习来刷题,每天三四套题,尽量把01年到16年的题目全刷一遍.[fighting!!!] 4.无论是TCP/IP模型还是OSI模型,都可以视 ...
- jQuery Mobile事件,开发全解+完美注释
全栈工程师开发手册 (作者:栾鹏) jQuery Mobile事件全解 jQuery Mobile 所有class选项 jQuery Mobile 所有data-*选项 jQuery Mobile事件 ...
- 吐槽CSDN--想钱想疯了--阅读全文需要关闭广告屏蔽
吐槽CSDN 想钱想疯了–阅读全文需要关闭广告屏蔽 近来csdn开始主推博客皮肤升级,说白了就是有一套新的盈利模式,具体怎么操作呢: 1. 采用信息流方式,博客内容变成类似朋友圈.微博.知乎那样的信息 ...
- string使用
一.list和string转化 List转字符串,用逗号隔开 List<string> list = new List<string>();list.Add("a&q ...
- win10 uwp 切换主题
本文主要说如何在UWP切换主题,并且如何制作主题. 一般我们的应用都要有多种颜色,一种是正常的白天颜色,一种是晚上的黑夜颜色,还需要一种辅助的高对比颜色.这是微软建议的,一般应用都要包含的颜色. 我们 ...
- python第五课——自定义线程池
内容概要: 1.low版线程池 2.绝版线程池 1.low版线程池 设计思路:运用队列queue 将线程类名放入队列中,执行一个就拿一个出来 import queue import threading ...
- [hihoCoder]无间道之并查集
题目大意: #1066 : 无间道之并查集 时间限制:20000ms 单点时限:1000ms 内存限制:256MB 描述 这天天气晴朗.阳光明媚.鸟语花香,空气中弥漫着春天的气息……额,说远了,总之, ...
- LeetCode 628. Maximum Product of Three Numbers (最大三数乘积)
Given an integer array, find three numbers whose product is maximum and output the maximum product. ...
- 【HDFS】相关概念及常用命令
本文用于记录Hadoop中HDFS相关概念以及常用命令,以便日后查询. 概念之间无先后顺序,只是用于记录. 参考的Hadoop的版本是2.7.3 1. 角色 hadoop fs [hdfs dfs] ...
- centos 安装giblab
本文章转载自:http://www.cnblogs.com/fanjingfeng/p/6665597.html 一, 服务器快速搭建gitlab方法 可以参考gitlab中文社区 的教程 cento ...