用户控件的控件值总是使用此用户控件在页面后面的代码中返回null,为什么? [英] control values of a usercontrol are always returning null in code behind of the page using this usercontrol, why?

查看:79
本文介绍了用户控件的控件值总是使用此用户控件在页面后面的代码中返回null,为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


Web编程的新手,使用vs2010,我使用ajax控件工具包控件,asp控件和html控件(输入文本框)创建了一个用户控件(UserControl.ascx),我在页面上使用了该用户控件(second.aspx) ,现在用户将在该页面中输入数据,这将填充用户控件文本框,填充后将按OK按钮,而OK按钮位于second.aspx上,因此在填充数据后,用户将单击OK,我称在此second.aspx页的文件后面保存用户控制代码的功能,但是当我执行此操作时,所有文本框无论这些文本框是否由用户填充都将返回null.这里有什么问题?这与我通过新对象确定后使用usercontrols函数有关吗
(
我在second.aspx后面的代码中创建了
UserControl uControl =新的UserControl()
然后调用保存
uControl.Save()
)
用户控件上的HTML文本框位于asp:table控件中,我尝试使用在second.aspx标记中使用的用户控件的标记名和前缀来访问second.aspx代码后面文件中的usercontrol.aspx代码功能,但是它无法在后面的代码中访问.
请问我是Web编程的新手,我如何访问用户用来输入数据的相同实例文本框?


am new to web programming, using vs2010, i created a user control (UserControl.ascx) thats using ajax control toolkit controls, asp controls and html controls (input text boxes), i used that user control on a page (second.aspx), now users will enter data in that page, which will fill in the user controls text boxes, after filling will press OK button, and the OK button is on second.aspx, so when after filling data user will hit OK, i call the save function of usercontrol code behind file from this second.aspx page, but when i do this all the text boxes no matter these were filled by the user or not will return null. whats the issue here? is it something to do with that i am using the usercontrols function after OK through a new object
(
i-e in Code behind of second.aspx i create
UserControl uControl=new UserControl()
and then call save by it
uControl.Save()
)
HTML textboxes on the user control are whithin an asp:table control, i tried to access functions of usercontrol.aspx code behind file in second.aspx code behind using the tagname and prefix of the usercontrol i used in markup of second.aspx, but its not accessible in code behind.
Pleae i am new to web programming, how do i access the same instance textboxes which user was using to input his data??

推荐答案

hmm对不起,我似乎太幼稚了,但至少有人应该回答.但经过一番挖掘就可以了.如果其他类似我的菜鸟碰到这个帖子,将会解释我做错了什么以及解决问题的方法.
我正在使用在后面的代码中创建的新实例来访问usercontrol的控件,而我已经通过
在aspx标记代码中将该控件重新注册了一个实例,从而创建了一个实例
hmm sorry guys seems like, i was being too childesh, but at least someone should answer. but ok after a little bit digging got it working. and if some other noob like me comes across this post will explain what i was doing wrong and what solved the issue.
i was accessing the controls of a usercontrol using a new instance created in code behind, while i already had created an instance by registring this control in aspx markup code by
<%@ Register src="UserControl.ascx" tagname="ucLogin" tagprefix="MyControls" %>


因此,当我使用此控件时,我给了它一个ID,如下所示


so when i used this control i gave an id to it as below

<MyControls:ucLogin ID="UserLogin"  runat="server" />

因此,通过查看此内容,我认为我也应该使用标记前缀和标记名以相同的方式在代码后面访问它,而这就是我出错的地方,可以将我的usercontrol的多个实例放置在同一页面上,与我在<%@ Register>中注册时定义的标签相同的标签.标签,即

so by looking at this, i thought i should access it the same way in code behind too by using tag prefix and tagname, and this is where i went wrong, i could place multiple instances of my usercontrol on the same page, with same tag i defined while registering it in the <%@ Register> tag, i-e

<mycontrols:uclogin ...="" xmlns:mycontrols="#unknown" />

,但请记住,在标记中创建的该控件的每个新实例都将具有不同的ID,即i-e

but remember each new instance of this control created in markup will have a different id,i-e

<MyControls:ucLogin ID="UserLogin1"  runat="server" />
<MyControls:ucLogin ID="UserLogin2"  runat="server" />
<MyControls:ucLogin ID="UserLogin3"  runat="server" />


就是这样,此id将是我们对UserControl的每个实例的引用,并且将在使用此id的后面的代码中访问该用户控件的控件. i-e
字符串UserName = UserLogin1.txtUserName.Value;

希望对您有所帮助.


and this is it, this id will be our referance to each of the instance of UserControl and will access controls of that user control in code behind using this id. i-e
string UserName = UserLogin1.txtUserName.Value;

I hope it helps someone.


这篇关于用户控件的控件值总是使用此用户控件在页面后面的代码中返回null,为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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