解决模板页使用的情况下,在内容页中使用 document.getElementById找不到控件的方法

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">

    <script type="text/javascript">
        function setValue()
        {
        alert("hello");
        var label= document.all("<%=la_UserName.ClientID%>")
            label.innerText="aa";      
        }
    </script>
       
        <div>
         
            <asp:TextBox ID="txtUser" runat="server" MaxLength="11" onblur="setValue()" ></asp:TextBox>
                    <asp:Label ID="la_UserName" runat="server" Width="188px" Font-Size="Smaller"></asp:Label></div>
    
</asp:Content>

还有一个就是内容也访问控件的问题,在后台的c#中也访问不倒了,需要使用findcontrol方法,而且要先找到模板页的contentpalceholder,然后找到contentpalceholder中的内容页的控件,例如:
protected string getUserName()
    
{
        ContentPlaceHolder mpContentPlaceHolder;
        TextBox mpTextBox =new TextBox(); 
        string username=string.Empty;
        using (SqlConnection conn = new SqlConnection("server=.;uid=virus;pwd=;database=DevTest"))
        {
            conn.Open();

            
            mpContentPlaceHolder =
              (ContentPlaceHolder)(Master.FindControl("ContentPlaceHolder1"));
            if (mpContentPlaceHolder != null)
            {
                mpTextBox =
                    (TextBox)(mpContentPlaceHolder.FindControl("txtUser"));
                if (mpTextBox != null)
                {
                    mpTextBox.Text = "1";
                
}

            }

            // Gets a reference to a Label control that not in 
            // a ContentPlaceHolder
            //Label mpLabel = (Label)Master.FindControl("masterPageLabel");
            //if (mpLabel != null)
            //
{
            //    Label1.Text = "Master page label = " + mpLabel.Text;
            //
}


            int id = Convert.ToInt32(mpTextBox.Text);
            
            //TextBox tb = this.Master.FindControl("ContentPlaceHolder1").FindControl("TextBox1") as TextBox;
            SqlCommand comm = new SqlCommand("select username from users where userid=" + id);
            comm.Connection = conn;
            username = comm.ExecuteScalar().ToString();
        }
        return username;
    }


posted on 2008-05-08 11:59 virus 阅读(345) 评论(2)  编辑 收藏 所属分类: javascriptASP.NET 2.0

评论

#1楼  2008-05-08 12:10 李战      

路过,学到了ClientID   回复  引用  查看    

#2楼  2008-05-08 14:00 wit      

楼上很谦虚哦   回复  引用  查看    

<2008年5月>
27282930123
45678910
11121314151617
18192021222324
25262728293031
1234567

导航

统计

公告

大家快乐就好,可不要因为工作累坏身体啊,身体是工作的本钱啊!
任何一个傻瓜都能写出计算机可以理解的程序,只有写出人类容易理解的程序才是优秀的程序员。
我的MSN:jorden008@hotmail.com

与我联系

搜索

 

常用链接

留言簿(9)

我参与的团队

我的标签

随笔分类(132)

随笔档案(149)

文章分类(27)

收藏夹(1)

.NET 2.0 Dispose

.NET 2.0 Socket

.NET WinForm控件开发

.NET 加密解密

.NET 序列化

.NET 自定义异常

.NET安装部署

.NET技巧

.NET开源项目

.NET学习资源

ACM题库

ActiveX插件

ASP.NET 2.0

ASP.NET 2.0 Cache

ASP.NET 2.0 GridView

ASP.NET 2.0的用户密码加密(注册登录)

ASP.NET 3.5

ASP.NET AJAX

ASP.NET LINQ

ASP.NET MVC

ASP.NET Repeater

ASP.NET ViewState

ASP.NET Webservice

c# 3.0

c# Dllimport c++

c# ORM

c# WndProc

c# 面向接口设计

c#+winform自动升级

c#操作数据库

c#插件编程

c#事件委托

Castle

eclipse社区

Enterprise Library

ExtJS

j2ee struts2

java se6帮助

java 相关

javascript

java社区

MSN机器人开发

myeclipse6+hibernate

MySql

NHibernate

ObjectDataSource

OR-Mapping

Petshop 4.0

SharpDevelop

SQL Server 2005

TDD

VMWare

web.config

WEB控件开发

windows service

Windows Workflow

WinForm DataGridView技巧

WMI编程

XML

常用正则表达式

创业信息

弹出窗口

飞信

分词算法

国内牛人

国外牛人

前端界面开发

权限系统设计思路

设计模式

数据字典

系统定时运行,计划任务

下载好地方

友情链接

专业SVN托管服务

最新随笔

积分与排名

最新评论