Click here to Skip to main content
16,021,172 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello every one,

I have a gridview with following definition

C#
<asp:GridView ID="timegrid" runat="server" AutoGenerateColumns="false" ShowHeader="true"      OnRowDataBound="timegrid_RowDataBound" OnRowCreated="timegrid_RowCreated">
 <Columns> 
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
  <asp:TemplateField>
   <ItemTemplate>
     <asp:PlaceHolder ID="place1" runat="server"></asp:PlaceHolder>\\5 fields of this type
   </ItemTemplate>
  </asp:TemplateField>                          
 </Columns>
</asp:GridView>


and generated row dynamically with textbox and placed it in this place holder as follows:

C#
PlaceHolder pc1 = ((PlaceHolder)e.Row.Cells[4].Controls[1]);
TextBox tb = new TextBox();
tb.ID = "tb_4" ;
tb.Width = 50;
tb.Style.Add("text-align", "center");
pc1.Controls.Add(tb);


Its working fine as expected.

Now i have one button located outside gridview. On click of this button i want to access that controls which were generated dynamically as well as other gridview cells values also,i am not able to access it. I tried like

C#
TextBox textBox = timegrid.Rows[0].Cells[4].FindControl("tab_4") as TextBox;
string temp = textBox.Text;\\Object reference error


Here i have only single row with multiple columns.I am getting error at highlighted line.Does this controls looses on postback(on button click)?If so,how to avoid that?

How to proceed? Any suggestions please..?
Posted
Updated 17-Jun-13 19:33pm
v2

1 solution

Below are the links for similar kind of problem, which are marked solved. Please go through these links. Hope it will help.

http://www.codeproject.com/Answers/599178/access-FindControl-plusdynamicallypluscreatedplusc#answer1

http://www.codeproject.com/Answers/596883/howpluscanplusiplusgetplusenteredplustextboxesplus#answer1

http://www.codeproject.com/Answers/594571/Ipluswantplustoplusretrieveplusandplussaveplusthep#answer2
 
Share this answer
 
Comments
Sejal Rabari 18-Jun-13 2:30am    
If i will try to access controls at page_load,then what when there are no controls generated? The criteria is,after generating controls i want to enter values of this dynamic controls into database.How to implementthis in page_load and button click?
Sejal Rabari 18-Jun-13 2:51am    
can you suggest me if i want to access textbox on button click,which is generated dynamically,for which id is tab_4 how to do this using

string = Request["tab_4"]; this is showing me errors. Would you please guide me some more?
Sejal Rabari 18-Jun-13 2:55am    
you answered somewhere like:

if (c is CheckBox)
{
var chkval = "";
if (Request[c.UniqueID] != null)
{
chkval = Request[c.UniqueID];
}
}
then what is c here?how to define it?
Mahesh Bailwal 18-Jun-13 3:13am    
Step1.In your page load you need to recreate you dynamic grid.
Step2. Below is sample code what you need to do in button click.

protected void btn_Click(object sender, EventArgs e)
{
for (var row = 0; row < timegrid.Rows.Count; row++)
{
TextBox txt = (TextBox)timegrid.Rows[row].Cells[4].Controls[0];
string newVal = Request[txt.UniqueID];
}
}
Let me know if it does not works.
Member 11207670 15-Jun-17 9:52am    
solved my problem, thanks a lot...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900