如何在gridview中查找文本框控件 [英] How to find textbox control in gridview

查看:101
本文介绍了如何在gridview中查找文本框控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了gridview,其中的文本框附加在特定单元格中,我使用以下代码

I have created gridview which has textboxes attached in specific cells for that i used following code

protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (e.Row.RowIndex >= 5)
            {
                for (int i = 0; i < e.Row.Cells.Count; i++)
                {
                    if (i > 1)
                    {
                        TextBox txt = new TextBox();
                        txt.Text = e.Row.Cells[i].Text;
                        txt.Width = System.Web.UI.WebControls.Unit.Pixel(25);
                        e.Row.Cells[i].Controls.Add(txt);
                    }
                }
            }
        }
    }







现在当我在文本框中输入值时,我想访问相应文本框上的值

我试过的那个

TextBox txt =(TextBox)grwUploadManual。行[y] .Cells [y - 3] .Controls [0];

但看起来错误正在获得

引发类型'System.ArgumentOutOfRangeException'的异常System.Web.UI.Control {System.ArgumentOutOfRangeException

我调试代码并检查该单元格上的值'grwUploadManual.Rows [y] .Cells [y - 3] .Text'我有值但是我无法找到控制我应该如何动态找到控件

提前感谢....




Now When I enter values in textbox I want to access values on that respective textboxes
for that i tried
TextBox txt = (TextBox)grwUploadManual.Rows[y].Cells[y - 3].Controls[0];
But it looks that error is getting
threw an exception of type 'System.ArgumentOutOfRangeException' System.Web.UI.Control {System.ArgumentOutOfRangeException
I debug code and check value on that cell 'grwUploadManual.Rows[y].Cells[y - 3].Text' I got values but i cant find control how should i find controls dynamically
thanks in advance....

推荐答案

只需尝试下面的代码

just try the code below
string s = datagridview1.CurrentCell.EdittedFormattedValue.ToString();



这将为您提供当前单元格值。


This will give you current cell value.


为了得到嵌入在gridview中的textbox的值,你需要找到文本框所在的行索引,你需要知道触发事件的命令arguement和rowcommand_event。



我们使用以下代码派生行索引:



In order to get the value of textbox which is embedded within a gridview, You need to find row index in which the textbox is present and you need to know the command arguement which fires the event and the rowcommand_event.

We derive the row index using the following code:

int rowIndex = ((GridViewRow((LinkButton)e.CommandSource).NamingContainer).
RowIndex;





获取行索引后,我们需要知道文本框的id,我们想让它们的文本我们假设我们的文本框有一个idtxtTitle,我们得到它包含的文本如下:





After getting the row index we need to know the id of the textbox whose text we want to get let us assume that our textbox has an id "txtTitle" and we derive the text it contains as follows:

string title = ((TextBox)GridView1.Rows[rowIndex].FindControl("txtTitle")).Text;


TextBox txt=(TextBox)GridViewFRM.Rows[i].Cells[0].FindControl("GVQuestionTextBox");
string Ques = txt.Text;



http://www.aspsnippets.com/ Green / Articles / Find-and-access-Text-control-in-GridView-FooterTemplate-Footer-Row-in-ASPNet.aspx [ ^ ]

http ://www.aspdotnet-suresh.com/2012/06/get-find-controls-inside-of-aspnet.html [ ^ ]



-KR


http://www.aspsnippets.com/Green/Articles/Find-and-access-Value-of-TextBox-control-in-GridView-FooterTemplate-Footer-Row-in-ASPNet.aspx[^]
http://www.aspdotnet-suresh.com/2012/06/get-find-controls-inside-of-aspnet.html[^]

-KR


这篇关于如何在gridview中查找文本框控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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