当前位置:编程学习 > C#/ASP.NET >>

再开一贴。求助关于使用document.getElementById的使用

.net 4.0,介绍说可以绑定使Control.ClientID=Control.ID。


protected void Page_Load(object sender, EventArgs e)
        {
            var t = new TextBox();
            var i = new Image();
            i.ClientIDMode = ClientIDMode.Static;
            i.ID="q123k-qweid-123er-12345";
            i.Visible = false;
            Panel1.Controls.Add(t);
            Panel1.Controls.Add(i);
            t.Attributes.Add("onfocus", "document.getElementById('q123k-qweid-123er-12345').style.display=''");
        }


报错object不存在。求教! --------------------编程问答-------------------- 查看生成出来的HTML源码 生成出来到底是嘛东西 --------------------编程问答--------------------

 
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head><title>
Home Page
</title><link href="Styles/Site.css" rel="stylesheet" type="text/css" />
</head>
<body>
    <form method="post" action="default.aspx" id="ctl01">
<div class="aspNetHidden">
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUJNjU1NDk0ODUzD2QWAmYPZBYCAgMPZBYCAgUPZBYCAgEPZBYCAgEPD2QWAh4Hb25mb2N1cwVDZG9jdW1lbnQuZ2V0RWxlbWVudEJ5SWQoJ3ExMjNrLXF3ZWlkLTEyM2VyLTEyMzQ1Jykuc3R5bGUuZGlzcGxheT0nJ2RkeMJ1rbAhnIJcvXFgodI5rmX/6CfFw3Zlp/Um4vlzZyQ=" />
</div>
 
 
<script src="/WebResource.axd?d=-uYyq2UItxQeWmcqa1mrseT_4x_4Dgu2dsqDOT7V4rV-BVxQtredj3ycfAlxKlBYDZzTbm4DoR_uDcZ9cCKObLGRd_SKyJOx_Jpiovj0X4E1&t=634671481629598994" type="text/javascript"></script>
<div class="aspNetHidden">
 
<input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="/wEWAgLP/6WqDgLB7/uoC7J9tFSWDBXLBh5Mon1WPLdawhXPT31RHsb3qFAge7rq" />
</div>
    <div class="page">
        <div class="header">
            <div class="title">
                <h1>
                    My ASP.NET Application
                </h1>
            </div>
            <div class="loginDisplay">
                
                        [ <a href="Account/Login.aspx" id="HeadLoginView_HeadLoginStatus">Log In</a> ]
                    
            </div>
            <div class="clear hideSkiplink">
                <a href="#NavigationMenu_SkipLink"><img alt="Skip Navigation Links" src="/WebResource.axd?d=n8YKnGYR_vMyj3p8qvrtVO7V0x0q2eN11OAj6WtpqJWVNxo_4L4a1WvAWx-HriSMV9B4bWu9rBYergtUx--oR8CNgQTTfo5JdX3tV6w6kDE1&t=634671481629598994" width="0" height="0" style="border-width:0px;" /></a><div class="menu" id="NavigationMenu">
<ul class="level1">
<li><a class="level1" href="Default.aspx">Home</a></li><li><a class="level1" href="About.aspx">About</a></li>
</ul>
</div><a id="NavigationMenu_SkipLink"></a>
            </div>
        </div>
        <div class="main">
            
    <div id="MainContent_Panel1">

    <input name="ctl00$MainContent$ctl00" type="text" onfocus="document.getElementById('q123k-qweid-123er-12345').style.display=''" />
</div>
 
        </div>
        <div class="clear">
        </div>
    </div>
    <div class="footer">
        
    </div>
    
<script type='text/javascript'>new Sys.WebForms.Menu({ element: 'NavigationMenu', disappearAfter: 500, orientation: 'horizontal', tabIndex: 0, disabled: false });</script></form>
</body>
</html>


--------------------编程问答-------------------- 你完全可以在前台加脚本事件,而没必要再CS里纠结

另外你还可以使用如下方式
this.RegisterClientScriptBlock("awoke","<script>alert('hello');</script>");
--------------------编程问答-------------------- 因为我需要做成一个custom control.能指点一下吗? --------------------编程问答-------------------- onfocus="document.getElementById('q123k-qweid-123er-12345').style.display=''" />

生成出来的ID让转换了
改成
--------------------编程问答-------------------- onfocus="document.getElementById('q123k-qweid-123er-12345').style.display=''" />
'在后台让转译了
补充:.NET技术 ,  ASP.NET
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,