views:

138

answers:

3

Possible Duplicate:
Using ASP.NET Controls without databinding

My previous question yielded few results so I thought I would reword the question and try again.

foreach (XXX x in TTTT){ %>
       <tr>
           <td><%=x.val %></td>
               <asp:ImageButton runat="server" CommandName="uiImgResolve_Click"
               CommandArgument='<%=X.pkid%>' ImageUrl="../images/IMG.png"/>
     <% } %>

This will not work as I cannot set a value in any control that has the RUNTAT attribute set. What is the best way to give each row a value so when the on click event happens I can read in the value of the item clicked, I cannot put the value on the query string and I cannot use asp.net databinding.

what I am doing is much more complex then this, databinding will not work.

PERHAPS a better way of asking the question is is there anyway to use the looping mechanism above and still maintain a link to a method on the codebehind of a control?

+3  A: 

I would recommend looking into the ASP repeater control. It provides an ItemDatabound event that allows you to manipulate the contents that is being dynamically generated.

LBushkin
what I am attempting to do involves looping thu and displaying multiple generic lists. I am not going to use databinding cause it is just going to create a mess, Thank you for your answer tho.
A: 

Mixing code into your markup really does not work well with ASP.Net server controls. There are several better alternatives. Here's one:

Markup:

<table>
  <asp:Placeholder id="RowPlaceholder" runat="server" />
</table>

Code behind:

foreach (MyObject x in TT)
{
   Literal l1 = new Literal();
   l1.Text = string.Format("<tr><td>{0}</td><td>", x.val);

   ImageButton b = new ImageButton();
   b.ImageUrl = "../images/IMG.png";
   b.CommandArgument = x.pkid;
   b.CommandName = "uiImgResolve_Click";  // maybe do this a little differenlty

   Literal l2 = new Literal();
   l2.TExt = "</td></tr>";

   RowPlaceholder.Controls.Add(l1);
   RowPlaceHolder.Controls.Add(b);
   RowPlaceHodler.Controls.Add(l2);

}
Joel Coehoorn
Thanks Joel this is the first answer that is useful and not just a complaint... Would you recommend removing the asp control and just use an html img?
No, because you want it wired up to a server event.
Joel Coehoorn
You'd be better with a repeater and using databind - makes it a bit simpler to see what's going on from the html and you don't have to deal with the complexities you get from dynamically adding controls to the page.
Paddy
@paddy it would not work in this instance
no idea why this is -1 it is the only answer that is close to what I am looking for....
A: 

A Repeater and even a GridView might just work. There's a need to perform OnRowCommand though. Definitely cleaner like what Paddy said.

ASPX

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowDataBound="GridView1_RowDataBound">
    <Columns>
        <asp:BoundField DataField="val" />
        <asp:TemplateField>
            <ItemTemplate>
                <asp:ImageButton ID="Button1" runat="server" />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

CodeBehind:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        GridView1.DataSource = TTTT;
        GridView1.DataBind();
    }
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType != DataControlRowType.DataRow)
        return;

    XXX x = (XXX)e.Row.DataItem;
    ImageButton btn = (ImageButton)e.Row.FindControl("Button1");
    btn.CommandArgument = x.pkid;
    btn.ImageUrl = "../images/IMG.png";
}
o.k.w
@o.k.w This is not an option, the logic is much more complex then shown and would involve embedding multiple controls inside one another.
OWK I am aware you can do this quite easily but what I am attempting to do involves looping thu and displaying multiple generic lists. I am not going to use databinding cause it is just going to create a mess, Thank you for your answer tho.
@Qiky Well, I suppose I wouldn't have the answer for the unknown complexity nature of your problem.
o.k.w
I should have been asked my question better sorry :)
@Qiky Perhaps what you need is to generate the controls dynamically like what Joel suggested. Tricky part is at which aprt of the page lifecycle do you do this. Do it on page load and you will not be able to get postback behaviour desired. I can go on more, but still, I don't know exactly your detailed question/problem. Good luck.
o.k.w