Click here to Skip to main content
15,896,912 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My datalist is DataList1. I have a footer template to show a button 'BtnCheckOut'.
I get an error like 'Object reference not set to an instance of an object.' on my code at line Button dd1 = (Button)DataList1.Items[index].FindControl("BtnCheckOut");
dd1.Enabled = true;

C#
public void dl_item_command(Object sender, DataListCommandEventArgs e)
    {
        if (e.CommandName == "myevent") // check commandname here
        {
        //BtnCheckOut.Enabled = true;
            int index = e.Item.ItemIndex;
            Button dd1 = (Button)DataList1.Items[index].FindControl("BtnCheckOut");
            dd1.Enabled = true;




XML
<FooterTemplate>
        <asp:Button ID="BtnCheckOut"  runat = "server" Text='Checkout'   Enabled="false"
           Width="100px"  onclick="CheckOutBtn_Click"
           style="z-index: 1; width:80px;cursor:pointer;  font-family:Arial;  text-align:center; height:22px;  font-weight:600; " >
           </asp:Button>
           </FooterTemplate>
Posted
Updated 30-Nov-13 3:15am
v3
Comments
Richard MacCutchan 30-Nov-13 4:56am    
The message means some part of that statement failed to return a valid object. Quite possibly the index variable is not valid.
S.Rajendran from Coimbatore 30-Nov-13 6:01am    
I tried like this. No use.
public void dl_item_command(Object sender, DataListCommandEventArgs e)
{
if (e.CommandName == "myevent") // check commandname here
{
foreach (DataListItem item in DataList1.Items)
{
if (item.ItemType == ListItemType.Footer)
{
Button dd1 = (Button)item.FindControl("BtnCheckOut");

}
}
S.Rajendran from Coimbatore 30-Nov-13 6:34am    
The Button on PageLoad is set to 'false'. When the user selects a row in the datalist , I want to make the button(inside footer) as 'enabled=true'. Thats all.
JoCodes 30-Nov-13 9:54am    
Are you getting error in ddl.Enabled = true or the above line?
S.Rajendran from Coimbatore 30-Nov-13 10:21am    
Yes

1 solution

Hello SRajendran,
I had to dig till now the most from the solution of this question so if you got it works for you don't forget to mark it as answer and to upvote also.
Now below is solution

C#
if (e.CommandName == "HitMyCommand")
           {
                  Button btn= (Button)DataList1.Controls[DataList1.Controls.Count - 1].FindControl("Button1");
                  btn.Enabled = true;

           }


Here Trick is in DataList1.Controls.Count . As we know Footer will be the last in Controls index and header will always be first.

Hope This Works For YOu Also!!!
 
Share this answer
 
Comments
S.Rajendran from Coimbatore 1-Dec-13 3:44am    
Thanks a lot.It works.
C For Code!!! 1-Dec-13 4:54am    
Its my pleasure

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