在ASP,NET中实现简单的button_click时出错 [英] error implementing simple button_click in ASP,NET

查看:74
本文介绍了在ASP,NET中实现简单的button_click时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

-我已将ASP按钮拖放到Web表单上.当有人单击按钮时,我希望它的文本属性发生变化.

-这是它的HTML

- I have drag-dropped an ASP button on to a webform. When someone clicks the button, I want it''s text property to change.

- This is the HTML for it

<asp:Button ID="LoginButton" runat="server" Text="Login" OnClick="LoginButton_Click" />




-这是背后的vb代码




- and this is the vb code behind

Protected Sub LoginButton_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        LoginButton.Text = "123"
    End Sub



-我收到错误消息未声明Name" Loginbutton".这确实很有趣,因为intellisense可以很好地检测到它!

-顺便说一下,这是一个ASP.NET Web应用程序....该代码在ASP.NET网站上也能正常工作.



- i''m getting the error "Name ''Loginbutton'' is not declared''. And this is really funny, cause the intellisense detects it just fine!

- btw this is an ASP.NET web application....the code works just fine on an ASP.NET website.

推荐答案

事件处理程序的sender属性.我相信这会产生相同的效果:

An alternative could be to use the sender property of the event handler. I believe this will have the same effect:

Protected Sub LoginButton_Click(ByVal sender As Object, ByVal e As System.EventArgs)
    Dim button as Button = CType(sender, Button)
    button.Text = "123"
End Sub



如果您习惯了这种习惯,则可以使事件处理程序更容易重用,具体取决于您的期望行为.无论按钮的名称如何,此事件处理程序都会始终将单击的按钮的文本更改为123.但是,您将需要执行常规检查,以确保发件人实际上是一个按钮,否则将引发异常.

免责声明:我只是直接在此处键入此代码,所以我还没有对其进行测试,自从我使用VB以来已经有一段时间了,所以您可能必须整理语法错误和不正确的类型名称等,但希望您能理解.



If you get in to the habit of this it can make your event handlers easier to reuse, depending on your desired behaviour. This event handler will always change the clicked button''s text to 123, regardless of the name of the button. However, you will need to implement the normal checks to make sure that sender actually is a button, otherwise you''ll get exceptions thrown.

Disclaimer: I just typed this code as is straight in to here, I haven''t tested it and it''s been a little while since I used VB, so you might have to sort out syntax errors and incorrect Type names, etc., but hopefully you get the idea.


感谢...显然,我需要初始化控件....奇怪的是ASP.NET网站(如相对于Web应用程序)不需要.
thanks...apparently i need to initialize the control....strange that ASP.NET websites (as opposed to web applications) dont require this.


您好,

您的查询非常简单,您要做的就是在按钮的click事件中编写以下代码

hello,

your query is very simple all u have to do is write the following code in button''s click event

Protected Sub LoginButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles LoginButton.Click
       LoginButton.Text = "abc"
   End Sub




我真的不知道为什么给定u错误,您的代码也很好
一旦发现有用就对我的答案进行评分

谢谢&问候
基:rose:




I really dont know why its given u error, ur code is also fine
do rate my answer once u find it useful

Thanks & regards
Radix :rose:


这篇关于在ASP,NET中实现简单的button_click时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