母板页中FindControl的问题............

vsstudio05 2007-09-26 12:41:31
页面:
<%@ Page AutoEventWireup="true" CodeFile="WEB_Case02b.aspx.cs" Inherits="Case_WEB_Case02b"
Language="C#" MasterPageFile="~/MasterPages/Default.master" %>

........

<asp:Content ID="Content3" ContentPlaceHolderID="Contentbar" runat="Server">
<asp:GridView ID="GridView1" ...>
<Columns>
<asp:TemplateField HeaderText="操作">
<ItemTemplate>
<asp:LinkButton ID="bt_Priview" runat="server" CausesValidation="False" Text="查看"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</asp:Content>


在后台cs代码中想控制bt_Priview的一些属性,现在要先FindControl这个控件,如何做?
尝试过
LinkButton myctrl = Master.FindControl("bt_Priview");
Control myctrl = FindControl("bt_Priview");
Control myctrl = Master.FindControl("bt_Priview");
此时myctrl里面都是null,找不到控件...........
...全文
198 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
随智阔 2012-06-15
  • 打赏
  • 举报
回复
正解
[Quote=引用 5 楼 的回复:]

你找的控件是不是在ContentPlaceHolder中啊
试试
Master.FindControl( "ContentPlaceHolderID ").FindControl( "GridView1 ")....;
[/Quote]
vsstudio05 2007-09-26
  • 打赏
  • 举报
回复
LinkButton myctrl = GridView1.FindControl("bt_Priview");
试试这个,要用包着他的控件FindControl
======================================================================
不行,语法错误!另外,因为GridView是包含在母板页里面的,我觉得因该先找到GridView1,然后在从GridView1里面找bt_Priview,但是同样的GridView1这个也没找到...........
menglingjun 2007-09-26
  • 打赏
  • 举报
回复
LinkButton myctrl = GridView1.FindControl("bt_Priview");
试试这个,要用包着他的控件FindControl
cheumen 2007-09-26
  • 打赏
  • 举报
回复
mark
include_me 2007-09-26
  • 打赏
  • 举报
回复
GridView1.FindControl("bt_Priview");
这句应该实现不了楼主想要的结果.

想想 GridView 是个多行绑定控件,
在数据绑定时应该会为每一行创建个 bt_Priview 实例.
GridView1.FindControl("bt_Priview")
这样查找系统没法区分你要的是哪一个..

可以试试在行上查找.
LinkButton lb = GridView1.Rows[0].FindControl("bt_Priview") as LinkButton;

kyouken2007 2007-09-26
  • 打赏
  • 举报
回复
GridView gv = (GridView)Master.FindControl("GridView1");
DropDownList ddl = (DropDownList)gv.Rows[0].Cells[2].FindControl("DropDownList2");
longjia 2007-09-26
  • 打赏
  • 举报
回复
LinkButton lbtn = (LinkButton)e.Row.Cells[单元格索引].FindControl("btn_Priview");
lbtn.ToolTip = "查看";
longjia 2007-09-26
  • 打赏
  • 举报
回复
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
LinkButton lbtn = (lbtn)e.Row.Cells[单元格索引].FindControl("btn_Priview");
lbtn.ToolTip = "查看";

}
}
LikeCode 2007-09-26
  • 打赏
  • 举报
回复
要使用的页面还在加上

<%@ MasterType TypeName="MasterPage" %>
指令, 抱歉啊, 忘了.
tds__609 2007-09-26
  • 打赏
  • 举报
回复
你找的控件是不是在ContentPlaceHolder中啊
试试
Master.FindControl("ContentPlaceHolderID").FindControl("GridView1")....;
LikeCode 2007-09-26
  • 打赏
  • 举报
回复
LinkButton l = (LinkButton)Master.FindControl("bt_Priview");
这样应该没有问题.

建议给MASTER加个属性.
在母版页代码加上:
public LinkButton Bt_preview
{get{return bt_Priview;}}
在要使用的页面调用MASTER 的属性
Master.Bt_preview就行了.
aa2223 2007-09-26
  • 打赏
  • 举报
回复
找个这个类,在把他转成找到的对象,装箱就可以了!

62,046

社区成员

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

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

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

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