我遇到了要调用的函数没有触发的问题。
我已经从对 HTML 上的按钮进行硬编码,转而使用 cs 中的添加控件方法;我已经从使用 Button 和 HtmlButton 转向使用 LinkButton。然而,这些似乎都不起作用。在Onserverclick 和 onclick 不起作用Anup 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 看起来更好,但我的目标是首先实现功能)。
当我从下拉列表中选择一个项目然后单击保存时,屏幕似乎会刷新,使下拉列表的值与选择的值相同。但是,当我检查数据库时,该过程并未触发。此外Response.Write("<script>alert('Hello');</script>");
,当放置在方法/函数 saveRecord 中时,我无法执行此代码段。
此外,当我运行调试模式并在 saveRecord 中放置断点时,它们都没有被命中。
检查元素后,这就是我得到的:InspectElementResults
有什么建议么?我错过了什么?如果我不使用 LinkButton(或带有 onServerClick 的 Button/HtmlButton),那么我会收到错误消息,指出该函数未定义 - 这是因为函数/方法是在 aspx.cs 上定义的,而不是 JS 脚本标签中的 aspx。
我已经弄清楚了。至少,它是功能性的。
我试图设置函数以我想要的格式传递我想要的值,但显然当你设置 LinkButton 时,它更喜欢对象和事件参数作为参数,并且对象是 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);
...
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句