I have an ajaxtabcontainer control in my project which contains a datalist and in datalist, I have set the names as a link button. The datalist that I am binding to a source is containing Names and age. Now I want to use the age as a selected value as we use in dropdownlist selected value? what I want is on clicking the name link button I get its value inside a label? how to do it? what I have done is as follows: in Default aspx:
<ajaxToolkit:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
</ajaxToolkit:ToolkitScriptManager>
<ajaxToolkit:TabContainer ID="TabContainer1" runat="server" ActiveTabIndex="1">
<ajaxToolkit:TabPanel runat="server" HeaderText="TabPanel1" ID="TabPanel1">
<HeaderTemplate>
Names
</HeaderTemplate>
<ContentTemplate>
<asp:DataList ID="DataList1" runat="server" RepeatDirection="Horizontal" RepeatColumns="5" ForeColor="Red"
RepeatLayout="Table" >
<ItemTemplate>
<asp:LinkButton Text = '<%#Eval("names")%>' ID="lnkpro" runat="server" ForeColor="Red" OnClick="btn_Click"></asp:LinkButton>
</ItemTemplate>
</asp:DataList>
</ContentTemplate>
</ajaxToolkit:TabContainer>
in cs code:
protected void btn_Click(object sender, EventArgs e)
{
LinkButton myButton = sender as LinkButton;
if (myButton != null)
{
Label1.Text = myButton.Text;
}
}
protected void datalist1_ItemCommand(object source, DataListCommandEventArgs e)
{
int i;
int index = Convert.ToInt32(e.Item.ItemIndex);
LinkButton lnkid = (LinkButton)e.Item.FindControl("lnkpro");
Label1.Text = lnkid.Text;
i = Convert.ToInt32(lnkid.Text);
}
but they are not working. the btn_click is working but giving the only name not its id. the second datalist_itemcommand is not working? Thank you.
you did not have OnItemCommand="datalist1_ItemCommand" in the aspx page. datalist1_ItemCommand should fire after you set OnItemCommand property.
<asp:DataList ID="DataList1" runat="server" RepeatDirection="Horizontal" RepeatColumns="5" ForeColor="Red" OnItemCommand="datalist1_ItemCommand"
RepeatLayout="Table" DataKeyField="age" >
<ItemTemplate>
<asp:LinkButton Text = '<%#Eval("names")%>' ID="lnkpro" runat="server" ForeColor="Red" OnClick="btn_Click"></asp:LinkButton>
</ItemTemplate>
</asp:DataList>
If you are trying to get the age field value in the datalist1_itemCommand function, you need to set DataKeyFeild= "age". And in the item command function you can do Convert.ToInt32(DataList1.DataKeys[e.Item.ItemIndex]) to get the value. Your item Command function will be
protected void datalist1_ItemCommand(object source, DataListCommandEventArgs e)
{
int age = Convert.ToInt32(DataList1.DataKeys[e.Item.ItemIndex]);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With