获取标签文本值的当前值
Get the current value of a label's text value
我搜索了其他类似的问题,其中大部分与jscript有关。我在尝试学习 C# 时遇到了足够困难,所以希望有答案。我好像总是接近答案才发现我遇到了另一个隐藏的异常。我想做的是解决必须以更优雅的方式存在的问题,但它是:我有一个复选框,选中后,将 Label1 文本更改为“1”,未选中时将 Label1 文本更改为“0” .单击页面上的按钮时,它会将数据字段发送到存储过程。尽管我可以看到标签值是 1 或 0,但发送到 SP 函数总是为 Label1 发送 'NULL',我假设这是空白标签的默认值。那么如何让页面在点击按钮时读取标签的当前值呢?
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-/W3C//DTD/ XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1-transitional.dtd">
<script runat="server">
public void InsertCard (object source, EventArgs e)
{
SqlDataSource1.Insert();
}
public void CheckBox1_CheckChanged (object source, EventArgs e)
{
Response.Write("");
if (CheckBox1.Checked == true)
{
Label1.Text = "1";
}
else
{
Label1.Text = "0";
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:sqldatasource
id="SqlDataSource1"
runat="server"
connectionstring="<%$ ConnectionStrings:DevTestConnectionString %>"
insertcommand="AddMember" InsertCommandType="StoredProcedure">
<insertparameters>
<asp:FormParameter Name="CardNumber" formfield="CardNumberBox" Type="int32" />
<asp:FormParameter Name="NameFirst" formfield="NameBox" Type="String" />
<asp:FormParameter Name="Valid" formfield="Label1" Type="string" />
</insertparameters>
</asp:sqldatasource>
<div>
<asp:Label ID="Label1" runat="server"></asp:Label>
<hr />
Last Name:<asp:TextBox id="NameBox" runat="server" Width="150px"></asp:TextBox>
<br>
Card Number:<asp:textbox id="CardNumberBox" runat="server" />
<br>
<asp:CheckBox ID="CheckBox1" runat="server" Text=" Valid" OnCheckedChanged="CheckBox1_CheckChanged" AutoPostBack="true" />
<br>
<asp:button id="Button1" runat="server" text="Add Card" onclick="InsertCard" />
</div>
</form>
</body>
</html>
为什么要用标签?直接使用复选框并将 table 列上的数据类型设置为位。将标签替换为下面。
<asp:ControlParameter ControlID="CheckBox1" Name="Valid" PropertyName="Checked" Type="Boolean"/>
我搜索了其他类似的问题,其中大部分与jscript有关。我在尝试学习 C# 时遇到了足够困难,所以希望有答案。我好像总是接近答案才发现我遇到了另一个隐藏的异常。我想做的是解决必须以更优雅的方式存在的问题,但它是:我有一个复选框,选中后,将 Label1 文本更改为“1”,未选中时将 Label1 文本更改为“0” .单击页面上的按钮时,它会将数据字段发送到存储过程。尽管我可以看到标签值是 1 或 0,但发送到 SP 函数总是为 Label1 发送 'NULL',我假设这是空白标签的默认值。那么如何让页面在点击按钮时读取标签的当前值呢?
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-/W3C//DTD/ XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1-transitional.dtd">
<script runat="server">
public void InsertCard (object source, EventArgs e)
{
SqlDataSource1.Insert();
}
public void CheckBox1_CheckChanged (object source, EventArgs e)
{
Response.Write("");
if (CheckBox1.Checked == true)
{
Label1.Text = "1";
}
else
{
Label1.Text = "0";
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:sqldatasource
id="SqlDataSource1"
runat="server"
connectionstring="<%$ ConnectionStrings:DevTestConnectionString %>"
insertcommand="AddMember" InsertCommandType="StoredProcedure">
<insertparameters>
<asp:FormParameter Name="CardNumber" formfield="CardNumberBox" Type="int32" />
<asp:FormParameter Name="NameFirst" formfield="NameBox" Type="String" />
<asp:FormParameter Name="Valid" formfield="Label1" Type="string" />
</insertparameters>
</asp:sqldatasource>
<div>
<asp:Label ID="Label1" runat="server"></asp:Label>
<hr />
Last Name:<asp:TextBox id="NameBox" runat="server" Width="150px"></asp:TextBox>
<br>
Card Number:<asp:textbox id="CardNumberBox" runat="server" />
<br>
<asp:CheckBox ID="CheckBox1" runat="server" Text=" Valid" OnCheckedChanged="CheckBox1_CheckChanged" AutoPostBack="true" />
<br>
<asp:button id="Button1" runat="server" text="Add Card" onclick="InsertCard" />
</div>
</form>
</body>
</html>
为什么要用标签?直接使用复选框并将 table 列上的数据类型设置为位。将标签替换为下面。
<asp:ControlParameter ControlID="CheckBox1" Name="Valid" PropertyName="Checked" Type="Boolean"/>