2
<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>

How do I get the txtChatMessage text when I click on LinkButton btnSendChat in code behind

HatSoft
  • 11,077
  • 3
  • 28
  • 43
sly_Chandan
  • 3,437
  • 12
  • 54
  • 83

2 Answers2

1

In your rpChat_ItemCommandevent of the Button you can get the textBox value

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;
    }
   }
}
HatSoft
  • 11,077
  • 3
  • 28
  • 43
  • The problem with your code is that it will **always** return the first textbox in the first row. Not the **current textbox in the current row** – Jupaol Jul 22 '12 at 16:27
1

You do not need to iterate the whole Items collection, simply do this:

ASPX

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

Code behind

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;
    }
}
Jupaol
  • 21,107
  • 8
  • 68
  • 100
  • The code works. In your case it isn't because the control you are looking for is in a nested server control: `
    ` that's the reason, you would have to change the call `var txt = e.Item.FindControl("txtFirstName") as TextBox;` to use a recursive search. Check this link: http://stackoverflow.com/questions/4955769/better-way-to-find-control-in-asp-net
    – Jupaol Jul 22 '12 at 21:43