GridView FindControl方法 [英] GridView FindControl Method

查看:87
本文介绍了GridView FindControl方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从gridview获取TextBox的值



I want to fetch the value of TextBox from gridview

int TaskDay = Convert.ToInt32(((TextBox)row.FindControl("TextBoxSalary01")).Text);





我知道我可以使用此Findcontrol获取值( ControlName);



但我想要检索喜欢



string ControlName =TextBoxSalary01;

Findcontrol(ControlName);



是否可以??????





I know i can get the value using this Findcontrol("ControlName");

But I want to retrieve lik

string ControlName="TextBoxSalary01";
Findcontrol(ControlName);

will it be possible??????

 string Box = "TextBoxSalary01";
            foreach(GridViewRow row in GridView1.Rows)
            {
                int TaskDay = Convert.ToInt32(((TextBox)row.FindControl(Box)).Text);
}





我得到了

发生了'System.NullReferenceException'类型的异常WebApplication2.dll但未在用户代码中处理





I got
"An exception of type 'System.NullReferenceException' occurred in WebApplication2.dll but was not handled in user code"

<Columns>
                <asp:BoundField DataField="EmployeeID" HeaderText="EmployeeID" InsertVisible="False" ReadOnly="True" SortExpression="EmployeeID" />
                <asp:BoundField DataField="FirstName" HeaderText="FirstName" SortExpression="FirstName"/>
                <asp:BoundField DataField="LastName" HeaderText="LastName" SortExpression="LastName" />
                <asp:BoundField DataField="Department" HeaderText="Department" SortExpression="Department" />
                <asp:BoundField DataField="Salary" HeaderText="Salary" SortExpression="Salary" />
         
         <asp:TemplateField HeaderText="Day01" Visible="false">
             <ItemTemplate>
                 <asp:TextBox ID="TextBoxSalary01" runat="server"></asp:TextBox>
             </ItemTemplate>
         </asp:TemplateField>

         <asp:TemplateField HeaderText="Day02" Visible="false">
             <ItemTemplate>
                 <asp:TextBox ID="TextBoxSalary02" runat="server" Text=""></asp:TextBox>
             </ItemTemplate>
         </asp:TemplateField>
         <asp:TemplateField HeaderText="Day03" Visible="false">

             <ItemTemplate >
                 <asp:TextBox ID="TextBoxSalary03" runat="server"></asp:TextBox>
             </ItemTemplate>
         </asp:TemplateField>
             </Columns>





TextBoxSalary01,TextBoxSalary02,TextBoxSalary03是我的项目模板



所以当我做FindControl我不能直接提供名字。相反,我在变量中分配值并在FindCOntrol中使用tat变量



TextBoxSalary01, TextBoxSalary02, TextBoxSalary03 are my item Templates

So when i do FindControl I cant provide name directly. Instead i'm assigning the value in a variable and using tat variable in FindCOntrol

推荐答案

根据你的问题,它应该工作

< br $>


int h = 0;

TextBox nt =(TextBox)Gridview1.Rows [h] .Cells [1] .FindControl(txt_Product_Name );

string myVal = nt.Text;



使用FindControl方法你可以在网格视图中选择任何控件的值。



希望它有效...编码快乐
According to your question, it should work


int h=0;
TextBox nt = (TextBox)Gridview1.Rows[h].Cells[1].FindControl("txt_Product_Name");
string myVal= nt.Text ;

by using FindControl method u can pick value of any control inside a grid view.

Hope it work...happy coding


这篇关于GridView FindControl方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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