Changing an ImageButton CommandName located in a DataList Using C#

Hi, I asked this question before but did not get an answer in C#.
I have a LinkButton in the HeaderTemplate of a DataList.
When you click it, I want it to change the CommandName of a ImageButton in the same DataList Control located in it's ItemTemplate.

How do I do this in C#?  I get an error on line 3 of this code.
Thanks for your help.

LPP

========================================================

          void TestDataBound (object sender, DataListCommandEventArgs e) {
        ImageButton ib = e.Item.FindControl("edittemplate") as ImageButton;
            Response.Write(ib.CommandName);                  //<<----------Object reference not set to an instance of an object.
            Response.End();
      }
</script>
<html>
<body>
<form id="myform" name="myform" runat="server">
<asp:DataList ID="dl1" ShowHeader="true" runat="server" ExtractTemplateRows="false" OnItemCommand="TestDataBound">
      <headertemplate>
                              <asp:LinkButton ID="linkbutton" runat="server" CommandName="Test" Text="Test Item Data Bound" /><br>
      </headertemplate>
      <itemtemplate>
            <asp:ImageButton ID="edittemplate" CommandName="EditTemplate" runat="server" Visible="false" ImageUrl="images/view.jpg" CommandArgument='<%#DataBinder.Eval(Container.DataItem, "EventID")%>' />
      </itemtemplate>
</asp:DataList>
lpparkerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

PeterFearnCommented:
void TestDataBound (object sender, DataListCommandEventArgs e)
{
        ImageButton ib = e.Item.FindControl("edittemplate") as ImageButton;

        if (ib != null)
            // do stuff here...
}
lpparkerAuthor Commented:
Here is my solution to the actual question I had.

<script language="c#" runat="server">
      public string sTextBox1, LinkButtonText;
      public static string CommandName;
      Hashtable hTable;
      Hashtable hTable2;

      void Page_Load() {
            hTable = new Hashtable();
            hTable2 = new Hashtable();
            
            if (!IsPostBack) {
                  CommandName = "ShowJobListings";
                  LinkButtonText = CommandName;
                  hTable2.Add("EventID", "6");
                  dl1.DataSource = hTable2;
                  dl1.Visible = true;
                  Page.DataBind();
            }
      }

      void TestDataBound (object sender, DataListCommandEventArgs e) {
            switch (e.CommandName) {
                  case "SwitchMode":
//                        hTable = new Hashtable();
                        switch (CommandName) {
                              case "ShowTemplates":
                                    CommandName = "ShowJobListings";
                                    LinkButtonText = CommandName;
                                    hTable.Add("EventID", "7");
                              break;
                              case "ShowJobListings":
                                    CommandName = "ShowTemplates";
                                    LinkButtonText = CommandName;
                                    hTable.Add("EventID", "6");
                              break;
                        }
                        dl1.DataSource = hTable;
                        dl1.Visible = true;
                        Page.DataBind();
                  break;
                  case "ShowTemplates":
                        Response.Write(e.CommandArgument + " " );
                        Response.Write(e.CommandName);
                        Response.End();
                  break;
                  case "ShowJobListings":
                        Response.Write(e.CommandArgument + " " );
                        Response.Write(e.CommandName);
                        Response.End();
                  break;
                  default:
                        Response.Write("error");
                  break;
            }
      }
</script>
<html>
<body>
<form id="myform" name="myform" runat="server">
<asp:DataList ID="dl1" ShowHeader="true" runat="server" ExtractTemplateRows="false" OnItemCommand="TestDataBound">
      <headertemplate>
            <asp:LinkButton ID="linkbutton" runat="server" CommandName="SwitchMode">
                  <%#CommandName%>
            </asp:LinkButton><br>
      </headertemplate>
      <itemtemplate>
            <asp:ImageButton ID="edittemplate" CommandName='<%#CommandName%>' runat="server" ImageUrl="images/view.jpg" CommandArgument='<%#DataBinder.Eval(Container.DataItem, "Value")%>' AlternateText='<%#CommandName%>' />
      </itemtemplate>
</asp:DataList>

PeterFearnCommented:
ok, try this in the item command event handler for the :
switch(e.CommandName)
{
    case "SwitchMode":
            
// loop through all items in data list
    foreach(DataListItem item in dl1.Items)
    {
// try to find the image button
         ImageButton ib = item.FindControl("edittemplate") as ImageButton;

// skip to next datalist item if image button not found
         if (ib == null)
             continue;

// set the command name of the image button
         ib.CommandName = "whatever you want it to be here";
    }
    break;
}

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.