Click here to Skip to main content
15,914,225 members
Home / Discussions / ASP.NET
   

ASP.NET

 
GeneralRe: wanna show resume(word file) in aspx page. Pin
yazali7-Jun-09 23:27
yazali7-Jun-09 23:27 
QuestionFileIOPermission Pin
Matt Cavanagh7-Jun-09 21:14
Matt Cavanagh7-Jun-09 21:14 
AnswerRe: FileIOPermission Pin
Christian Graus7-Jun-09 21:32
protectorChristian Graus7-Jun-09 21:32 
GeneralRe: FileIOPermission Pin
Matt Cavanagh7-Jun-09 22:12
Matt Cavanagh7-Jun-09 22:12 
QuestionGenerate PDF file Pin
vnsraj7-Jun-09 20:43
vnsraj7-Jun-09 20:43 
AnswerRe: Generate PDF file Pin
Abhijit Jana7-Jun-09 20:48
professionalAbhijit Jana7-Jun-09 20:48 
AnswerRe: Generate PDF file Pin
Abhishek Sur7-Jun-09 21:11
professionalAbhishek Sur7-Jun-09 21:11 
QuestionPlease help with Viewstate and nested webcontrol !!! Pin
dwfresh7-Jun-09 20:07
dwfresh7-Jun-09 20:07 
HI, i am quite frustrated trying to retrieve values from nested web control inside a composite control .
I have stripped the control(s) down to barebones to try to make this explanation as easy as possible. if anyone can help me out it would be greatly appreciated!!

(This code exists entirely in codebehind, nothing exists in the aspx pages)

In my aspx.cs page i declare a composite(Parent) control. Within this Parent control is a public property that is an IList of 'ChildControl's.

The 'ChildControl' is just 2 texboxes. When a user clicks a button in the Parent control, i add a new 'ChildControl' to the 'ChildControlList' property of the composite (Parent), and render it. Seems pretty basic.

When user clicks on btnGetValues (Button) in the aspx page, i want to grab all the values of the TextBoxes in the ParentControls 'ChildControlList' property.

From the dozens of forums that I've read on this, it appears i need to add the controls in the OnInit Property instead of the CreateChildControls property to ensure Viewstate. I've tried both but neither have worked.

When I click on the 'Add Child Control' button for the first time, everything looks fine, and 2 new textboxes are rendered. However, if I enter text into those textboxes, and try to retrieve the values, the ChildControl list is always emtpy. (or if I try to add additional childcontrols to the page, the ChildCOntrolList Count is always 0, so never get more than 1 item in the ChildCOntrolList.)

please help me.

Below is the relevant code:

ASPX.CS

public partial class _Default : System.Web.UI.Page
    {
        private ParentWebControl parentControl;
        private Button btnGetValues;
        protected void Page_Load(object sender, EventArgs e)
        {    }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            HtmlForm form = (HtmlForm)Page.FindControl("form1");
            btnGetValues = new Button();
            btnGetValues.Text = "Get values (aspx)";
            parentControl = new ParentWebControl();

            btnGetValues.ID = "btnGetValues";
            parentControl.ID = "parentCompositeControl";
            btnGetValues.Click += btnGetValues_Click;

            form.Controls.Add(btnGetValues);
            form.Controls.Add(parentControl);
        }

        void btnGetValues_Click(object sender, EventArgs args)
        {
            List <ChildControl> subChildren = parentControl.ChildControlList; 

        // this is always emtpy  !!!!     
            foreach (ChildControl child in subChildren)
            {
                string value1 = child.TextBox1.Text;
                string value2 = child.TextBox2.Text;
            }
        }
    }

 

PARENT CONTROL:

 public class ParentWebControl : CompositeControl
    {
        private Button addChildControl;
        private Label label;
        private List<ChildControl> childControlList;

        public ParentWebControl()
        {
            if(childControlList==null)
             childControlList = new List<ChildControl>();
        }

        public List<ChildControl> ChildControlList
        {
            get
            {
                EnsureChildControls();
                return childControlList;
            }
            set
            {
                EnsureChildControls();
                childControlList = value;
            }
        }

        private void  addChildControl_Click(object source, EventArgs e)
        {
            ChildControl newChild = new ChildControl();
            newChild.ID = "newChild";
            childControlList.Add(newChild);
        }

        protected override void OnInit(EventArgs e)
        {

            addChildControl = new Button();
            addChildControl.ID = "addChildControl";
            addChildControl.Click += addChildControl_Click;

            addChildControl.Text = "Add Child Control";

            label = new Label();
            label.ID = "myLabel";
            label.Text = "some label";

            Controls.Add(addChildControl);
            int id = 0;
            foreach (ChildControl control in ChildControlList)
            {
                control.ID = "childMemberControl" + id++;
                Controls.Add(control);
            }
            Controls.Add(label);
            base.OnInit(e);
        }       


        protected override void Render(HtmlTextWriter writer)
        {
            writer.RenderBeginTag(HtmlTextWriterTag.Div);
            addChildControl.RenderControl(writer);
            foreach (ChildControl control in ChildControlList)
            {
                control.RenderControl(writer);
            }
            label.RenderControl(writer);
            writer.RenderEndTag();
        }
    }

 

