Link to home
Start Free TrialLog in
Avatar of lpparker
lpparker

asked on

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>
Avatar of PeterFearn
PeterFearn

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

        if (ib != null)
            // do stuff here...
}
Avatar of lpparker

ASKER

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>

ASKER CERTIFIED SOLUTION
Avatar of PeterFearn
PeterFearn

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial