Mark
Mark

Reputation: 2760

How to get value from a repeater in server side on button click

   <asp:Repeater ID="rptList" runat="server">
                         <HeaderTemplate>
                         </HeaderTemplate>
                         <ItemTemplate>
                             <tr>
                                 <td width="15%">
                                     <b>Subject</b>
                                 </td>
                                 <td width="60%">
                                     <%#Eval("Title")%>
                                 </td>
                             </tr>

I do databind to a repeater, and bind the title value.

 string MysqlStatement = "SELECT Title, RespondBy FROM tbl_message WHERE MsgID = @Value1";
        using (DataServer server = new DataServer())
        {
            ..        }
        rptList.DataSource = ds;
        rptList.DataBind();

How can I get the value of title in server side, when a button in clicked in the same page.

Upvotes: 0

Views: 2154

Answers (2)

James Johnson
James Johnson

Reputation: 46047

I would put the title in a server control, like a label, and then you can do something like this:

<asp:Repeater ID="rptList" runat="server"> 
    <ItemTemplate> 
        <asp:Label ID="Label1" runat="server" Text='<%#Eval("Title")%>' />
    </ItemTemplate>
</asp:Repeater>

And then in the code behind:

int itemIndex = 0;

Label lbl = rptList.Items[itemIndex].FindControl("Label1") as Label;
if (lbl != null)
{
    string labelValue = lbl.Text;
}

Upvotes: 1

John Batdorf
John Batdorf

Reputation: 2542

I would set the value of title to the text of a label that you could call FindControl() on.

Upvotes: 1

Related Questions