2012-03-13 18 views
0

TextBoxとLinkBut​​tonを含むRepeaterがあります。 LinkBut​​tonコントロールがクリックされたとき、私はTextBox.Textを取得し、ものを行う必要がある...イベントハンドラTextBoxコントロールを持つOnItemCommand

EVENT Repeater1_ItemDataBound(オブジェクト送信者、RepeaterItemEventArgs E)を使用して、私は= EをテキストボックスTXを使用してテキストボックスの値を取得することができていますEVENT Repeater1_ItemCommand(オブジェクト送信者、RepeaterCommandEventArgs e)を使用してテキストボックス

として.Item.FindControl( "txCode")しかし

私は戻って何かを得ていないのです。 TextBoxは空です。

「OnItemCommand」を使用してTextBoxからテキスト/コンテンツを取得するにはどうすればよいですか?

<asp:Repeater ID="Repeater1" runat="server" onitemdatabound="Repeater1_ItemDataBound" OnItemCommand="Repeater1_ItemCommand"> 
    <ItemTemplate> 
     <li>      
     <asp:TextBox ID="txCode" runat="server"></asp:TextBox> 
     <asp:LinkButton CommandName="verifyCode" ID="lbCode" runat="server">Submit<asp:LinkButton> 
     </li> 
    </ItemTemplate> 
</asp:Repeater> 

私は私があなたのRepeaterをバインドしないでください

protected void Repeater1_ItemCommand(object sender, RepeaterCommandEventArgs e) 
{ 
    if (e.CommandName == "verifyCode") 
    { 
     TextBox tx = e.Item.FindControl("txCode") as TextBox; 
     string myText = tx.Text; '<--- NOT working 
} 
+2

は、このイベントが発生したかどうかを確認するためにデバッグがありますか?おそらく、あなたはPage_Loadの '!IsPostBack'をチェックしていないでしょう。ポストバックではないリピータをDataSourceにバインドします。 –

+0

if(!IsPostBack) - Damm < - worked!おかげで@ティムSchmelter –

答えて

2

以下のTextBoxの値を取得することはできませんよ

protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e) 
{ 
    TextBox tx = e.Item.FindControl("txCode") as TextBox; 
    string myText = tx.Text; '<--- working 
} 

以下のTextBoxの値を取得することができていますすべてのポストバックにはDataSourceです。それ以外の場合、ViewStateはこのような問題を引き起こす原因を正確に再ロードすることはできません。

だから、常にViewStateが有効になっているのPage_Load(EnableViewState=true)でIsPostBack propertyをチェックしてください。

if(!IsPostBack)BindRepeaterToDataSource();