Changing a CommandName to a ImageButton located in a DataLIst

How do I access and change a CommandName to a ImageButton located inside a ItemTemplate in a DataList Control.
I tried many things and am not an expert.  Please profide any feedback.
Any code examples?

LPP
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.

RogerSTHLMCommented:
Use the ItemDataBound event of the Datalist control. Here you can get a ref to your controls inside the datalist.

[pseudo code in vb.net]
sub datalist_itemdatabound(...,...)
   dim datalistItem as datalistitem=e.item
   select case datalistitem.itemtype
      case datalistitem.item,datalistitem.alternativeitem
         dim theData as ???=datalistitem.dataitem <-Here's the data for the specific row. The type depends on what you've bound to the datalist
         dim yourImageBtn as imagebutton=directcast(datalistitem.findcontrol("imageButtonControlId"),imagebutton)
         select case thedata.???
              case something
                  yourimageBtn.commandname="Something"
              case somethingElse
                  yourimageBtn.commandname="SomethingElse"

Hope it helps!
R
   

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
lpparkerAuthor Commented:
Do you know how to do t his in c#?
Can you list the arg list too?  is it DataLIstBoundEventArgs????? or son.

Thnx
lpparkerAuthor Commented:
Hi, actually I am not sure if that answered my question.  I need samples in C#.  and here is my code.  I get an error on the 3rd line.

Thanks,

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:Table runat="server">
                  <asp:TableRow runat="server">
                        <asp:TableCell runat="server">
                              <asp:LinkButton ID="linkbutton" runat="server" CommandName="Test" Text="Test Item Data Bound" /><br>
                        </asp:TableCell>
                  </asp:TableRow>
            </asp:Table>
      </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>

<asp:DataList ID="dl2" ShowHeader="true" runat="server" ExtractTemplateRows="true" Visible="false">
//...
</asp>
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.