【发布时间】:2019-03-24 20:49:10
【问题描述】:
我正在尝试使用 c# 变量来删除属性。
在实际编码应用程序之前,我正在测试我的方法。
我已经尝试过 javascript 和 jQuery,但没有发现任何允许 用代码隐藏中的字符串值替换 TextBox ID。
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="index.aspx.cs" Inherits="WebApplication2.index" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="myText" runat="server" required="required"></asp:TextBox>
</div>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication2
{
public partial class index : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string straspID = "myText";
bool fieldRequired = false;
if (fieldRequired == false)
{
//FindControl("myText");
FindControl(straspID);
if (straspID != null)
//
myText.Attributes.Remove("required");
// I want to use straspID instead of the ID of the asp page
// which will returned from a table - I'm simulating here
// I get an error if I use straspID for remove attribute
}
}
}
}
我的预期结果是删除所选 ID 的属性。 我目前遇到语法错误。
【问题讨论】:
-
您是否尝试过使用
myText?为什么要改用字符串? -
我将从包含 asp ID 的表中获取值
-
mytext 有效,但我不知道标签在现实世界中的价值
-
我的建议是不要使用 Web 表单......它实际上没有用,并且在这一点上被认为是遗留......使用.net core mvc 或 web api。无论如何,看看stackoverflow.com/a/34759219/8167494,它可能会有所帮助
-
将其称为无用有点牵强@IsaacVidrine。
标签: javascript c# jquery asp.net