问题描述
你好专家
任何人都可以帮助我如何从Gridview中动态添加的文本框中访问数据. Findcontrol
方法在这里不起作用,因为Gridview控件在页面回发时丢失了其子控件(模板字段,文本框等).
我知道我们需要在回发时重新创建它们,但是问题是如何获取用户在文本框中输入的数据.
我知道一种方法是从Request.Form
字符串中获取它们,但这不是可接受的处理方法.
请对此提供您宝贵的建议.
提前谢谢!
非常感谢您的答复Sandeep,但是我之前已经尝试过我们的建议,但它们没有用.您能否提供一些代码示例,在其中启用ViewState并重新创建网格,您可以获取动态添加的文本框的数据.
谢谢
Hello experts
Can anyone help me on how to access data from dynamically added textbox in a Gridview. Findcontrol
method wont work here, as the Gridview control lost its childcontrol(template field,textbox etc.) on postback of page.
I know that we need to re-create them on postback but the issue is how to get the data in textbox entered by the user.
I know that one way is to get them from Request.Form
string but that is not an acceptable way to deal with it.
Please provide your valuable suggestion on this.
Thanks in advance!
Thank u so much for ur reply Sandeep , But i tried already ur suggetions before , they are not working. Can u please provide me some code sample where with ViewState enabled and recreation of grid u get the data of dynamically added textbox back.
Thanks
推荐答案
我知道我们需要重新创建他们回发,但是问题是如何在用户输入的文本框中获取数据
I know that we need to re-create them on postback but the issue is how to get the data in textbox entered by the user
是的,只需重新创建网格和控件,然后基于选定的行,找到文本框,您就应该能够获取数据.确保已启用文本框的ViewState.
此外,如果仍然遇到问题,那么您始终可以使用JavaScript来保留您的值.例如,一种方法可以是:
1.在动态创建文本框时,在其onchange/onblurr/onfocusout事件上附加一个JavaScript函数.可以说函数名称是DataChanged();
2.现在,每当更改文本框数据时,都将调用此JS,并且可以在此JS函数中将文本框值存储在隐藏"字段(hdF1)中.还将网格行索引或行标识符存储在另一个隐藏字段(hdF2)中
3.页面回发之后,您已经重新创建了网格.使用hdF2转到该行,然后使用hdF1设置文本框的值.
此外,请确保是否存在动态创建的控件的任何服务器事件,然后在回发时重新绑定事件.完成后,将调用它们并执行它们.
Yes, just re-create the grid and controls and then based on the row selected, find the textbox, you should be able to get the data. Make sure the ViewState of the textbox is enabled.
Further, if you still face issues then you can always use JavaScript to hold on the values for you. Like, one of the ways can be:
1. While creating the textbox dynamically, append a JavaScript function to it on it''s onchange/onblurr/onfocusout event. Lets say the function name is DataChanged();
2. Now, whenever textbox data is changed, this JS will be called and in this JS function you can store the textbox value in a Hidden field (hdF1). Also store the grid row index or row identifier in another hidden field (hdF2)
3. After the page postbacks and you have re-created the grid. Use the hdF2 to get to the row and then hdF1 to set back the value of the textbox.
Further, make sure if there were any server events of the dynamically created control then on postback you re-bind the events. Once done, they will be called and executed.
这篇关于从Gridview中动态添加的文本框访问数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!