Asp.net Linkbutton循环 [英] Asp.net Linkbutton loop

查看:94
本文介绍了Asp.net Linkbutton循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对动态链接按钮有疑问,需要帮助.我正在基于数据表中的记录创建一个动态的asp.net表.我也在使用动态链接按钮.

I am having a problem with dynamic link buttons which I need help with. I am creating a dynamic asp.net table based on records in a datatable. I am also using dynamic linkbuttons.

protected System.Web.UI.WebControls.LinkButton lb;    

protected override void OnInit(EventArgs e)
    {
        // Build controls before page load
        lb = new LinkButton();
        lb.Text = "Update Image";

        // LinkButton obj for updating record
        lb.ID = "UpdateImg";
        lb.Click += new EventHandler(UpdateImg);
        this.Controls.Add(lb);
        base.OnInit(e);
    }

我创建了一个新的linkbutton实例OnInit,因此可以在页面加载之前将其添加到页面中.

I create a new linkbutton instance OnInit so I can add it to the page before page load.

foreach (DataRow r in tb.Rows) // Create new row foreach row in table
            {
                TableRow tr = new TableRow();

                // Build cells
                TableCell c1 = new TableCell();
                TableCell c2 = new TableCell();
                TableCell c3 = new TableCell();
                TableCell c4 = new TableCell();
                TableCell c5 = new TableCell();
                TableCell c6 = new TableCell();

                c1.Controls.Add(new LiteralControl(r["Image_id"].ToString()));
                tr.Cells.Add(c1);
                c2.Controls.Add(new LiteralControl(r["Image_name"].ToString()));
                tr.Cells.Add(c2);
                c3.Controls.Add(new LiteralControl(r["Alt_text"].ToString()));
                tr.Cells.Add(c3);
                c4.Controls.Add(new LiteralControl("<input id=\"" + r["Image_id"].ToString() + "\" type=\"checkbox\"" + "\"></input>"));
                tr.Cells.Add(c4);


                LinkButton lbcopy = new LinkButton();
                lbcopy = lb;
                lbcopy.ID = "UpdateImg" + i;
                i++;
                c5.Controls.Add(lbcopy);
                tr.Cells.Add(c5);


                c6.Controls.Add(new LiteralControl("<a href=\"javascript:void(0);\" onclick=\"DeleteImage('" + r["Image_id"].ToString() + "','" + r["Image_name"].ToString() + "');\"><img src=\"../images/clipboard/del.png\" id=\"" + r["Image_id"].ToString() + "\" width=\"20\" height=\"20\" BORDER=0></a>"));
                tr.Cells.Add(c6);
                tblImageLibrary.Rows.Add(tr); // Assign tr to table

然后我使用一个foreach循环来遍历数据表中的每一行,这样我就可以构建每个表行并将单元格添加到每一行.我遇到的问题是linkbutton出现在最后一行.可能是因为只有一个linkbutton对象,并且通过每次循环迭代将其移至下一行?

I then use a foreach loop to iterate through each row in the datatable so I can build each table row and add the cells to each row. The problem I am having is the linkbutton appears in the very last row. Possibly because there is only one linkbutton object and through each loop iteration its being moved to the next row?

我是asp.net的新手,所以请放轻松.

I am new to asp.net so go easy on me.

推荐答案

我感谢您正在尝试做的事情,但是我可以建议另一种方法.尝试使用ListView(.Net 3.5+),并将linkBut​​ton放在每一行中.然后可以根据需要通过链接到

I appreciate what you are trying to do but could I suggest another way. Try using a ListView (.Net 3.5+) and putting the linkButton in each row. The linkButton can be then hidden as required by hooking into the ItemDataBound event. Also the button can have command arguments embedded into it at this point.

 protected void MyListView_ItemDataBound(object sender, ListViewItemEventArgs e)
 {
     LinkButton linkButton = (LinkButton)e.Item.FindControl("linkButtonId");
     System.Data.DataRowView rowView = e.Item.DataItem as System.Data.DataRowView;

     //.. can hide and show depending on data
     linkButton.Visible = rowView["SomeData"].ToString() == "SomeValue";

     //.. or set command arg
     linkButton.CommandArgument = rowView["SomeMoreData"].ToString();
}

然后,链接按钮将触发 ItemCommand 事件.可以从事件中检索数据行和任何命令参数.

The link button will then trigger the ItemCommand event. The row of the data and any command arguments can be retrieved from the event.

 protected void MyListView_OnItemCommand(object sender, ListViewCommandEventArgs e)
 {
     if(e.CommandArgument == "SomeValue")
     {
       //.. do something
     }
 }

类似的方法可用于与早期版本的.Net(分别为1.0和2.0)一起工作的Repeater和GridView控件.您甚至可能会发现GridView更加有用,因为编辑按钮的开箱即用.

Similar methods can be used on Repeater and GridView controls which work with earlier versions of .Net (1.0 and 2.0 respectively). You might even find the GridView more useful as the edit button is more out of the box.

您动态构造表的方式会遇到事件和ViewState问题.利用.Net网格控件会容易得多.

Your way of dynamically constructing a table will get problematic with events and maybe ViewState. Leveraging .Net grid controls would be a lot easier.

这篇关于Asp.net Linkbutton循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