Click here to Skip to main content
15,900,511 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In my following code-behind I want to assign the value to Label4 using datalist1. Earlier, in 'datalist1' I pass value to 'Label1' from column 'item_name'. On button18_click
I get the following exception as: 'Object reference not set to an instance of an object'.
Can anyone guide me in this regard so that I could pass the value to Label4 in code behind.
Does asp.net has this facility to findcontrol that lyes within a datalist?

XML
<asp:DataList ID="Datalist1" runat="server" DataKeyField="item_code"  RepeatDirection="Horizontal"
    RepeatLayout="Table"  OnItemCommand="dl_item_command"  >
    <HeaderTemplate>
    </HeaderTemplate>
    <ItemTemplate>
    Item Name:
    <asp:Label Id="Itmnamelbl" runat="server" Text='<%#Eval("item_name") %>'>
    </asp:Label>
    <br />
<asp:Button ID="Button18" runat="server" ForeColor="White"  onclick="Button18_Click"
 BackColor="Teal" Font-Size="small"
 style="z-index: 1;font-family:Sans-Serif; height:22px;  font-weight:900; "
 Text="Add to cart" />
 </ItemTemplate>
<SelectedItemTemplate>
<asp:Label id="Label1"
Text='<%#DataBinder.Eval(Container.DataItem,"item_name") %>'
runat="server"/>
<br />
</SelectedItemTemplate>
</asp:DataList>

C#
protected void Button18_Click(object sender, EventArgs e)
    {
              Label4.Text = (Datalist1.SelectedItem.FindControl("Label1").ToString());
    }
Posted
Updated 27-Sep-13 18:11pm
v4
Comments
idenizeni 27-Sep-13 12:25pm    
I'm guessing you never set the SelectedItem so this is the object that is throwing the error because it is not set to an instance.

Hi,
If you have set OnItemCommand event of datalist, then why you are firing alone button click event, which is inside datalist??
Add commandname,commandargument to button and handle button click event in OnItemCommand of datalist. 

ASP.NET
<!--design side-->
<asp:datalist id="Datalist1" runat="server" datakeyfield="item_code" repeatdirection="Horizontal" repeatlayout="Table" onitemcommand="dl_item_command" xmlns:asp="#unknown">
    <headertemplate>
    </headertemplate>
    <itemtemplate>
         .....
         <!--See CommandName. Add CommandArgument if you required to do so.-->
         <asp:button id="Button18" runat="server" forecolor="White" commandname="myevent">
         BackColor="Teal" Font-Size="small" style="z-index: 1;font-family:Sans-Serif;
         height:22px;  font-weight:900;" Text="Add to cart" />
    </asp:button></itemtemplate>
</asp:datalist>

C#
//Server side
protected void dl_item_command(object source, DataListCommandEventArgs e)
{
    if (e.CommandName == "myevent") // check commandname here
    {
        int index = e.Item.ItemIndex;
        Label lbl = (Label)Datalist1.Items[index].FindControl("Label1");
        Label4.Text = lbl.Text; 
        // your code
    } 
}

Hope it helps you.
Thanks.
 
Share this answer
 
You forgot convert object type as label.
Try this :
C#
Label4.Text = ((Label)DataList1.SelectedItem.FindControl("Label1")).Text;



--Amit
 
Share this answer
 
Comments
S.Rajendran from Coimbatore 27-Sep-13 5:28am    
I get the same exception.
S.Rajendran from Coimbatore 27-Sep-13 5:31am    
i tried like this:
Label4.Text = ((Label)Datalist1.SelectedItem.FindControl("Label1")).Text;
Get the same exception.

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