按钮单击一个TableCell的从未火灾表中消失 [英] Button Click in a TableCell never fires and table disappears

查看:229
本文介绍了按钮单击一个TableCell的从未火灾表中消失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的A SP.NET Web表单应用程序,我有一个我在其中添加动态的所有数据。行包含按钮中的每个单元格。我想按钮,当用户点击它开火的onclick 事件。但是,随着低于code,事件永远不会触发与表中消失。这里的code:

I my ASP.NET Webforms app, I have a table in which I add all data dynamically. One row contains Buttons in each cell. I want the button to fire onclick event when the user clicks on it. But, with the below code, the event never fires and the table disappears. Here's the code :

    <asp:Table ID="floorTable" runat="server"  Width="100%" GridLines="Both">
    </asp:Table>

在code后面

   // This method is called on a DropDownList SelectedItemChanged Event - so
   // the buttons cannot be created in Page_Load or so. Have to create 
   // totally based on the DropDown selected item.
    private void PopulateFloorRow(int floorNo, FloorPattern fp)
    {
        int cols = fp.UnitPattern.Count;

        // HEADER ROW
        TableRow thead = new TableRow(); 
        thead.Width = Unit.Percentage(100);
        thead.TableSection = TableRowSection.TableHeader;
        TableCell theadCell = new TableCell();
        theadCell.ColumnSpan = cols;
        Label title = new Label();
        title.Text = "Floor # " + floorNo;
        theadCell.Controls.Add(title);
        thead.Controls.Add(theadCell);

        TableRow planRow = GetFloorPlan(floorNo, fp);

        TableRow tr = new TableRow();
        TableCell tc = null;
        int tcWidPerc = (int)fp.UnitPattern.Count / 100;

        foreach (UnitPattern up in fp.UnitPattern)
        {                
            tc = new TableCell();

            Button imgBtn = new Button();

            // On Adding BELOW Line - ERROR - 0x800a1391 - JavaScript runtime error: 'UnitLinkClicked' is undefined
            //imgBtn.Attributes.Add("onClick", "UnitLinkClicked(this)");

            imgBtn.CommandArgument = up.UnitPatternId.ToString(); // I want to know which button is pressed. So, sort of Tag
            imgBtn.Click += new EventHandler(UnitLinkClicked);
            imgBtn.BorderWidth = Unit.Pixel(10);
            imgBtn.BorderColor = Color.Transparent;
            imgBtn.Width = Unit.Percentage(100);
            if (up.UnitNo != null)
            {
                imgBtn.Text = up.UnitNo;
            }

            tc.Controls.Add(imgBtn);

            tr.Controls.Add(tc);
        }

        floorTable.Rows.Add(thead);
        floorTable.Rows.Add(planRow);
        floorTable.Rows.Add(tr);

        // Create Footer
        PopulateTableFooter(cols);

    }

    protected void UnitLinkClicked(object sender, EventArgs e)
    {
        Button btn = (Button)(sender);
        string upId = btn.CommandArgument;

        System.Diagnostics.Debug.WriteLine("LINK Button clicked Of UP ID :" + upId);
    }

编辑:的SelectedIndexChanged添加了code

EDIT : CODE OF SELECTEDiNDEXCHANGED ADDED

我DropDown_SelectedIndexChanged code:

My DropDown_SelectedIndexChanged Code :

    protected void floorDropDownList_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (selectedProject == null)
            selectedProject = _db.Projects.Find(projectsList.SelectedValue);

        System.Diagnostics.Debug.WriteLine("SELCTED Project = " + selectedProject.ProjectId);

        // "Select Floor" is selected, so Hide floor Table
        if (floorDropDownList.SelectedValue == "-1")
        {
            floorTable.Visible = false;
        }
        else
        {
            int floorNo = int.Parse(floorDropDownList.SelectedValue);
            if (floorNo > 0)
            {
                PopulateFloorRow(floorNo, (FloorPattern)selectedProject.FloorPattern.ElementAt(floorNo - 1));
            }
        }
    }

如果我在我的下拉菜单中选择3,出现预期的表。我点击一个按钮,表中消失,但在降值仍低于3而已。

If I had selected "3" in my drop down, the table appears as expected. I click on a button and the table disappears, but the value in the drop down in still "3" only.

编辑零件OVER

通过上面的code,我当我点击一个按钮,在 UnitLinkClicked 事件从来没有发射(我加的断点),整个表中消失。

With the above code, I when I click on a button, the UnitLinkClicked event is never fired (I had added breakpoint) and the whole table disappears.

可以说,这可能是什么问题?默认的按钮,就是要的AutoPostBack &安培;它不具有该属性也。缺少什么我在这里,如何解决这个问题。我坚持在这天起,并试图弄清楚。

Can you say what problem can this be ? A button by default is meant to be AutoPostBack & it doesn't have that property too. What am I missing here and how to solve this. Am stuck on this since days and trying to figure out.

任何帮助是非常AP preciated。
谢谢

Any help is highly appreciated. Thanks

推荐答案

您需要设置一个ID为您的按钮。如果没有ID的事件不会被解雇。

You need to set an ID for your button. Without the ID the event will not be fired.

Button imgBtn = new Button();
imgBtn.ID = "imgBtn";

另外,还要确保每次添加按钮都有一个唯一的ID。你很可能没有任何拼接的地板上。或任何其他领域的唯一的名称到达。

Also make sure that each button you add has a unique ID. You could probably concatenate the floor no. or any other fields to arrive at a unique name.

此外,该表将消失,除非,您管理您的Page_Load。你需要重新加载表,如果下拉列表中有一个选定的索引。

Also, the table will disappear unless, you manage that in your Page_Load. You need to reload the table if the DropDown has a selected index.

编辑:code样品

的.cs code

protected void Page_Load(object sender, EventArgs e)
{
    if (IsPostBack)
    {
        PopulateFloorRow();
    }
}        

protected void cmb_SelectedIndexChanged(object sender, EventArgs e)
{
    PopulateFloorRow();
}

private void PopulateFloorRow()
{
    floorTable.Rows.Clear();

    for (int i = 0; i < 2; i++)
    {
        TableRow tableRow = new TableRow();
        tableRow.Cells.Add(new TableCell());
        tableRow.Cells.Add(new TableCell());

        tableRow.Cells[0].Controls.Add(new Label() { Text = cmb.SelectedItem.Text });
        Button button = new Button();
        button.ID = "btn" + i.ToString();
        button.Text = "Click";
        button.Click += button_Click;
        tableRow.Cells[1].Controls.Add(button);
        floorTable.Rows.Add(tableRow);
    }

}

void button_Click(object sender, EventArgs e)
{
    throw new NotImplementedException();
}

的.aspx code

.aspx code

<asp:DropDownList id="cmb" runat="server" OnSelectedIndexChanged="cmb_SelectedIndexChanged" AutoPostBack="true"><asp:ListItem>One</asp:ListItem><asp:ListItem>Two</asp:ListItem></asp:DropDownList>
<asp:Table ID="floorTable" runat="server"  Width="100%" GridLines="Both">
</asp:Table>

这篇关于按钮单击一个TableCell的从未火灾表中消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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