【问题标题】:Issues with onclick/onserverclick with buttons, HtmlButtons, and LinkButtons带有按钮、HtmlButtons 和 LinkBut​​tons 的 onclick/onserverclick 问题
【发布时间】:2019-07-15 17:40:16
【问题描述】:

我遇到了要调用的函数无法启动的问题。

我已经从对 HTML 上的按钮进行硬编码转变为在 cs 中使用添加控件方法;我已经从使用 Button 和 HtmlButton 转变为使用 LinkBut​​ton。然而,这些似乎都不起作用。在Onserverclick and onclick not workAnup Sharma 推荐使用LinkBut​​ton,而Keyvan Sadralodabai 表示如果在昆虫元素中显示runat="server",那么他的控件设置错误。

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

    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。

【问题讨论】:

    标签: javascript c# html asp.net webforms


    【解决方案1】:

    我已经弄明白了。至少,它是功能性的。

    我试图设置函数以我想要的格式传递我想要的值,但显然当您设置 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);
                ...
            }
        }
    

    【讨论】:

      猜你喜欢
      • 2011-03-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-09
      • 1970-01-01
      相关资源
      最近更新 更多