Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

If I DYNAMICALLY add user control how can I read back from it? 1

Status
Not open for further replies.

mb22

Programmer
Sep 4, 2002
258
US
If I add a user control (UC) at design time ... I have no problem reading from the controls in it ...say (text boxes) bcos I create a public property for each text box.

say in my UC i have a TextBox txtfname ...and a property p1_txtfname to read and write to it

Public Property p1_txtfname() As String
Get
Return txtfname.Text
End Get
Set(ByVal Value As String)
txtfname.Text = Value
End Set
End Property


Then in my HOST page suppose my UC id = Namebox... I can grab whatever is entered in the textbox by ....

myfNameInHostPage.Text = Namebox.p1_txtfname

..but this is bcos the UC id=Namebox is available at design time.

But suppose I add the UC dynamically at run-time like ...

Dim control As Control
control = Page.LoadControl("UserControls/Namebox.ascx")
Me.pageContentsCell.Controls.Add(control)

where pageContentsCell is say a table cell .... the page downloads OK ......

how do I read back the same
myfNameInHostPage.Text = Namebox.p1_txtfname

is it like
myfNameInHostPage.Text=control.ID("p1_txtfname") ?

since I don't know what ID will be assigned to the UC?
please help!
 
I know that there's a FindControl method (I'm using C# syntax) - I don't know if this would be of use to you ....
Steve
 
I've seen the FindControl but not sure how it may help ... I will checek it out ... if you have a c# example ..i can check that out too!
 
Dim control As Control
control = Page.LoadControl("UserControls/Namebox.ascx")
'When you create the control give it a name
control.Name = "MyControl"
Me.pageContentsCell.Controls.Add(control)



Then later use FindControl to get the control to access the text property. Remember to Cast the object returned in the findcontrol

Something like...
control = ctype(Me.pageContentsCell.Controls.FindControl.("MyControl"),Namebox)

control.p1_txtfname.Text


The syntax is probable wrong....


 
thanks .. ... i actually thought about giving it an ID. I will try your "NAME" suggestion tomorrow and let you know how it goes!
 
there is no NAME property when you create a control .. only an ID property ..and when I set it ... later the FindControl did not work ... as simple example might help!

I used a simple example and I set the id property as "Step1Control" .. when the page came up and I looked at the View Source ... the name of the UC added dynamically was like

<input name="_ctl0:Step1Control:fname" id="_ctl0_Step1Control_fname" type="text" size="25" />
 
I've had similar problems with using dinamically created controls. I had to create several controls in different containers and the best I could do was to loop throught all controls in the page until I found the one I need.

The problem is that the control might not be recreated at the moment you're looking for it (take a look at the Page's Life Cycle). To ease your job, take a look here and use the DynamicControlPlaceholder to add your controls to.

For more help, post back

--------------------------
"two wrongs don't make a right, but three lefts do" - the unknown sage
 
stsuing
the FindControl does not seem to work ... it can't find it when you submit the page again ... Moreover when I go into View/Source .. I do not see any control named "MyControl" ..even though I typed in

control.ID = "MyControl"

when I created the control and added dynamically!
 
the name of the control on the server, may not be the name of the control on the form. There are 2 ids to define a control.
1. control.ID: this is the name of the control on the server.
2. control.ClientID: this is the name of the control on the client's box.

if the control is just on the form, then the client id will match the server id. if the contorl is within another control (datagrid, user control) then the client id will not be the same as the server id. it will have a prefix appended to the server id.

Jason Meckley
Database Analyst
WITF
 
how can i read back what a user has entered into the user control. ... I'm getting frustrated trying to read back the property .... from dynamically adding the UC

 
if you don't know how the container of your control is named use a backtracking function to loop through all controls in the page until you find the control using your ID.
supposing your control is a textbox, use something like:
Code:
public string readValueFromControlID(string id, Control container)
{
   for (int i=0; i < container.Controls.count; i++)
   {
       if (((Control)container.Controls[i]).ID == id && container.Controls[i] is TextBox) return ((TextBox)container.Controls[i]).Text;
       else if (((Control)container.Controls[i]).HasControls) return readValueFromControlID(id, Control)container.Controls[i]).Controls)l
       else return "";
   }
}

now to get the value of a textbox with the id = "txtBox1" you would use readValueFromControlID("txtBox1", Page.Controls)

--------------------------
"two wrongs don't make a right, but three lefts do" - the unknown sage
 
Here is what I found I may be doing wrong .. but need to overcome!

I started trying to use a DataList as a navigation menu ... each item is linked to a HYPERLINK!!!! This appears to be my problem. The hyperlink DOES NOT trigger a POSTBACK .... .. it reloads the page from scratch again i believe ...that is why I am not able to read the value a user may have entered. To test .. I added a COMMAND button on my main page ... and when I click the command button it triggers a POSTBACK and I am able to get the value entered in the UC.

