在GridView中全局查找控件 [英] finding a control in a gridview globally

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

问题描述



在全球范围内,我想找到一个位于gridview中的文本框,以便可以在任何事件中使用该变量.

.cs文件中有可能吗

如果不是,那么在.aspx页面中使用javascript找到文本框后,如何将文本框的变量检索为函数.

我试图在.cs文件中找到一个控件,但找不到它.
请提供解决方案.

Hi,

Globally i want to find a textbox which is in a gridview such that i can use that variable in any of the events.

Is that possible in .cs file
or
if not how can we retreive the variable of a textbox in to a function after finding the textbox using javascript in .aspx page .

I tried to find a control in .cs file but I couldn''t get it.
Please, give the solution.

推荐答案

您可能必须递归调用FindControl方法.

您可以为此使用以下扩展方法.
You may have to call the FindControl method recursively.

You can use the following extension method for that.
public static class Extensions
    {
        // Searches recursively in this control to find a control with the name specified.
        public static Control FindControlR(this Control root, string id)
        {
            Control controlFound;
            if (root != null)
            {
                controlFound = root.FindControl(id);
                if (controlFound != null)
                {
                    return controlFound;
                }
                foreach (Control c in root.Controls)
                {
                    controlFound = c.FindControlR(id);
                    if (controlFound != null)
                    {
                        return controlFound;
                    }
                }
            }
            return null;
        }
}



您可以像
这样使用此方法



You can use this method like

TextBox txtbox = gridview1.FindControlR("TxtCtrlName") as TextBox;


您可以遍历整个网格视图和每个单元格中的访问控件,但是为什么不想让特定的行成为您要处理的行,为什么要任意行和列?
You can iterate over your whole gridview and access controls in each cell, but why would you not want a specific row to be the one you work with, why do you want an arbitrary row and column ?


hi,

您可以使用以下扩展方法.

在HTML页面中


you can use the following extension method for that.

In Html Page
lt;asp:RadioButtonList runat=server ID=RadioButtonList1 AutoPostBack=true OnSelectedIndexChanged="RadioButtonList1_OnSelectedIndexChanged" >
         <asp:ListItem Value="1">Value1</asp:ListItem>
         <asp:ListItem Value="2">Value2</asp:ListItem>
         <asp:ListItem Value="3">Value3</asp:ListItem>
         </asp:RadioButtonList><br />
  <asp:GridView runat=server ID=GridView3  CellPadding=3 CellSpacing=0 AutoGenerateColumns=false>
  <Columns>
   <asp:TemplateField HeaderText="Column1">
     <ItemTemplate>
        <asp:TextBox runat=server ID=TextBox1 Text="1" />
       <asp:TextBox runat=server ID=TextBox2  Text="2"/>
      <asp:TextBox runat=server ID=TextBox3   Text="3"/>
     </ItemTemplate>
   </asp:TemplateField>
  <asp:BoundField DataField="Column1" HeaderText="Column1" />
     <asp:BoundField DataField="Column2" HeaderText="Column2" />
     <asp:BoundField DataField="Column3" HeaderText="Column3" />
  </Columns>
  </asp:GridView>



在代码中:



In code:

<pre lang="cs">protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        BindGridView();
    }
}


private void BindGridView()
        {
            DataTable datatable = new DataTable();
            datatable.Columns.Add("Column1", typeof(System.String));
            datatable.Columns.Add("Column2", typeof(System.String));
            datatable.Columns.Add("Column3", typeof(System.String));
            datatable.Rows.Add("1", "2", "3");
            datatable.Rows.Add("4", "5", "6");
            this.GridView3.DataSource = datatable;
            this.GridView3.DataBind();
        }
        protected void RadioButtonList1_OnSelectedIndexChanged(object sender, EventArgs e)
        {
            string value = "";
            for (int i = 0; i < RadioButtonList1.Items.Count; i++)
            {
                if (RadioButtonList1.Items[i].Selected)
                {
                    value = RadioButtonList1.Items[i].Value;
                    break;
                }
            }
            foreach (GridViewRow GridViewRow1 in this.GridView3.Rows)
            {
                TextBox TextBox1 = (TextBox)GridViewRow1.FindControl("TextBox1");
                TextBox TextBox2 = (TextBox)GridViewRow1.FindControl("TextBox2");
                TextBox TextBox3 = (TextBox)GridViewRow1.FindControl("TextBox3");
                switch (value)
                {
                    case "1"://
                        TextBox1.Visible = true;
                        TextBox2.Visible = false;
                        TextBox3.Visible = false;
                        break;
                    case "2":
                        TextBox1.Visible = false;
                        TextBox2.Visible = true;
                        TextBox3.Visible = false;
                        break;
                    case "3":
                        TextBox1.Visible = false;
                        TextBox2.Visible = false;
                        TextBox3.Visible = true;
                        break;
                }
            }            
        }


您可以尝试这些代码.希望对您有帮助.


you can try these code. Hope help you.


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

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