Using ASP.NET Controls without databinding

2020-03-31 21:08发布

问题:

It appears that I have been using asp.net databinding for so long I have forgotten some of the basics.

I am dynamically creating a table in an ASPX page without databinding.

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

As you can probably see I cannot use the RUNAT=Server or if I do I cannot specify an id for the row. This is just a simple example what I am attempting to do is much more complex and is not conducive to databiding. In Classic ASP we used to just call a JS method and then submit the form with the PKID embedded in a hidden field. It there a more elegant way to solve this(We are using Jquery if this helps)

AS I said I am VERY familiar with all the .net controls and use them often, in this instance it is not the correct tool.

回答1:

You can pass the parameters by query string.
something like this :

foreach (XXX x in TTTT){ %>
   <tr>
       <td><%=x.val %></td>
           <a href='?Action=Resolve&pkid=<%=X.pkid%>'>
               <img src='../images/IMG.png' />
           </a>
 <% } %>


回答2:

I'd suggest a listview control. Something like this:

<asp:ListView ID="lvImages" runat="server" OnItemDataBound="lvImages_ItemDataBound">
    <LayoutTemplate>
        <table class="tblImages" cellpadding="0" cellspacing="0">
            <tr class="ImageHeaderRow">
                <td>Imagename</td>
                <td>Image</td>
                <td>Location</td>
            </tr>
            <asp:PlaceHolder ID="itemContainer" runat="server"></asp:PlaceHolder>
        </table>
    </LayoutTemplate>
    <ItemTemplate>
        <tr>
            <asp:literal ID="ltrCell1" runat="server" />
            <td><asp:ImageButton ID="imgMyImage" runat="server" /></td>
            <td><asp:Label ID="lblLocation" runat="server" ></asp:Label></td>
        </tr>
    </ItemTemplate>
</asp:ListView>

Code behind :

protected void lvImages_ItemDataBound(object sender, ListViewItemEventArgs e) {
            if (e.Item.ItemType == ListViewItemType.DataItem) {
                ListViewDataItem currentItem = (ListViewDataItem)e.Item;
                MyImageObject oImg = (MyImageObject)currentItem.DataItem;


                Literal ltrCell1 = e.Item.FindControl("ltrCell1") as Literal;
                ImageButton imgMyImage = e.Item.FindControl("imgMyImage") as ImageButton ;
                Label lblLocation= e.Item.FindControl("lblLocation") as Label;
                ltrCell1.text = string.format(@"<td id=""{0}"">{1}</td>", oImg.id, oImg.val);
               imgMyImage.CommandArgument = oImg.arg;
               imgMyImage.CommandName = "cmdNAme";
               imgMyImage.ImageUrl = oImg.URL;

}

BEst of Luck!