如何保持标签在Asp.net回发后的价值? [英] How to maintain the value of label after postback in Asp.net?
问题描述
在asp.net页面,我需要知道如何保持由HTML按钮点击指定特定标签的值。后回传完成的。
In asp.net page I need to know how to maintain the value of a particular label assign by the html button click. After postback done.
详细code:
<table>
<tr>
<td><asp:Label ID="lbl1" runat="server" ClientIDMode="Static">Before Changing</asp:Label></td>
<td><asp:Label id="lbl2" runat="server" ClientIDMode="Static"></asp:Label></td>
<td><asp:TextBox ID="txtbox" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td><asp:Button ID="btnasp" runat="server" Text="ASP Button" Height="50px" Width="150px" OnClick="btnasp_Click"/></td>
<td><input type="button" id="btnhtml" value="HTML Button" onclick="showlabel()" style="height:50px; width:150px"/></td>
</tr>
</table>
剧本
<script type="text/javascript">
function showlabel() {
$('#lbl1').text("After Changing");
}
</script>
CS code
cs code
protected void btnasp_Click(object sender, EventArgs e)
{
txtbox.Text = lbl1.Text;
}
输出
如果我点击HTML按钮改变之前,标签文字的变化改变了。然后我点击按钮ASP后更改值呈现在文本框。
If I click HTML button the label text before changing is change to after changing. Then I click the ASP button after changing value is show in textbox.
这是无隐藏字段添加值,而不是使用服务器控件的HTML按钮来完成。这怎么可能?
This is done without adding value in hidden field and not using the server control to html button. How is this possible?
推荐答案
的标签
转换到跨度
而像HTML元素跨度
或 DIV
没有的ViewState
这些都不text / html文件被发送到服务器端的如格式
元素。张贴在表单元素是输入元素和隐藏
字段就是其中之一。在asp.net使用隐藏域维护的ViewState。恐怕你必须使用隐藏字段,以保持回发之间标签的值。
The label
is converted in to span
and the html element like span
or div
does not have ViewState
neither the text/html of these is sent to server side like form
elements. The form elements are posted are the input elements and hidden
field is one of them. The asp.net maintain ViewState using hidden field. I am afraid you have to use hidden field to maintain the value of label between postbacks.
HTML
<input id="hdnLabelState" type="hidden" runat="server" >
的的JavaScript 的
document.getElementById('<%= hdnLabelState.ClientID %>').value = "changed value of span";
的服务器端(code后面)的
string changedLabelValue = hdnLabelState.Value;
这篇关于如何保持标签在Asp.net回发后的价值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!