ASP.NET - 动态添加的按钮包含额外的值属性
因此,我尝试向网站动态添加一个按钮,以使用户了解他们要添加到表单中的内容。但是,当它呈现新按钮时,它会继续将 value 属性提供为“”并忽略我添加的属性。我尝试修复此问题但无济于事,包括在添加我的版本之前删除 value 属性并清除所有属性。
WebControl myControl = null;
string[] elementInfo = elementCode.Split(new char[] { ';' });
switch (elementID)
{
case 1:
myControl = new Button();
myControl.Attributes.Remove("value");
myControl.Attributes.Add("type","submit");
break;
case 2:
myControl = new TextBox();
myControl.Attributes.Clear();
myControl.Attributes.Add("type", "text");
break;
case 3:
myControl = new CheckBox();
myControl.Attributes.Clear();
myControl.Attributes.Add("type", "checkbox");
break;
}
if (myControl != null)
{
string[] properties;
if (elementCode.Length > 0)
{
foreach (string attr in elementInfo)
{
properties = attr.Split(new char[] { '=' });
myControl.Attributes.Add(properties[0], properties[1]);
}
}
return myControl;
}
else
return null;
我知道循环正在触发,并且第 2 行返回的值是一行“value=submit”。事实上,标记是这样出现的:
<div id="divLastElement">
<input type="submit" name="ctl03" value="" type="submit" value="Submit Me!" />
</div>
我确信它是第一个 [value=''] 导致它为空,但是我如何覆盖这种行为? (您可以看到,在生成按钮的 switch 语句中,我尝试提前删除 value 键)
So, I'm trying to dynamically add a button to a site to give the user of an idea of what they are adding to a form. However, when it renders a new button, it keeps providing the value property as "" and ignoring the attribute that I add. I've attempted to fix this to no avail, including removing the value attribute before adding in my version and clearing ALL the attributes.
WebControl myControl = null;
string[] elementInfo = elementCode.Split(new char[] { ';' });
switch (elementID)
{
case 1:
myControl = new Button();
myControl.Attributes.Remove("value");
myControl.Attributes.Add("type","submit");
break;
case 2:
myControl = new TextBox();
myControl.Attributes.Clear();
myControl.Attributes.Add("type", "text");
break;
case 3:
myControl = new CheckBox();
myControl.Attributes.Clear();
myControl.Attributes.Add("type", "checkbox");
break;
}
if (myControl != null)
{
string[] properties;
if (elementCode.Length > 0)
{
foreach (string attr in elementInfo)
{
properties = attr.Split(new char[] { '=' });
myControl.Attributes.Add(properties[0], properties[1]);
}
}
return myControl;
}
else
return null;
I know that the loop is firing and the value returned in line 2 is a single line, "value=submit". In fact the markup comes out thus:
<div id="divLastElement">
<input type="submit" name="ctl03" value="" type="submit" value="Submit Me!" />
</div>
I'm sure it's the first [value=''] that is causing it to be empty, but how do I override this behavior? (You can see that in the switch statement that generates the button I've tried to remove the value key early)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
最终,对于 ASP.NET Button 控件,HTML 值来自 ASP.NET Button 上的 Text 属性。我猜如果没有设置,它只会呈现另一个值属性。
尝试将按钮上的 .Text 属性设置为“提交我!”而不是通过 Attributes 集合设置其值。
所以部分片段看起来像:
Ultimately with an ASP.NET Button control, the HTML value comes from the Text property on the ASP.NET Button. I'm guessing if that's not set, it simply renders another value attribute.
Try setting the .Text property on the Button to "Submit me!" instead of setting its value via the Attributes collection.
So the partial snippet looks like:
尝试使用 HtmlInputButton 相反。
Try using a HtmlInputButton instead.
您需要使用 Text 属性。正如您在评论中所说,您在智能感知中看不到“文本”属性。这是因为对于 Intellisense 来说,myControl 是一个 WebControl(您碰巧制作了一个 Button)。 WebControl 是您创建的特定控件的基类,但它本身没有“Text”属性。请改用以下内容:
YOu need to use the Text property. As you said in a comment you don't see the "Text" property in Intellisense. This is because to Intellisense, myControl is a WebControl (that you happen to make a Button). The WebControl is a base class for the specific controls you create, but itself does not have a "Text" property. Use the following instead: