I have a Gridview that has a Placeholder that was added to the 2nd col, ColB. That placeholder has an undetermined number of textbox written to it on Edit clicked, based on the number of carriage returns that exist in another col, ColA.
Here is the code that places the textboxes into the placeholder....
But I am not getting any rows returned from the gridview. The gridview has 100's of rows. The Textbox(s) I am looking for are inside of the placeholder.
The problem was much more complicated than I ever thought. And I am posting the details here so that others can benefit from my experience.
I am using two Master pages, one within the other, in addtion the texboxes are being created dynamically, so they don't exist in the control heirarchy.
In order to even get close I had to use a FindControl all the way from the top as displayed in the code below.....
Dim MainContent As ContentPlaceHolder = CType(Page.Master.FindControl("ContentPlaceHolder1"), ContentPlaceHolder)
Dim MainContent2 As ContentPlaceHolder = CType(MainContent.FindControl("ContentPlaceHolder2"), ContentPlaceHolder)
Dim GRD4 As GridView = CType(MainContent2.FindControl("Gridview4"), GridView)
Dim PLH1 As PlaceHolder = CType(GRD4.Rows(1).FindControl("PlaceHolder1"), PlaceHolder)
Dim RPTER As Repeater = CType(GRD4.Rows(1).FindControl("Repeater1"), Repeater)
The Repeater was just another attempt, thinking that the placeholder might have been the problem.
Because I wasn't creating the text box(s) in the Page Init event they were not able to exist in all postbacks. I couldn't because I didn't know how many I needed until the user selected a given row from the gridview with thousands of records, I show 50 per
page.
Great article here from Scott Mitchell explains the issue. If you can't create your controls in Page Init, better think of another way.
My solution was to pass everything to a popup page. By the time the use clicked edit I new how many textboxes I would need to show, so I could build them in the PAge Init event. You populate them at anytime, provided they are built first. Actually, check
the article there are some limitations.
Hope this helps someone else.
Marked as answer by march11 on Sep 30, 2011 08:37 PM
I am in a similar situation now. I have a grid with list of employees.
When i click an employee name i intend to redirect to another page which will show the values in a grid view. I managed to do that. But the problem is , there is an update button in the grid showing the details and when i edit and click the update button
i am not getting the current values from the textbox inside the grid.
My guess here is that you are databinding the old data when you return to the page. If I recall I think I used AJAX and an update panel to refresh the Gridview with the new record info form the popup.
I would recommend reading over the link the answer post, it was very helpful.
march11
Contributor
2981 Points
1350 Posts
How to retrieve textbox values from within Placeholder in Gridview
Sep 26, 2011 08:01 PM|LINK
I have a Gridview that has a Placeholder that was added to the 2nd col, ColB. That placeholder has an undetermined number of textbox written to it on Edit clicked, based on the number of carriage returns that exist in another col, ColA.
Here is the code that places the textboxes into the placeholder....
txtB.ID = "txtBEdit" & I.ToString("D2") ph.Controls.Add(txtB)I would like to read the values in the textboxes on Update link click event, Row Updating.
Could someone provide some code to retrive the value of these textboxes in the placeholder?
nicksoft
Participant
802 Points
216 Posts
Re: How to retrieve textbox values from within Placeholder in Gridview
Sep 26, 2011 08:19 PM|LINK
Within the RowUpdating method, you might wants to cast the control ID and then grab the value from there:-->
Dim row = GridView1.Rows(e.RowIndex)
dt.Rows(row.DataItemIndex)("txtB") = DirectCast(GridView1.Rows(e.RowIndex).FindControl("txtB"), TextBox).Text
Please Mark the detail as Answered if it resolve your issue.
Thanks,
Nick
march11
Contributor
2981 Points
1350 Posts
Re: How to retrieve textbox values from within Placeholder in Gridview
Sep 26, 2011 08:31 PM|LINK
Solved that error.
But I am not getting any rows returned from the gridview. The gridview has 100's of rows. The Textbox(s) I am looking for are inside of the placeholder.
the error I get is....
There is no row at position 0.
nicksoft
Participant
802 Points
216 Posts
Re: How to retrieve textbox values from within Placeholder in Gridview
Sep 26, 2011 08:49 PM|LINK
can you provide some more code? or Do you have the Gridview available in seesion variable or somewhere?
Because what is going on is when the gridview goes to rowupdating event, i am not sure how your application preserving the Gridview data.
march11
Contributor
2981 Points
1350 Posts
Re: How to retrieve textbox values from within Placeholder in Gridview
Sep 30, 2011 08:37 PM|LINK
The problem was much more complicated than I ever thought. And I am posting the details here so that others can benefit from my experience.
I am using two Master pages, one within the other, in addtion the texboxes are being created dynamically, so they don't exist in the control heirarchy.
In order to even get close I had to use a FindControl all the way from the top as displayed in the code below.....
Dim MainContent As ContentPlaceHolder = CType(Page.Master.FindControl("ContentPlaceHolder1"), ContentPlaceHolder) Dim MainContent2 As ContentPlaceHolder = CType(MainContent.FindControl("ContentPlaceHolder2"), ContentPlaceHolder) Dim GRD4 As GridView = CType(MainContent2.FindControl("Gridview4"), GridView) Dim PLH1 As PlaceHolder = CType(GRD4.Rows(1).FindControl("PlaceHolder1"), PlaceHolder) Dim RPTER As Repeater = CType(GRD4.Rows(1).FindControl("Repeater1"), Repeater)The Repeater was just another attempt, thinking that the placeholder might have been the problem.
Because I wasn't creating the text box(s) in the Page Init event they were not able to exist in all postbacks. I couldn't because I didn't know how many I needed until the user selected a given row from the gridview with thousands of records, I show 50 per page.
Great article here from Scott Mitchell explains the issue. If you can't create your controls in Page Init, better think of another way.
http://msdn.microsoft.com/en-us/library/aa479330.aspx
My solution was to pass everything to a popup page. By the time the use clicked edit I new how many textboxes I would need to show, so I could build them in the PAge Init event. You populate them at anytime, provided they are built first. Actually, check the article there are some limitations.
Hope this helps someone else.
arjun04
Member
2 Points
1 Post
Re: How to retrieve textbox values from within Placeholder in Gridview
Dec 09, 2012 05:12 AM|LINK
I am in a similar situation now. I have a grid with list of employees.
When i click an employee name i intend to redirect to another page which will show the values in a grid view. I managed to do that. But the problem is , there is an update button in the grid showing the details and when i edit and click the update button i am not getting the current values from the textbox inside the grid.
I used the code shoen below
int empID;
string age, address, name;
Button btnUpdateUser = sender as Button;
TextBox tbName, tbAge, tbAddress;
SqlConnection sqlConnect;
try
{
tbName = (TextBox)GridView2.Rows[0].FindControl("txtName");
tbAge = (TextBox)GridView2.Rows[0].FindControl("txtAge");
tbAddress = (TextBox)GridView2.Rows[0].FindControl("txtAddress");
sqlConnect = new SqlConnection("server=WINNER-PC\\SQLEXPRESS;integrated security=true;initial catalog=Test");
if (sqlConnect.State == ConnectionState.Closed)
sqlConnect.Open();
empID = Convert.ToInt32(btnUpdateUser.CommandArgument);
name = tbName.Text;
age = tbAge.Text;
address = tbAddress.Text;
SqlCommand sqlCommand = new SqlCommand("UpdateEmployee", sqlConnect);
sqlCommand.CommandType = CommandType.StoredProcedure;
sqlCommand.Parameters.AddWithValue("@ID", empID);
sqlCommand.Parameters.AddWithValue("@Name", name);
sqlCommand.Parameters.AddWithValue("@Age", age);
sqlCommand.Parameters.AddWithValue("@Address", address);
sqlCommand.ExecuteNonQuery();
Response.Redirect("Employees.aspx", false);
}
catch (Exception ex)
{
throw ex;
}
march11
Contributor
2981 Points
1350 Posts
Re: How to retrieve textbox values from within Placeholder in Gridview
Dec 10, 2012 12:07 PM|LINK
My guess here is that you are databinding the old data when you return to the page. If I recall I think I used AJAX and an update panel to refresh the Gridview with the new record info form the popup.
I would recommend reading over the link the answer post, it was very helpful.