私の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();
}
ご参考までに、コンテンツページでこれをすべて行っています
実際、私の要件は、プロジェクトにいくつかの CheckBoxes といくつかの Textboxes と 1 つの追加ボタンがあることです。いくつかの CheckBox をチェックしてから、選択したチェックボックスの値を表示する必要があるテキストボックスをクリックします。[追加] ボタンをクリックすると、選択した CheckBox の値がその TextBox に表示されます。Clicked テキストボックスの 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を使用していない場合)。
しかし、コンテンツ ページでこの同じコードを使用しようとすると、クリックされた TextBox のID が、TextBox の元の IDctl00_ChildPageContents_txtRasi3
ではなく、次のようにtxtRasi3
なります。
上記のコード ビハインド コードで述べたように、FindControl()
はクリックされたテキスト ボックスを見つけられませんでした。ID が異なるためctl00_ChildPageContents_txtRasi3
です。元の ID を取得するにはどうすればよいtxtRasi3
ですか?
もう1つ問題も見つかりました...
メソッドで直接 TexBox のコントロール ID を指定したにもかかわらず、まだ txtbox オブジェクトは null ですFindControl()
。