带有按钮,HtmlButtons和LinkBut​​tons的onclick / onserverclick的问题

时间:2019-02-21 17:54:03

标签: javascript c# html asp.net webforms

我在调用功能时遇到了问题。

我已经从对HTML上的按钮进行硬编码转变为在CS中使用添加控件方法;我已经从使用Button和HtmlButton转变为使用LinkBut​​ton。但是这些似乎都不起作用。 Anup Sharma在Onserverclick and onclick not work中建议使用LinkBut​​ton,而Keyvan Sadralodabai则指出,如果runat =“ server”显示在worm元素中,则说明控件设置错误。

所以这是我正在使用的简化版本:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Drawing;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;
    using System.Web.Services;

    using System.Data;
    using MySql.Data;
    using MySql.Data.MySqlClient;

    public partial class backCodeExper : System.Web.UI.Page
    {

        protected void saveRecord(string recordID, string buttonId, string dropdownId)
        {

            PlaceHolder db = Page.FindControl("TestingCS") as PlaceHolder;
            HtmlTable tbl = db.FindControl("TestTable") as HtmlTable;
            HtmlTableRow tr = tbl.FindControl("TheRow") as HtmlTableRow;
            HtmlTableCell tc = tr.FindControl("TheCell2") as HtmlTableCell;
            DropDownList ddl = tc.FindControl(dropdownId) as DropDownList;


            var status = ddl.SelectedValue.ToString();

            HttpContext context = HttpContext.Current;
            MySqlConnection conn = new MySqlConnection();
            conn.ConnectionString = "Server=localhost; Database********; User=********; Password=********; Port=3306"; 
            conn.Open();
            MySqlCommand cmd = new MySqlCommand();
            cmd.Connection = conn;
            cmd.CommandType = CommandType.StoredProcedure;

            cmd.CommandText = "updatetesttable";
            cmd.Parameters.AddWithValue("@param", status);
            cmd.ExecuteNonQuery();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            HtmlTable myTable = new HtmlTable();
            myTable.ID = "TestTable";
            myTable.BorderColor = "teal";
            myTable.BgColor = "black";
            TestingCS.Controls.Add(myTable);

            HtmlTableRow newRow;
            HtmlTableCell cell;
            DropDownList DropList;
            LinkButton saveButton;

            newRow = new HtmlTableRow();
            newRow.ID = "TheRow";
            cell = new HtmlTableCell();
            cell.ID = "TheCell1";
            DropList = new DropDownList();
            DropList.ID = "StatusDD";
            DropList.Items.Add(new ListItem("", "0"));
            DropList.Items.Add(new ListItem("A", "1"));
            DropList.Items.Add(new ListItem("B", "2"));
            DropList.Items.Add(new ListItem("C", "3"));
            cell.Controls.Add(DropList);

            newRow.Cells.Add(cell);

            cell = new HtmlTableCell();
            cell.ID = "TheCell2";
            cell.BgColor = "black";
            saveButton = new LinkButton();
            saveButton.ID = "saveButton";

            saveButton.CommandName = "saveRecord";
            saveButton.CommandArgument = "'1A',this.id,'StatusDD'";
            saveButton.BackColor=Color.Green;
            saveButton.ForeColor=Color.Cyan;
            saveButton.BorderColor=Color.Maroon;
            saveButton.Text = "Save";
            saveButton.Visible = true;
            cell.Controls.Add(saveButton);
            newRow.Cells.Add(cell);
            myTable.Rows.Add(newRow);
        }
    }

通过简单的下拉菜单和(不时髦的)按钮,它就可以很好地加载屏幕(坦率地说,HtmlButton看起来要漂亮得多,但是我的目标是功能性)。

当我从下拉菜单中选择一个项目,然后单击“保存”时,屏幕似乎将刷新,使下拉菜单的值保持与所选值相同。但是,当我检查数据库时,该过程尚未触发。另外,当放置在方法/函数saveRecord中时,我无法执行此代码段Response.Write("<script>alert('Hello');</script>");

此外,当我运行调试模式并将断点放入saveRecord时,没有一个命中。

检查元素后,这就是我得到的: InspectElementResults

有什么建议吗?我想念什么? 如果我不使用LinkBut​​ton(或onServerClick上的Button / HtmlButton),则会收到错误消息,指出未定义函数-这是因为函数/方法是在aspx.cs上定义的,而不是在JS脚本标记中的aspx定义的。

1 个答案:

答案 0 :(得分:0)

我知道了。至少它是有功能的。

我试图设置函数以所需的格式传递所需的值,但是显然当您设置LinkBut​​ton时,它更喜欢对象和Event Args作为参数,而对象是ListButton本身,因此,如果该ListButton对象在其属性中包含您所需的值,则在调用该函数时,您将解析出所需的属性。可能有比将我需要的两个值分配给CommandName和CommandArgument更好的方法,但这可行。 (我曾想过使用.Attributes.Add(“ ROW_ID”,“ 1a”)和.Attributes.Add(“ DD_ID”,“ StatusDD”)...但是最初无法弄清楚如何从发件人对象...稍后将进行调查,并提供功能解决方案。

    ...
        protected void saveRecord(object sender, EventArgs e)
        {
            LinkButton lb = (LinkButton)sender;
            string ROW_ID = (string)lb.CommandName;
            string DD_ID = (string)lb.CommandArgument;
            Response.Write("<script>alert('Hello');</script>");
            ...
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            ...

            saveButton.CommandName = "1a";
            saveButton.CommandArgument = "StatusDD";
            saveButton.Click += new EventHandler(saveRecord);
            ...
        }
    }
相关问题