【发布时间】:2019-07-15 17:40:16
【问题描述】:
我遇到了要调用的函数无法启动的问题。
我已经从对 HTML 上的按钮进行硬编码转变为在 cs 中使用添加控件方法;我已经从使用 Button 和 HtmlButton 转变为使用 LinkButton。然而,这些似乎都不起作用。在Onserverclick and onclick not workAnup Sharma 推荐使用LinkButton,而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
有什么建议吗?我错过了什么? 如果我不使用 LinkButton(或带有 onServerClick 的 Button/HtmlButton),那么我会收到错误消息,指出该函数未定义 - 这是因为函数/方法是在 aspx.cs 上定义的,而不是 JS 脚本标签中的 aspx。
【问题讨论】:
标签: javascript c# html asp.net webforms