Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Finding a any control in a GridView

Posted on 2008-06-18
7
Medium Priority
?
2,621 Views
Last Modified: 2013-12-17
I have a GrivView (grd) that sits in a ContentPlaceHolder. I've add various controls - textboxes, radiobuttonlist, checkbox etc -  to grd dynamically.
For example.
When I added a text box and created a textbox of the form "txt" + someValue.ToString() so I can locate it later on.
How do I go about finding these controls so I can get the value from these textboxes or better yet how can I loop through the grd to find all the controls by their id and retrieve the control that has the textbox substring?

Thanks.


Thanks in advance.
0
Comment
Question by:mmassy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 5

Expert Comment

by:scgstuff
ID: 21815226
You would name the box whatever you want.  You do not need to dynamically change the textbox name.  You would find it by using:


TextBox txtTesting1 = GridView1.Rows[i].FindControl("txtAnything");
String myValue = txtTesting1.Text.ToString();

To loop through, you would do a for each row in datatable loop and find the value for each one.

Shawn
0
 

Author Comment

by:mmassy
ID: 21816877
That doesn't work. The html the textbox created has an id like
ctl00_main_grdSurvey_ctl06_txt2-1-c

The part that says txt2-1-c is the ID I gave the text box when I first load the grid. Remember I'm using a master page and my grid sits in the ContentPlaceHolder.
0
 
LVL 5

Accepted Solution

by:
scgstuff earned 2000 total points
ID: 21818514
I just tested this and it works great.  Give this a try.  I have added a textbox called txtName as a control in the datagrid.  Here is the code that I put in after the gridview1.databind event.....

        string strTesting = string.Empty;
        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
        {
            TextBox tb = (TextBox)GridView1.Rows[i].FindControl("txtName");
            strTesting = strTesting.ToString() + ", " + tb.Text.ToString();
        }
        Response.Write(strTesting);

This is how I populated the textbox when it first bound the data....

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if(e.Row.RowType == DataControlRowType.DataRow)
        {
            TextBox txtName = (TextBox)e.Row.FindControl("txtName");
            DataRowView drv = (DataRowView)e.Row.DataItem;
            txtName.Text = drv["customer_address"].ToString();
        }

    }

Let me know if you have any problems with that.

Shawn

0
Fill in the form and get your FREE NFR key NOW!

Veeam® is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

 

Author Comment

by:mmassy
ID: 21819011
How did u add the control... Did u add it at design time or did you add it using the RowDataBound event?
0
 
LVL 5

Expert Comment

by:scgstuff
ID: 21819037
At design...

here is the gridview on the aspx page:

<asp:GridView ID="GridView1" runat="server" OnRowDataBound="GridView1_RowDataBound">
        <Columns>
              <asp:TemplateField>
                    <ItemTemplate>
                          <asp:TextBox ID="txtname" runat="server"></asp:TextBox>
                    </ItemTemplate>
              </asp:TemplateField>
        </Columns>
</asp:GridView>

I have it autogenerate all the columns from the database, and use the tempate column as the first column just to test with.

Shawn
0
 

Author Comment

by:mmassy
ID: 21819095
At design time my grid has nothing but the columns tags and I'm creating adding the controls in the RowDataBoundEvent check the code example. I have to do it this way because I'm add other controls as well base the values I receive from a datatable. I can get then to populate on load but I can't  retrieve the value if I want to submit the form.
protected void grd_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        TextBox txt = new Textbox();
        txt.ID = "txtName";
        e.Rows.Cells[0].Control.Add(txt)
    }
}

Open in new window

0
 

Author Comment

by:mmassy
ID: 21840539
Ok I got it to work (been a few days, sorry for the delay)..

I changed the way I was creating the controls. Instead I used to template field ilike you (Shawn) did in design time but I added the controls I will need and set Visiblie="false".  In the RowDataBound event, which ever control is needed, FindControl and  set Visible="true" ;  See code snippet
Thanks Shawn,

Mark.

<asp:GridView ID="grd" runat="server">
  <Columns>
    <asp:BoundField DataField="Number" ></asp:BoundField>
    <asp:TemplateField>
      <ItemTemplate>
        <asp:Label ID="lblName" runat="server" Visible="false"></asp:Label>
        <asp:TextBox ID="txtName" runat="server" Visible="false" />
        <asp:RadioButtonList ID="rblName" runat="server" Visible="false"></asp:RadioButtonList>
        <asp:CheckBox ID="chkName" runat="server" Visible="false" />
      </ItemTemplate>
    </asp:TemplateField>
  </Columns>
</asp:GridView>
 
//This goes in the RowDataBound event
if(e.Row.RowType == DataControlRowType.DataRow)
{
    //If you need a text box and want to populate it
    if (INeedATextBox)
    {
        TextBox txtName = (TextBox)e.Row.FindControl("txtName");
        txtName.Visible = true;
        DataRowView drv = (DataRowView)e.Row.DataItem;
        txtName.Text = drv["customer_address"].ToString();
    }
    if (INeedACheckBox)
    {
        CheckBox chkName = (CheckBox).e.Row.FindControl("chkName");
        chkName.Visible = true;
        DataRowView drv = (DataRowView)e.Row.DataItem;
        chkName.Text = drv["customer_address"].ToString();
    }
}

Open in new window

0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Screencast - Getting to Know the Pipeline

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question