处理 Sharepoint 中 Web 部件内的回发
我正在通过 CreateChildControls() 中的 Web 部件代码初始化 GridView、文本框和按钮。
上述控件被声明为类变量,但仅在稍后初始化。
接下来,我给出了按钮单击的处理程序。处理函数应该用作搜索 - 对文本框中输入的内容执行一些操作,在 Gridview 中加载结果,显示 Gridview
当我在文本框中键入内容并单击按钮时,相同的控件再次加载并且文本框中输入的内容会丢失。我尝试过 ViewState() 和 ViewState() 但无济于事。网格没有显示,因为我的逻辑跳过了尝试绑定它,因为没有正确的搜索字符串。
我的问题: 1)在哪里/如何从文本框中获取值回发? 2) 在 PreRender() 中填充 GridView 是否有意义,或者事件处理会在 PreRender() 之后发生吗?
编辑: 似乎事件处理程序没有被调用。
Dim btnClickHandler = New EventHandler(AddressOf SetSearchParameter)
AddHandler srchBtn.Click, btnClickHandler
是正确的吗?
编辑: 我从 0 开始重新编写了所有代码。现在可以使用了。
I am initializing a GridView, text box and a button via code to a Webpart in CreateChildControls()
The above controls are declared as class variables but initialized only later.
Next, I've given the handler for button click. The handler function is supposed to work as a search - perform some operations on the content entered in the textbox, load the results in the Gridview, display the Gridview
When I type something in the text box and hit the button, the same controls load again and the content entered in the text box is lost. I've tried ViewState() and ViewState() but to no avail. The grid doesn't show because my logic skips attempting to bind it since a proper search string was not available.
My questions:
1) Where/how can I get the values postback from the textbox?
2) Will it make sense to populate the GridView in PreRender() or will Event Handling happen after PreRender()?
EDIT:
Seems like the event handler is not getting called.
Dim btnClickHandler = New EventHandler(AddressOf SetSearchParameter)
AddHandler srchBtn.Click, btnClickHandler
is correct?
EDIT:
I redid all code from 0. Its working now.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这也总是让我感到困惑。我总是引用这个问题以便弄清楚(还有控制执行生命周期)。
您不需要管理视图状态,这应该由您的控件自动处理。
我建议使用 OnPreRender 来填充您的网格。届时,您的控件的值应该由 ViewState 填充。
This always confuses me too. I always reference this question in order to get it straight (also the control execution lifecycle).
You shouldn't need to manage viewstate, that should be taken care of automatically by your controls.
I would suggest using the OnPreRender to populate your grid. Your controls should have their values populated by the ViewState by then.
您应该能够在按钮单击的事件处理程序中引用控件值,具体取决于如何构建它们静态/动态。 (textbox)Page.FindControl('controlname').value 在按钮单击的事件处理程序中。
呃...这个.findcontrol。
我凭记忆这样做,所以语法可能会关闭。
you should be able to reference the control values in the event handler for the button click, depending on how to build them static/dynamic. (textbox)Page.FindControl('controlname').value in your event handler for the button click.
er... this.findcontrol.
im doing this from memory so syntax might be off.