简体   繁体   中英

Asp.net Linkbutton loop

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.

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.

我认为这是不可能的,因为如果要动态添加Control,则必须为每个记录创建循环,然后将其添加到表中。

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

myListView.datasource = myDataSet myListView.databind()

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM