2012-03-13 19 views
0

我有包含TextBox和LinkBut​​ton的Repeater。當點擊LinkBut​​ton的,我需要搶TextBox.Text和做的東西......使用TextBox控件的事件處理程序OnItemCommand

使用事件Repeater1_ItemDataBound(對象發件人,RepeaterItemEventArgs E)我能夠獲得使用文本框TX = E文本框的值.Item.FindControl( 「txCode」)作爲文本框

然而

使用事件Repeater1_ItemCommand(對象發件人,RepeaterCommandEventArgs E)我沒有得到任何東西。 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> 

我能夠得到以下

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

的文本框的值,我沒能獲得低於

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。 – 2012-03-13 23:17:53

+0

if(!IsPostBack) - Damm < - working!謝謝@Tim Schmelter – 2012-03-13 23:21:56

回答

2

的文本框的值不要你Repeater結合在每次回傳時都是DataSource。否則ViewState無法正確重新加載,導致這樣的問題。

因此時常檢查在Page_Load中的IsPostBack property當啓用ViewStateEnableViewState=true):

if(!IsPostBack)BindRepeaterToDataSource();