CHILDCONTROL:

public class ChildControl : WebControl
    {
        private TextBox textBox1;
        private TextBox textbox2;       

        public TextBox TextBox1
        {
            get
            {
                EnsureChildControls();
                return textBox1;
            }
            set
            {
                EnsureChildControls();
                textBox1 = value;
            }
        }

        public TextBox TextBox2
        {
            get
            {
                EnsureChildControls();
                return textbox2;
            }
            set
            {
                EnsureChildControls();
                textbox2 = value;
            }
        }

        public ChildControl()
        {
            TextBox1 = new TextBox();
            TextBox2 = new TextBox();
        }

        protected override void OnInit(EventArgs e)
        {
           
            TextBox1 = new TextBox();
            TextBox1.ID = "textBox1";
            TextBox2 = new TextBox();
             TextBox2.ID = "textBox2";
            Controls.Add(TextBox1);
            Controls.Add(TextBox2);
            base.OnInit(e);
        }

        protected override void Render(HtmlTextWriter writer)
        {
            writer.RenderBeginTag(HtmlTextWriterTag.Div);
            TextBox1.RenderControl(writer);
            TextBox2.RenderControl(writer);
            writer.RenderEndTag();
        }
    }


----------

Can anyone please set me in the right direction, i have spent almost a whole day on trying to achieve this.

Thanks so much!!



dwfresh
AnswerRe: Please help with Viewstate and nested webcontrol !!! Pin
Christian Graus7-Jun-09 20:32
protectorChristian Graus7-Jun-09 20:32 
GeneralRe: Please help with Viewstate and nested webcontrol !!! Pin
Abhijit Jana7-Jun-09 20:49
professionalAbhijit Jana7-Jun-09 20:49 
GeneralRe: Please help with Viewstate and nested webcontrol !!! Pin
dwfresh8-Jun-09 5:29
dwfresh8-Jun-09 5:29 
Questioncheck if record exist Pin
hahii7-Jun-09 18:33
hahii7-Jun-09 18:33 
AnswerRe: check if record exist Pin
Christian Graus7-Jun-09 19:02
protectorChristian Graus7-Jun-09 19:02 
GeneralCustom web controls Pin
Juvil John7-Jun-09 16:53
Juvil John7-Jun-09 16:53 
Questionhow to save gridview contents in cache at runtime Pin
ashutosh_karna7-Jun-09 11:33
ashutosh_karna7-Jun-09 11:33 
AnswerRe: how to save gridview contents in cache at runtime Pin
Christian Graus7-Jun-09 12:06
protectorChristian Graus7-Jun-09 12:06 
QuestionHow to show image from ms access to gridview Pin
Pratheepa7-Jun-09 3:26
Pratheepa7-Jun-09 3:26 
AnswerRe: How to show image from ms access to gridview Pin
Abhijit Jana7-Jun-09 3:42
professionalAbhijit Jana7-Jun-09 3:42 
GeneralRe: How to show image from ms access to gridview Pin
Pratheepa7-Jun-09 3:54
Pratheepa7-Jun-09 3:54 
GeneralRe: How to show image from ms access to gridview Pin
Christian Graus7-Jun-09 11:10
protectorChristian Graus7-Jun-09 11:10 
GeneralRe: How to show image from ms access to gridview Pin
Abhijit Jana7-Jun-09 17:40
professionalAbhijit Jana7-Jun-09 17:40 
AnswerRe: How to show image from ms access to gridview Pin
Matt Cavanagh7-Jun-09 22:39
Matt Cavanagh7-Jun-09 22:39 
GeneralRe: How to show image from ms access to gridview Pin
Pratheepa13-Jun-09 21:10
Pratheepa13-Jun-09 21:10 
QuestionChess Engine Pin
owolabi Victor6-Jun-09 18:51
owolabi Victor6-Jun-09 18:51 
AnswerRe: Chess Engine Pin
Abhijit Jana6-Jun-09 19:12
professionalAbhijit Jana6-Jun-09 19:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.