Click here to Skip to main content
16,019,174 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
A FormView question:

i user :
C#
FormView1.ItemTemplate = LoadTemplate("FormView.ascx");

FormView.ascx:

<asp:textbox id="textbox" runat="server" text="This is a test" xmlns:asp="#unknown"></asp:textbox>


C#
protected void FormView1_DataBound(object sender, EventArgs e)
{
  // how to get thi control "textbox"  by FindControl?

}


thank you
Posted
Updated 20-May-10 8:16am
v2

i write a function to find a control in formview:

#region       
        protected Control GetControlOfFormView(FormView fv, string strControlID)
        {
            Control ctl = new Control();
            FormViewRow fvr = fv.Row;
            TableCell tc = new TableCell();
            Control CellCtl = new Control();
            int nCell,nCellCtl;
            int j, k;
            nCell = fvr.Cells.Count;
            for (j = 0; j < nCell; j++)
            {
                tc = fvr.Cells[j];
                nCellCtl = tc.Controls.Count;
                for (k = 0; k < nCellCtl; k++)
                {
                    CellCtl = tc.Controls[k];
                    ctl = CellCtl.FindControl(strControlID);
                    if (ctl != null)
                    {
                        k = nCellCtl;
                        j = nCell;
                    }
                }
            }
            return ctl;
        }
        #endregion


use it :
protected void FormView1_DataBound(object sender, EventArgs e)
{
   DataRowView row = (DataRowView)FormView1.DataItem;
   TextBox tb = null;

   tb = (TextBox)GetControlOfFormView(FormView1, "textbox");
   tb.Text = row["Bsf_FormID"].ToString();
}


thanks erveryone, if have good idea ,i want to know how you do it.thanks
 
Share this answer
 
The sender is nothing but the Formview control it self just loop through the items and get the text box

Regards
Vinay
 
Share this answer
 
I want to set a value to id="textbox" ,how to do ?
 
Share this answer
 
Comments
kumardevarapalli 20-May-10 5:51am    
after finding the text box control just parse it like the below code

Dim txtNew As New TextBox
and assign the text box what u have found and set the value

Regards
Vinay
the question is that I don't know how to get the textbox control, thank you for your help
 
Share this answer
 
Comments
kumardevarapalli 20-May-10 6:33am    
hay the sender itself is the formview rite....just loop through the formview items and find the text box then assign the value
kumardevarapalli 20-May-10 6:45am    
sory due i am working on it if got i let u no
Check this code

VB
Dim frm As FormView = sender
       If (frm.DataItem IsNot Nothing) Then
           Dim txt As TextBox = frm.Controls.Item(0).FindControl("TextBox1")
           txt.Text = "Test" & frm.DataItemIndex
       End If
 
Share this answer
 
kumardevarapalli,thank you for your help.

it's wrong :

VB
Dim txt As TextBox = frm.Controls.Item(0).FindControl("TextBox1")



err message:
VB
System.Web.UI.ControlCollection' does not contain a definition for 'Item' and no extension method 'Item' accepting a first argument of type 'System.Web.UI.ControlCollection' could be found (are you missing a using directive or an assembly reference?)


need your help ,thanks
 
Share this answer
 

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