日期:2014-05-18  浏览次数:20354 次

js当前上下文找不到控件
HTML code

<%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Weekly.aspx.cs" Inherits="Weekly" Title="Untitled Page" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<script language="javascript" type="text/javascript">
<!--
    function getClientId()
     {
      var paraId1 = '<%= TextBox1.ClientID %>';//注册控件
      return {Id1:paraId1};//生成访问器
    }
//-->
</script>
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True"
        AutoGenerateColumns="False" CellPadding="1" ForeColor="#333333" GridLines="None" ShowFooter="True" OnRowCommand="GridView1_RowCommand" OnRowDataBound="GridView1_RowDataBound" PageSize="5" OnPageIndexChanging="GridView1_PageIndexChanging" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating">
        <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" HorizontalAlign="Center" />
        <Columns>
            <asp:TemplateField HeaderText="序号" SortExpression="序号">
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("序号") %>'></asp:TextBox>
                </EditItemTemplate>                
                <ItemTemplate>
                    <asp:Label ID="Label2" runat="server" Text='<%# Bind("序号") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
</asp:GridView> 
</asp:Content>



总是在注册控件那提示“上下文找不到‘TextBox1’”,这是怎么回事呢?

------解决方案--------------------
TextBox1放在模板里面,会有很多个TextBox的,
你应该这样
function getClientId(x)
{
var paraId1 = x;//注册控件
return {Id1:paraId1};//生成访问器
}

确定要使用哪行当TextBox1
------解决方案--------------------
textbox在gridview里不能被直接的访问到滴,编译器找不到啊。
------解决方案--------------------
其实换一种思路。比如你后台数据中有ID这一列。然后前台

<asp:TextBox ID="txt_<%# Eval("ID")%>" runat="server" Text='<%# Bind("序号") %>'></asp:TextBox>
------解决方案--------------------
function getClientId()
{
var paraId1 =document.getElementById('<%= GridView1.ClientID %>').getElementsByTagName("input");//注册控件
return {Id1:paraId1};//生成访问器
}
 <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("序号") %>'></asp:TextBox>
生成的html代码是input
------解决方案--------------------
加了模板后你的控件ID就变成动态的了类似于CsdnUbbEditorStatusbar_CsdnUbb_1这样子了,所以你还去TextBox1肯定是取不到值的!