Asp.net Linkbutton loop

烂漫一生 提交于 2019-12-25 08:27:52

问题


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);
    }

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

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?

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


回答1:


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();
}

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
     }
 }

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.

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




回答2:


I think that is not possible that you want because if you want to add Control dynamically then you must have loop for each record and then you add it in table.




回答3:


Are you trying to render a table with linkbuttons on each row? take look about asp:listview or asp:gridview.

If you can get a DataTable or a DataSet from your database then you just need to bind it to your listview or gridview. e.g.

myListView.datasource = myDataSet myListView.databind()

what else you need is to define layout and specify column name on ur aspx pages.



来源:https://stackoverflow.com/questions/8488086/asp-net-linkbutton-loop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!