Having said that ... can I change my Hyperlink control to a command button instead ... but it may not look nice ... is there a suggestion for what to substitute for the hyperlink and let it trigger a POSTBACK!

here is my datalist control!

<asp:DataList id="MenuList" runat="server" CellPadding="0">
<HeaderTemplate>
<IMG alt="" src="Images/DeptHeader.gif">
</HeaderTemplate>
<SelectedItemTemplate>
<asp:HyperLink id="HyperLink2" runat="server" NavigateUrl='<%# "../default.aspx?MenuID=" &amp; DataBinder.Eval(Container.DataItem, "MenuID") &amp; "&amp;MenuIndex=" &amp; Container.ItemIndex %>' Text='<%# DataBinder.Eval(Container.DataItem, "MenuName") %>' CssClass="MenuSelected">
</asp:HyperLink>
</SelectedItemTemplate>
<FooterTemplate>
<IMG alt="" src="Images/DeptFooter.gif">
</FooterTemplate>
<ItemStyle BackColor="#FFFF80"></ItemStyle>
<ItemTemplate>
<asp:HyperLink id="HyperLink1" runat="server" NavigateUrl='<%# "../default.aspx?MenuID=" &amp; DataBinder.Eval(Container.DataItem, "MenuID") &amp; "&amp;MenuIndex=" &amp; Container.ItemIndex %>' Text='<%# DataBinder.Eval(Container.DataItem, "MenuName") %>' CssClass="MenuUnselected">
</asp:HyperLink>
</ItemTemplate>
<HeaderStyle BackColor="#FFFF80"></HeaderStyle>
</asp:DataList>
 
if you want your link to post back the form you need to use
NavigateUrl='javascript:document.forms[0].submit();';

--------------------------
"two wrongs don't make a right, but three lefts do" - the unknown sage
 
Hei DazzleD ... do three lefts really make a right? You got me spinning in the kitchen!

Seriously though ... what about my Request.QueryString items? Does that mean I have to now find a way to put them in hidden fields on the form before submitting or is there a better way?
 
Hey Guys

Instead of having to set the NavigateUrl to fire the postback just use a LinkButton instead of a HyperLink. This is designed exactly for this puporse and will render the NavigateUrl as javascript:__doPostback(this, '') which is the framework rendered method for firing the postback with the appropriate sender and event args objects.

Querystring items should still be in the querystring collection after postback as if you request WebForm1.aspx?Id=10 then the rendered form tag should be <form method="post" action="WebForm1.aspx?Id=10"> which means your querystring values are persisted.

Rob

Every gun that is made, every warship launched, every rocket fired, signifies in the final sense a theft from those who hunger and are not fed, those who are cold and are not clothed - Eisenhower 1953
 
yes... you could have a hidden field in your form where you can first set the value of the link, then submit the page.
NavigateUrl='javascript:document.getElementById("myHiddenField").value="<%# "../default.aspx?MenuID=" &amp; DataBinder.Eval(Container.DataItem, "MenuID") &amp; "&amp;MenuIndex=" &amp; Container.ItemIndex %>";document.forms[0].submit();';

--------------------------
"two wrongs don't make a right, but three lefts do" - the unknown sage
 
Ooops. Didn't read the question properly - my bad [blush] - sorry :)

Every gun that is made, every warship launched, every rocket fired, signifies in the final sense a theft from those who hunger and are not fed, those who are cold and are not clothed - Eisenhower 1953
 
DazzleD ... I'm confused ...

OK here is the hidden field I added to the UC in addtion to the Datalist already in the UC

<INPUT id=txtHidden runat="server" type=hidden size=17
value='<%# "../default.aspx?MenuID=" &amp;amp; DataBinder.Eval(Container.DataItem, "MenuID") &amp;amp; "&amp;amp;MenuIndex=" &amp;amp; Container.ItemIndex %>'>

Is this correct?

How do I now modify the HyperLink1 in the Item Template of the Datalist ?

<ItemTemplate>
<asp:HyperLink id="HyperLink1" runat="server" NavigateUrl='javascript:document.getElementById("txtHidden").value;document.forms[0].submit();' CssClass="MenuUnselected">
</asp:HyperLink>
</ItemTemplate>

i'm having trouble with the syxtax and the logic
 
the hidden field does not have any value at all. the value is set using javascript at client side by the hyperlink

you just need to add a hidden field
<INPUT id=txtHidden runat="server" type=hidden size=17
value=''>
and use this NavigationURL for your template:
NavigateUrl='javascript:document.getElementById("txtHidden").value="<%# "../default.aspx?MenuID=" &amp; DataBinder.Eval(Container.DataItem, "MenuID") &amp; "&amp;MenuIndex=" &amp; Container.ItemIndex %>";document.forms[0].submit();';

--------------------------
"two wrongs don't make a right, but three lefts do" - the unknown sage
 
this way you can read the txtHidden control to see what link was used and you also get the page posted back to server.

--------------------------
"two wrongs don't make a right, but three lefts do" - the unknown sage
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top