为什么从背后的代码访问FormViews或GridViews中的文本框如此困难 [英] Why is it so hard to access textboxes that are in FormViews or GridViews from Code Behind

查看:60
本文介绍了为什么从背后的代码访问FormViews或GridViews中的文本框如此困难的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我已经尝试了很长时间,试图找出为什么我主要不能访问文本框,但是也不能访问放置在GridView(或FormView)内的标签或任何其他控件的原因.
我需要在自定义GridView模板中放置一个文本框,该模板将允许用户输入日期,然后单击提交"以将该日期发布到数据库中.

我想尝试处理代码隐藏文件中的所有插入"命令,但是Intellisense无法识别文本框的名称.

请有人可以简单地解释一下我如何在GridView中引用这些文本框.

提前谢谢.

皮特

-------------------------------------------------- ------

谢谢你们到目前为止的回答,对您最有帮助,但是我仍然很胖,无法正常工作:

就在这里.我已经在下面包含了下面的代码,我想做的是这样的:

当用户单击GridView"ActionsGridView"中的选择"链接(标准的允许GridViews允许选择链接")时,它将获取TextBox"ActionDateCompText"的值,然后在数据库中对其进行更新.

我正在使用一个名为"ActionsAddActionConn"的SqlDataConnection,并且隐藏代码"更新参数如下:

Hi
I''ve been trying for so long to try and find out why I can''t access Textboxes mainly but also labels or any other controls that are placed inside a GridView (or FormView).

I have a need to put a Textbox inside a Custom GridView template that will allow users to enter a date and then click on Submit to post this date to the database.

I like to try and handle all Insert commands in the codebehind file, but Intellisense won''t recognise the names of the Textboxes.

Please could somebody simply explain how I reference these textboxes in the GridView.

Thanks in advance.

Pete

--------------------------------------------------------

Thanks guys for the answers so far, most helpful but I''m still being thick and can''t get it working:

Right here''s a little more. I''ve included the code behind below and what I want to happen is this:

When the user clicks on the Select link in the GridView "ActionsGridView" (the standard Allow Selection Link for GridViews) it will take the value of TextBox "ActionDateCompText" and then update this in the database.

I am using a SqlDataConnection called "ActionsAddActionConn" and the Code Behind update parameters are as follows:

protected void ActionsGridView_SelectedIndexChanged(object sender, EventArgs e)
{
    ActionsAddActionConn.UpdateParameters["ACTION_STATUS_ID"].DefaultValue = "12";
    ActionsAddActionConn.UpdateParameters["ACTION_COMPLETED_DATE"].DefaultValue = DateTime.Now.ToShortDateString();
    ActionsAddActionConn.Update();
}



我希望"ACTION_COMPLETED_DATE"条目是GridView中TextBox"ActionDateCompText"的值.

希望这能帮助大家使我走上正确的轨道(或者我做错了所有事情吗?)?



I want the entry of "ACTION_COMPLETED_DATE" to be the Value of the TextBox "ActionDateCompText" in the GridView.

Hope this helps you all to get me on the right track (or am I doing it all wrong)?

推荐答案

尝试使用此

Try this

protected void ActionsGridView_SelectedIndexChanged(object sender, EventArgs e))
{
 ActionsAddActionConn.UpdateParameters["ACTION_STATUS_ID"].DefaultValue = "12";
    ActionsAddActionConn.UpdateParameters["ACTION_COMPLETED_DATE"].DefaultValue = DateTime.Now.ToShortDateString();

   foreach (GridViewRow row in GridView1.Rows)
   {
      
      Control ctrl = row.FindControl("txtdate") as Textbox;
        if (ctrl != null)
        {
            Textbox txtdate=(Textbox)ctrl;
ActionsAddActionConn.UpdateParameters["ActionDateCompText"].DefaultValue=   txtdate.Text;

           
      
        }
   }
}


您无法直接访问控件,因为它们在模板中.对于GridView,在GridView_RowUpdating事件上,您可以执行以下操作在该行中找到控件:

You can''t access the controls directly because they''re in a template. In the case of the GridView, on your GridView_RowUpdating event, you can do something like this to find controls in that row:

GridViewRow row = myGridView.Rows[e.RowIndex];

TextBox txt = row.FindControl("ID_OF_TEXTBOX") as TextBox;



找到控件后,一切照常进行.



After you''ve found your control, it''s business as usual.




您无法直接访问位于gridview下的文本框.但是有一种解决方法,您可以尝试一下
Hi,

There is no way you can directly access textbox placed under gridview.But there is workaround, you can try this
foreach (GridViewRow item in grvCustomers.Rows)
       {
            TextBox txtFirstName = item.FindControl("txtFirstName") as TextBox;
       }



考虑到您的gridview ID为 grvCustomers ,而所需的文本框ID为txtFirstName

您可以在提交"按钮单击事件中编写此代码,在该事件中,需要检索所有用户输入的数据并将其全部发送到数据库insert/update.

希望这会有所帮助.



Considering that your gridview id is grvCustomers and your desired textbox id is txtFirstName

You can write this inside Submit button click event where you need to retrieve all user entered data and send all them to database insert/update .

Hope this will help.


这篇关于为什么从背后的代码访问FormViews或GridViews中的文本框如此困难的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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