项目模板所需的中继器数据

时间:2012-07-22 15:43:28

标签: asp.net

<asp:Repeater ID="rpChat" runat="server" OnItemDataBound="rpChat_ItemDataBound" OnItemCommand="rpChat_ItemCommand">
    <ItemTemplate>
        <div id="divChatWindow" title='<%# Eval("Username2") %>' runat="server" class="clChatWindow">
            <div>
            <img src="../../Menu/close.jpg" onclick="HideDiv(this)" style="float: right; width: 20px;
                    height: 20px;" /></div>
            <span class="chatText">
            </span>
            <asp:TextBox ID="txtChatMessage" runat="server" Width="115px"></asp:TextBox>
            <asp:LinkButton ID="btnSendChat" runat="server" CommandName="Insert"
                CommandArgument='<%# Eval("Username2") %>'>Send</asp:LinkButton>
        </div>
    </ItemTemplate>
</asp:Repeater>

当我点击后面的代码中的LinkBut​​ton btnSendChat时,如何获取txtChatMessage文本

2 个答案:

答案 0 :(得分:1)

在你的rpChat_ItemCommandevent中你可以得到textBox值

protected void rpChat_ItemCommand(Object Sender, RepeaterCommandEventArgs e)
{
   if(e.CommandName == "Insert")
   {
    TextBox txtChatMessage= (TextBox)e.item.FindControl("txtChatMessage");
    if (txtChatMessage!= null)
    {
        string val = txtChatMessage.Text;
    }
   }
}

答案 1 :(得分:1)

您不需要迭代整个Items集合,只需执行此操作:

ASPX

<asp:Button Text="text" CommandName="myCommand" runat="server" ID="txtFirstName" />

背后的代码

protected void r_ItemCommand(object sender, RepeaterCommandEventArgs e)
{
    switch (e.CommandName)
    {
        case "myCommand":
            var txt = e.Item.FindControl("txtFirstName") as TextBox;
            var myValue = txt.Text;
            // place your awesome code here
            break;
    }
}