0

在我的 asp.net Web 应用程序中,我Texbox使用 JavaScript 获取 ID,如下所示

<script language="javascript" type="text/javascript">
function clearTextBox(textBoxID)
{
    document.getElementById('<% =RVTable.ClientID %>').value = textBoxID;
    alert(textBoxID.toString());
}

我将该 ID 存储在 HiddenField

 <asp:HiddenField ID="RVTable" runat="server" />

然后我TextBox在代码隐藏中使用以下代码检索 id

  TextBox txtbox = (TextBox)FindControl(RVTable.Value.ToString());
  if (txtbox != null)
  {
      if (txtbox.ID.ToString() == RVTable.Value.ToString())
          txtbox.Text = (string)CheckBoxString.ToString();
  }

供您参考,我在内容页面中执行所有这些操作

实际上我的要求是我的项目中有几个复选框和几个文本框以及一个添加按钮。我将检查一些 CheckBox,然后单击必须显示所选复选框值的文本框。现在,如果我单击“添加”按钮,所选 CheckBox 的值将显示在该 TextBox 中。我将单击的文本框的 ID 存储在隐藏字段中。

这是我的 pageLoad 代码,将 OnClick 属性添加到文本框

txtRasi1.Attributes["onclick"] = "clearTextBox(this.id)";
txtRasi2.Attributes["onclick"] = "clearTextBox(this.id)";
txtRasi3.Attributes["onclick"] = "clearTextBox(this.id)";
txtRasi4.Attributes["onclick"] = "clearTextBox(this.id)";
txtRasi5.Attributes["onclick"] = "clearTextBox(this.id)";
txtRasi6.Attributes["onclick"] = "clearTextBox(this.id)";
txtRasi7.Attributes["onclick"] = "clearTextBox(this.id)";
txtRasi8.Attributes["onclick"] = "clearTextBox(this.id)";
txtRasi9.Attributes["onclick"] = "clearTextBox(this.id)";
txtRasi10.Attributes["onclick"] = "clearTextBox(this.id)";
txtRasi11.Attributes["onclick"] = "clearTextBox(this.id)";
txtRasi12.Attributes["onclick"] = "clearTextBox(this.id)";

我已经通过在一个简单的 asp.net Web 应用程序中使用上述代码(不使用 MasterPage 时)实现了解决方案。

但是当我尝试在内容页面中使用相同的代码时,单击的文本框的 id 就像文本框的原始 ID一样ctl00_ChildPageContents_txtRasi3来代替。txtRasi3

正如我在上面的代码隐藏代码中提到的,FindControl()没有找到点击的文本框,因为它获得了不同的 ID ctl00_ChildPageContents_txtRasi3,. 我怎样才能得到原来的身份证txtRasi3

我还发现了一个问题...在此处输入图像描述

即使我在FindControl()方法中直接提到了我的 TexBox 的控件 ID,txtbox 对象仍然为空。

4

1 回答 1

1

将文本框客户端 ID 模式设置为静态......

 <asp:TextBox ID="TextBox1" runat="server" ClientIDMode="Static"></asp:TextBox>
于 2013-04-06T13:33:25.647 回答