Link to home
Start Free TrialLog in
Avatar of RobertNZana
RobertNZanaFlag for United States of America

asked on

Find control value on dynamically created gridview

I have a gridview which I dynamically bind in the page_load event. On the gridview1_RowDataBound event I want to check the value of the 2nd column (called "BookTitle"). How do I access this controls value since it's dynamically created? Code samples preferred. Thanks.
Avatar of Ross-C
Ross-C
Flag of United Kingdom of Great Britain and Northern Ireland image

might not have fully understood your question, let me know if i have but try the following.

put this into your row databound event

//Check row state of gridview whether it is data row or not
if (e.Row.RowType == DataControlRowType.DataRow)
        {
    TextBox MyTextBox= (TextBox )(e.Row.FindControl("MyTextBox"));
   
    MyTextBox.Text = "MyValue"; // or what ever you need to do with it
        {
Avatar of RobertNZana

ASKER

I know how to do that already. My problem is that the gridview is DYNAMICALLY created so I don't have a textbox, label, etc... with a predefined control name. So I cannot use FindControl by the name.
not how do that sure, sorry i misread your question. Would it be feasable in your app to hide the gridview in a panel instead of creating it dynamically.
Avatar of rajeeshmca
Have u created the gridview dynamically with the templatefield or the boundfield?

If it is templatefield, u can find the control as said by Ross-C.

If it is bound field u can find using the e.cells
Avatar of klakkas
klakkas

My friend, to add a handler in a dynamically created control, use the addHandler method.
Example:


Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Dim dt As New Data.DataTable
        dt.Columns.Add("Test1")
        dt.Columns.Add("Test2")
        Dim r As Data.DataRow = dt.NewRow
        r.Item("test1") = "a"
        dt.Rows.Add(r)

        Dim grd2 As New GridView
        grd2.ID = "grdMyDynamicGrid"
        grd2.DataSource = dt
        AddHandler grd2.RowDataBound, AddressOf GridView1_RowDataBound
        grd2.DataBind()
        FindControl("Form1").Controls.Add(grd2)
    End Sub


    Protected Sub Grd2_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
        Response.Write("Adding line in " + CType(sender, Control).ID + Environment.NewLine)
        'Do what you want here
     End Sub

Open in new window

So in the RowDataBound for this case how would I search for the value in the 2nd column? Please post a code sample.
ASKER CERTIFIED SOLUTION
Avatar of klakkas
klakkas

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Just use

e.cells[1].ToString();

in row databopund if u have used bound field