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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

programatically putting a user control into a content place holder

Status
Not open for further replies.

zma

Programmer
May 30, 2006
25
US
I am trying to put user controls in a placeholder when a buttonfield column in a gridview is clicked and runs a command. Right now, I am doing this.

<asp:content id="Content3" contentplaceholderid="place3" runat="server">
<% if dropdownlist1.selecteditem.value="tbldem" then %>
<prefix1:name1 runat="server" />
<% else %>
<prefix2:name3 runat="server" />
<% end if %>
</asp:content>

The problem is that a user control appears when any button is clicked, not only buttons in the gridview.

How can a user control be made to appear only when a button in the gridview is clicked?

Can this be done from the vb sub that runs when the gridview button is clicked?
 
Use the code-behind page, not inline ASP style coding. It will allow you to debug and maintain your code.

Look into the LoadControl() method.
 
Code:
protected override void OnInit(EventArgs e)
{
   Control name1 = Page.LoadControl("~/name1 .ascx");
   name1.Id = "foo";
   Control name3  = Page.LoadControl("~/name3 .ascx");
   name3.Id = "bar";

   name1.Visible = (dropdownlist1.SelectedValue == "tbldem");
   name3.Visible = !(dropdownlist1.SelectedValue == "tbldem");

   Content3.AddControl(name1);
   Content3.AddControl(name3);
}

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Tried this in a sub page_load:

dim testplace3 = LoadControl("test1.ascx")
content3.controls.add(testplace3)

but got the error that content3 was not declared.

This is what I am trying to put the user controls in.

<asp:content id="Content3" contentplaceholderid="place3" runat="server">
</asp:content>

What am I doing wrong?

 
Thank you jbenson001 and jmeckly.

My previous reply was sent before I saw what jmeckly said.

I am still getting an error saying 'content3'is not declared. This is the code.
------------------------------------------------------
sub page_load
dim name1 = Page.LoadControl("~/name1 .ascx")
name1.Id = "foo"
dim name3 = Page.LoadControl("~/name3 .ascx")
name3.Id = "bar"
if dropdownlist1.SelectedValue = "tbldem"
name1.Visible=true
end if
if dropdownlist1.SelectedValue <> "tbldem"
name3.Visible=true
end if
Content3.AddControl(name1)
Content3.AddControl(name3)
end sub
-------------------------------------------------------
Help with this would make me grateful.
 
this code needs to be in the Init event, not Load event.
your visible logic won't work because controls visible by default. instead just set the visibility property like this
Code:
name1.Visible = dropdownlist1.SelectedValue == "tbldem";
name3.Visible = dropdownlist1.SelectedValue != "tbldem";

as for 'Content3' either add the controls directly to the page. or use a asp:placeholder within Content3 and load the controls into that.
Code:
Page.Controls.Add(name1);
Page.Controls.Add(name3);
Code:
///markup
<asp:content id="Content3" ...>
   <asp:PlaceHolder id="Holder" runat="server" />
</asp:content>

///code behind
Holder.Controls.Add(name1);
Holder.Controls.Add(name3);

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Thanks for writing-no luck though. Keep seeing the control3 is not declared error
This is what I have in the aspx file now:

::::::
Towards the top are these
::::::

<%@ Register TagPrefix="prefix1" TagName="name1" Src="test1.ascx" %>
<%@ Register TagPrefix="prefix2" TagName="name2" Src="test2.ascx" %>
<%@ Register TagPrefix="prefix3" TagName="name3" Src="empty.ascx" %>

::::::
In a vb function that is called when a buttonfield in a grid is clicked-I hope to have different ascxs going into content3 depending on the value of a previously entered dropdownlist-here is what is in that function now:
::::::

label1.text="sample text"
dim testadd as usercontrol=loadcontrol("test1.ascx")
control3.controls.add(testadd) 'ERROR CONTENT3 NOT DECLARED

::::::
This is at the end of the aspx:
::::::

<asp:content id="Content3" contentplaceholderid="place3" runat="server" Visible="true">
</asp:content>

I hope that helps.
 
Code:
<asp:content id="Content3" contentplaceholderid="place3" runat="server" Visible="true">

<asp:Panel id="myPanel" runat="server"></asp:Panel>

</asp:content>

Code:
label1.text="sample text"
dim testadd as usercontrol=loadcontrol("test1.ascx")
myPanel.controls.add(testadd)
 
It works. Thank you very very much. How did you know a panel would work?

The contents of the ascx disappear from panel2 when the button in it is clicked. I am hoping to eventually have form elements in it corresponding to data table fields to error check and then insert or update that table with.

Is there a way to make text1.ascx stay in the panel after button1 is clicked?

This is what is in test1.ascx so far :

got here 1

<script language="vb" runat="server">
sub a(sender as object,e as eventargs)
response.write("*")
end sub
</script>

<input name="Text1" type="text" />
<asp:Button runat="server" Text="Button" id="Button1" onclick="a"/>
 
On page_init, the panel populates fine, but too soon.

I need to populate it when a buttonfield on a gridview is clicked. Which ascx goes in the panel depends on a dropdownlist value.

That seems to work except when a button is clicked in an ascx populating a panel, the whole panel goes away. I have tried several things so far with no luck yet.

I hope to use the aspx in that panel like an input form. The actual form tags are in the master page.

Thanks.
 
welcome to the wonderful world of asp.net life cycle! at a minimum you must load the controls onto the page during Page_Init event. then you may interact with those durning any other event.

if you go this route you will need to assign the control to a private field. you may even need to cast the value.
Code:
public class MyPage : Page
{
   private control name1, name3;

   protected override void OnInit(EventArgs e)
   {
      name1 = LoadControl("~/name1.ascx");
      name1.Id = "foo";
      name3 = LoadControl("~/name3.ascx");
      name1.Id = "bar";
   }

   //this could be any event
   private void DoSomething()
   {
       string clientId = name1.ClientID;
   }
}

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Ok how about this approach....

when a button is clicked....

populate a datagrid/gridview. One of the columns is a TemplateColumn and in your ItemTemplate you put your .ASCX...

This would totally eliminate your problems.
 
Just saw your post today. Gridview does not take templatecolumn so I hope templatefield is ok.

Queried for 1 record displayed in grid2, found it, bound it to grid3 with the ascx in it. Am seeing ascx file! It has 1 textbox and 1 button.

Hoping that ascx files can eventually act like input forms (form tags in master page) so trying to send a value. Hope to send many values or send 1 value and run query for that value in ascx but have no idea how to populate the ascx's textboxes etc if done that way.

Why is value of recid not in textbox?
How can I get which ascx shows up to depend on the value of a dropdownlist?


<asp:content id="Content3" contentplaceholderid="place3" runat="server" Visible="true">
<asp:panel runat="server" id="Panel2" BackColor="Teal" Visible="true">
<asp:Gridview runat="server" id="Gridview3">
<columns>
<asp:TemplateField HeaderText="col1">
<ItemTemplate>
<prefix1:name1 TestData='<%#Eval("recid")%>' ID="ctrlTester1" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</columns>
</asp:gridview>
</asp:panel>
</asp:content>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top