Murat Akarsu
Murat Akarsu

Reputation: 71

how I get id which is clicked in datalist

I have an table in database.The table contains column name Answer_Id and Answer_Content.And I show Answer_Content in the datalist.My aspx code is below;

<asp:DataList ID="datalistcevaplar" runat="server" Width="740px" OnItemCommand="datalistcevaplar_ItemCommand" >
        <ItemTemplate>
<div class="divcvpicerik">
    <asp:Label ID="lblcvpicerik" runat="server" Text='<%# Eval("Answer_Content") %>'></asp:Label>
</div>
</ItemTemplate>
</asp:DataList>

Now I have to get Answer_Id which I click Answer_Content in the datalist.Please help

Upvotes: 1

Views: 9432

Answers (3)

Alireza Zare
Alireza Zare

Reputation: 1

int count = DataList1.Items.Count;
            for(int i=0;i<count;i++)
            {
            Label lbl = DataList1.Items[i].FindControl("label1") as Label;
            string labeltext = lbl.Text;
            TextBox txttest = new TextBox();
            txttest.Text = labeltext;
            }

Upvotes: 0

Kevin Shah
Kevin Shah

Reputation: 1617

you can do this

<asp:DataList ID="datalistcevaplar" runat="server" Width="740px" OnItemCommand="datalistcevaplar_ItemCommand" DataKeyField="Answer_Id">
<ItemTemplate>
<div class="divcvpicerik">
<asp:LinkButton CssClass="lnkcss" ID="lnkDeactive" runat="Server" CommandName="Answer"
 Text='<%# Eval("Answer_Content") %>'></asp:LinkButton>
</div>
</ItemTemplate>
</asp:DataList>

In this you have to set DataKeyField so that you can get that Id on ItemCommand Event

Now on Item Command event of datalist do code like this.

protected void datalistcevaplar_ItemCommand(object source, DataListCommandEventArgs e)
    {

        if (e.CommandName.Equals("Answer"))
        {
              int AnswerId = Convert.ToInt32(DLUsers.DataKeys[e.Item.ItemIndex].ToString());

        }       
    }

Now using this code you can get Answer_Id or any Id field of Datalist. Try this.

Upvotes: 1

Tim Schmelter
Tim Schmelter

Reputation: 460108

Answer_Content is linkbutton.The code is above is wrong it Answer_content is not a label it is a LinkButton

You can use an invisible Label to store the ID, then it's easy. Handle the LinkButton's ckick-event and use it's NamingContainer and FindControl.

aspx:

<asp:DataList ID="datalistcevaplar" runat="server" Width="740px" OnItemCommand="datalistcevaplar_ItemCommand" >
 <ItemTemplate>
   <div class="divcvpicerik">
    <asp:Label ID="LblID" Visible="false" runat="server" Text='<%# Eval("Answer_Id") %>'>
    </asp:Label>
    <asp:LinkButton id="LnkAnswer"
       Text='<%# Eval("Answer_Content") %>'
       OnCick="LnkClicked" 
       runat="server" />
    </div>
</ItemTemplate>
</asp:DataList>

codebehind:

protected void LnkClicked(Object sender, EventArgs e)
{
    LinkButton btn = (LinkButton) sender;
    DataListItem item = (DataListItem) btn.NamingContainer;
    Label lblId = (Label) item.FindControl("LblID");
    string ID = lblId.Text;
}

Edit or use the CommandArgument of the LinkButton as mentioned from Garrison Neely.

<asp:LinkButton id="LnkAnswer"
       Text='<%# Eval("Answer_Content") %>'
       CommandArgument='<%# Eval("Answer_Id") %>'
       OnCick="LnkClicked" 
       runat="server" />

Then you can get it in this way:

protected void LnkClicked(Object sender, EventArgs e)
{
    LinkButton btn = (LinkButton) sender;
    string ID = btn.CommandArgument;
}

Upvotes: 3

Related Questions