You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何设置运行时创建的input控件的值?

问题描述

我用C#在表单页面运行时创建控件,因为样式需求,通过Literal生成input控件并添加到表格单元格(示例中tc为表格单元格),创建代码如下:

ltl = new Literal();
ltl.Text =
    "<div class=\"number-input\"> " +
    "  <button onclick=\"this.parentNode.querySelector('input[type=number]').stepDown(); return false;\" class=\"nopopup minus\">-</button> " +
    "  <input runat=\"server\" ID=\"edProduct1Quantity\" class=\"nopopup quantity\" min=\"0\" name=\"edProduct1Quantity\" value=\"0\" step=\"12\" type=\"number\"> " +
    "  <button onclick=\"this.parentNode.querySelector('input[type=number]').stepUp(); return false;\" class=\"nopopup plus\">+</button> " +
    "</div> ";

tc.Controls.Add(ltl);

处理表单时可以通过名称读取值:

FieldText = Page.Request.Form["edProduct1Quantity"].ToString();

但创建控件后,在另一个例程中设置默认值时,参考方法都无效,比如执行Page.Request.Form["edProduct1Quantity"] = "1";会引发异常。这不是页面生命周期问题,之前用TextBox控件时能正常设置值,换成input后找不到设置方法,请问有可行的设置方式吗?

可行的设置方式

方法1:动态修改Literal的Text内容

既然控件是通过Literal输出的,直接修改Literal的Text属性,替换input的value值即可。可以在需要设置默认值的例程中,用字符串替换更新value:

// 假设ltl是已创建的Literal实例
string updatedHtml = ltl.Text.Replace("value=\"0\"", "value=\"1\"");
ltl.Text = updatedHtml;

如果需要更灵活的变量赋值,创建Literal时可以用占位符,后续替换:

// 创建时用占位符
ltl.Text =
    "<div class=\"number-input\"> " +
    "  <button onclick=\"this.parentNode.querySelector('input[type=number]').stepDown(); return false;\" class=\"nopopup minus\">-</button> " +
    "  <input runat=\"server\" ID=\"edProduct1Quantity\" class=\"nopopup quantity\" min=\"0\" name=\"edProduct1Quantity\" value=\"{DEFAULT_VALUE}\" step=\"12\" type=\"number\"> " +
    "  <button onclick=\"this.parentNode.querySelector('input[type=number]').stepUp(); return false;\" class=\"nopopup plus\">+</button> " +
    "</div> ";

// 设置默认值时替换占位符
ltl.Text = ltl.Text.Replace("{DEFAULT_VALUE}", "1");

方法2:改用HtmlInputNumber控件替代Literal

直接使用ASP.NET的HtmlInputNumber控件,既能保留原生input的样式,又能像服务器控件一样操作:

// 创建数字输入控件
HtmlInputNumber quantityInput = new HtmlInputNumber();
quantityInput.ID = "edProduct1Quantity";
quantityInput.Name = "edProduct1Quantity";
quantityInput.Value = "0";
quantityInput.Min = "0";
quantityInput.Step = "12";
quantityInput.Attributes.Add("class", "nopopup quantity");

// 创建容器div
HtmlGenericControl numberDiv = new HtmlGenericControl("div");
numberDiv.Attributes.Add("class", "number-input");

// 创建减号按钮
HtmlGenericControl minusBtn = new HtmlGenericControl("button");
minusBtn.Attributes.Add("class", "nopopup minus");
minusBtn.Attributes.Add("onclick", "this.parentNode.querySelector('input[type=number]').stepDown(); return false;");
minusBtn.InnerText = "-";

// 创建加号按钮
HtmlGenericControl plusBtn = new HtmlGenericControl("button");
plusBtn.Attributes.Add("class", "nopopup plus");
plusBtn.Attributes.Add("onclick", "this.parentNode.querySelector('input[type=number]').stepUp(); return false;");
plusBtn.InnerText = "+";

// 组装控件并添加到单元格
numberDiv.Controls.Add(minusBtn);
numberDiv.Controls.Add(quantityInput);
numberDiv.Controls.Add(plusBtn);
tc.Controls.Add(numberDiv);

后续设置值直接操作控件的Value属性:

HtmlInputNumber input = (HtmlInputNumber)tc.FindControl("edProduct1Quantity");
if (input != null)
{
    input.Value = "1";
}

方法3:通过客户端脚本设置值

如果是页面渲染完成后需要设置值,可以注册客户端脚本,在页面加载时修改input的value:

Page.ClientScript.RegisterStartupScript(this.GetType(), "SetQuantityValue", 
    "document.getElementById('edProduct1Quantity').value = '1';", true);

内容的提问来源于stack exchange,提问作者Darren

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.08 17:53:13