如何将标签控制文本存储到文本框控制值中
我正在尝试将ASPX页面中文本框控件的值设置为标签文本的值。我正在使用以下代码,但什么也不会发生。我已经使用C#从文件后面的代码尝试了,然后我想将文本框值分配给会话变量。如果我只分配了“ Hello”之类的字符串值,则可以使用,但否则无效。
protected void btnBook_Click(object sender, EventArgs e)
{
txtmtcid.Text = blbtest.Text;
Session["mtcid"] = txtmtcid.Text;
//Response.Redirect("booknow.aspx");
}
}
我还尝试了JavaScript,但没有用:
var mtcid = parsedData.employeeCode;
document.getElementById('txtmtcid').value = mtcid;
如果我将mtCID的值分配给标签文本,但对于文本框而不是,上述JS代码可以正常工作。请帮忙。
i am trying to set the value of a textbox control in my aspx page as the value of a label text. I am using the following code but nothing happens. I have tried from the code behind file using c# and then I want to assign the textbox value to a session variable. If I just assign a string value like "Hello"it works, but otherwise nothing works.
protected void btnBook_Click(object sender, EventArgs e)
{
txtmtcid.Text = blbtest.Text;
Session["mtcid"] = txtmtcid.Text;
//Response.Redirect("booknow.aspx");
}
}
I also tried with Javascript, but no use:
var mtcid = parsedData.employeeCode;
document.getElementById('txtmtcid').value = mtcid;
The above js code works fine if I am assigning the value of mtcid to a label text, but not for the text box. please help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不会显示可能会搞砸的事情。
而且我们看不到您使用的标记。
您的页面加载事件是什么样的。请记住,每个按钮单击,后背包或使用事件代码存根的页面上的任何控件都会每次运行页面加载事件。页面加载事件每次运行,按钮点击代码运行。
因此,如果我有此标记:
然后在此背后进行编码:
然后单击按钮
因此,我们可以看到现在如何复制可差的文本到文本框。
您必须发布更多的标记,并给出futher detilas(例如一些网格视图,中继器或任何其他可以解释您的问题的功能)。
请注意,您可以双击Web表单设计器中的按钮以创建事件,以单击该按钮。可能不存在该按钮的事件代码,并且您的按钮点击代码和背后的代码永远不会运行。
You don't show things that could be messing this up.
and we can't see your markup used.
What does your page load event look like. Keep in mind that EVERY button click, post-back or ANY control on the page with a event code stub WILL RUN the page load event EVERY time. the page load event runs every time, and BEFORE your button click code runs.
So, if I have this markup:
And then code behind of this:
I then get this when I click on the button
So, we can see how the lable text is now copy to the text box.
You have to post more markup, and give futher detilas (such as some grid view, repeater or any other boatload of features that well explain your issue).
Note that you can double click on the button in the web forms designer to create a event click for the button. Its possible that no event code for the button exists, and your button click code and code behind never runs.
因此,这是标记:
代码背后:
更新JS:
我是JS和ASP.NET的新手,因此请尝试浏览任何可能的东西并在此处解决问题。会话变量值只是没有传递到下一页。老实说,我不需要文本框,我不知道标签文本是否可以存储在会话变量中。如果可以的话,那么我要做的就是将blbtest标签文本分配到会话变量,但这也没有起作用,但是如果我像这样进行硬编码:
这起作用,并且会传递会话变量的值。
因此,我现在正在使用文本框尝试。请让我知道是否有更好的方法。
如果有一种方法可以避免使用标签和文本框,而只需将会话变量传递到“ session” [“用户名”],从后面的代码到JavaScript,那就太好了。我该怎么做?
So this is the markup:
Code behind:
updated js:
I am new to js, and asp.net, so trying to browse whatever possible and work things out here. The session variable value is just not getting passed to next page. Honestly, i dont need the textbox, I dont know if label text can be stored in session variables. If thats possible, then all I want to do is assign the blbtest label text to the session variable, but that also didnt work,but if I am hard coding it like:
this works and the value of session variable is passed.
hence I am trying now with textbox. Please let me know if theres a better approach to this.
If there is a way to avoid the use of the label and textbox and simply pass the session variable, Session["username"] from the code behind to the javascript, that would be great. how can I do it?