Click here to Skip to main content
15,893,814 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to take value from Label in Datalist How to get please give full code in C#

on click of every event I want the value from attached label in row of datalist
Posted
Updated 12-Oct-11 4:54am
v2
Comments
hitech_s 14-Oct-11 7:54am    
which "onclick" you want..?

whether your button inside datalist or outside? plz be specific

Try this:

C#
foreach(var item in DataList1.Items)
    string text = item.Controls[0].Text;


More detail needed in the question for a more detailed answer!
 
Share this answer
 
Comments
Dave Kerr 12-Oct-11 11:39am    
I noticed you posted that this wasn't working - we'll need more details to give you a better answer - what exactly does your data list contain?

If you provide code and more detail you're much more likely to get a decent answer ;)
HARI OM THAPA 27-Feb-13 20:11pm    
Thank You so much, i was too needy for it,...
datalist is like this

XML
<asp:DataList ID="DataList1" runat="server" OnItemCommand="DataList1_ItemCommand">
                        <HeaderTemplate>
                            Roleid Rolename description
                        </HeaderTemplate>
                        <ItemTemplate>
                            <table width="100%">
                                <tr>
                                    <td>
                                        <asp:Label ID="lblid11" runat="server" Text='<%#Eval("ROLE_ID") %>' />
                                    </td>
                                    <td>
                                        <asp:Label ID="lblid12" runat="server" Text='<%#Eval("ROLE_NAME") %>' />
                                    </td>
                                    <td>
                                        <asp:Label ID="lblid13" runat="server" Text='<%#Eval("DESCRIPTION") %>' />
                                    </td>
                                    <td>
                                        <asp:Button ID="btninside" runat="server" CommandName="call" Text="click me" />
                                    </td>
                                </tr>
                            </table>
                        </ItemTemplate>

                    </asp:DataList>







in button click if you want the label data (label is inside the datalist)
if this button is outside the datalist
you can get the label text like this

<asp:button id="btntest" runat="server" text="Test" onclick="btntest_Click" xmlns:asp="#unknown" />


C#
protected void btntest_Click(object sender, EventArgs e)
    {

        foreach (DataListItem item in DataList1.Items)
        {
            lblm.Text += "," + ((Label)(item.FindControl("lblid11"))).Text;
        }
    }



if your button is inside the datalist you can access like this

C#
 protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
{
if (e.CommandName == "call")
 {
 DataListItem item=(DataListItem)(((Button) (e.CommandSource)).NamingContainer);
 string text = ((Label)item.FindControl("lblid12")).Text;
}
}
 
Share this answer
 
Comments
Sukla Chakraborty 24-Nov-12 1:40am    
nice solution
HARI OM THAPA 27-Feb-13 20:11pm    
Thank You so much, i was too needy for it,...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900