读书人

母板页中FindControl的有关问题.

发布时间: 2012-01-06 22:55:18 作者: rapoo

母板页中FindControl的问题............
页面:
<%@ 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,找不到控件...........

[解决办法]
LinkButton myctrl = GridView1.FindControl( "bt_Priview ");
试试这个,要用包着他的控件FindControl

[解决办法]
LinkButton l = (LinkButton)Master.FindControl( "bt_Priview ");
这样应该没有问题.

建议给MASTER加个属性.
在母版页代码加上:
public LinkButton Bt_preview
{get{return bt_Priview;}}
在要使用的页面调用MASTER 的属性
Master.Bt_preview就行了.
[解决办法]
你找的控件是不是在ContentPlaceHolder中啊
试试
Master.FindControl( "ContentPlaceHolderID ").FindControl( "GridView1 ")....;
[解决办法]
要使用的页面还在加上

<%@ MasterType TypeName= "MasterPage " %>
指令, 抱歉啊, 忘了.
[解决办法]
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 = "查看 ";

}
}
[解决办法]
LinkButton lbtn = (LinkButton)e.Row.Cells[单元格索引].FindControl( "btn_Priview ");


lbtn.ToolTip = "查看 ";

[解决办法]
GridView gv = (GridView)Master.FindControl( "GridView1 ");
DropDownList ddl = (DropDownList)gv.Rows[0].Cells[2].FindControl( "DropDownList2 ");
[解决办法]
GridView1.FindControl( "bt_Priview ");
这句应该实现不了楼主想要的结果.

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

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


[解决办法]
mark

读书人网 >asp.net

热点推荐