Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Re: Dynamic controls based on user action

Reply
Thread Tools

Re: Dynamic controls based on user action

 
 
Eliyahu Goldin
Guest
Posts: n/a
 
      12-13-2006
Nadav,

Although there are scenarios when it is appropriate to create controls
dynamically, it fits better into object-oriented paradigm to use normal
static controls and to operate their properties to hide/show them as needed.
Controls with the Visible property set to 'false' don't get rendered to the
client, which is the same effect as not creating them in the first place.

If you do decide on creating controls dynamically, there is nothing
particularly wrong with doing it in the button event handlers. You will need
to re-create these controls on every postback where you need to access
their properties.

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]


"Nadav Popplewell" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hi everybody,
>
> I've got a question about adding dynamic controls to a asp.net net.
>
> As I understand it, dynamic controls need to be added in the page_init
> handler,
> otherwise there are problems with the controls view state and event
> handling.
>
> What do I do if the dynamic controls that need to be added depend on the
> user interaction with the web page?
>
> For example,
> If the user pressed button A I want to add Control X.
> if the user pressed button B I want to remove Control X and add Control Y.
> etc...
>
> Do I need to update the application's state and the repost (or do a
> Transfer) from the buttons' event handlers and then read the application's
> state in the page_init and add the appropriate controls?
>
> Thanks,
> Nadav



 
Reply With Quote
 
 
 
 
Eliyahu Goldin
Guest
Posts: n/a
 
      12-13-2006
Nadav,

Instead of going into this balagan with dynamic controls, why can't you
simply have both A and B on the page, one next to another, and make only one
of them visible? Several times I implemented tab control functionality with
this technique.

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]


"Nadav Popplewell" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hi Eliyahu,
>
> I've just did some testing and there are some problems with creating the
> controls
> and the buttons handlers.
>
> The problem is that if I created control A and now in the postback I want
> to
> display control B INSTEAD of control A, I need to remove control A and add
> Control B.
> Now, what happens is that if controls A & B are placed inside a control
> (placeholder or panel) after removing A and adding B the controlID that B
> will have is NOT the same as the controlID that B will have when it is
> created in the Page_Init().
>
> So what happens is, that after control B is created, it does not respond
> to
> events in the first postback (I assume it will also not save it's
> viewstate).
>
> Basiclly, what I want is a Tab Control functionallity with some way of
> testing in page load of the contained controls if they need to perform
> DataBinding or not
> (and of course, if you change tabs then the new tab needs to perform
> databinding).
>
> I'm trying to implement this using MultiView, but so far I have some
> problems.
>
> Thanks,
>
> Nadav
>



 
Reply With Quote
 
 
 
 
Steven Cheng[MSFT]
Guest
Posts: n/a
 
      12-14-2006
Hello Nadav,

For your scenario, you want to dyanmically present some controls on the
webform according to some user actions(like button click). Based on my
experience, there are two means to do this:

1. Put all the possible controls statically on the webform at design-time,
and hidden all the controls by default. At runtime, show the certain
control according to the user's actions(in postback events).

2. You can still programmtically create those dynamic controls. However,
since yoru dynamic controls are depend on some user interactive operations,
you need to add some variables to persist the current status (according to
user's action). for example, you can store the status in SessionState so
that you can check the value in Page_init event and create the proper
dynamic control accordingly. e.g.

========aspx=================
.............
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="btn1" runat="server" Text="Show TextBox1"
OnClick="btn1_Click" />
<asp:Button ID="btn2" runat="server" Text="Show TextBox2"
OnClick="btn2_Click" />
<asp:Button ID="btnSubmit" runat="server" Text="Submit Button"
OnClick="btnSubmit_Click" /><br />
<br />
<asplaceHolder ID="PlaceHolder1" runat="server"></asplaceHolder>

</div>
</form>
</body>


======code behind========
public partial class Part1_dynamiccontrol : System.Web.UI.Page
{
TextBox _txt1;
TextBox _txt2;

protected void Page_Init(object sender, EventArgs e)
{
if (!IsPostBack)
{
Session["controlid"] = 1;
}

CreateDynamicControls();
}
protected void Page_Load(object sender, EventArgs e)
{

}

private void CreateDynamicControls()
{
PlaceHolder1.Controls.Clear();


int cid = (int)Session["controlid"];



if (cid == 1)
{
_txt1 = new TextBox();
_txt1.ID = "txt1";
PlaceHolder1.Controls.Add(_txt1);

}
else
{
_txt2 = new TextBox();
_txt2.ID = "txt2";
PlaceHolder1.Controls.Add(_txt2);

}



}

protected void btn1_Click(object sender, EventArgs e)
{
Session["controlid"] = 1;
CreateDynamicControls();


}
protected void btn2_Click(object sender, EventArgs e)
{
Session["controlid"] = 2;
CreateDynamicControls();
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
if (_txt1 != null)
{
Response.Write("<br/>txt1" + _txt1.Text);
}

if (_txt2 != null)
{
Response.Write("<br/>txt2" + _txt2.Text);
}
}
}
=============================

Hope this helps.


Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead



This posting is provided "AS IS" with no warranties, and confers no rights.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Dynamic Controls created by Dynamic Controls Ronald ASP .Net 2 01-17-2006 12:44 AM
Dynamic Events from Dynamic Controls on a User Control - Guess What? Broken! mytestemailaccount@gmail.com ASP .Net 5 03-23-2005 05:06 PM
Dynamic Controls, Placeholder, Retrieve Information from Dynamic Controls Denny Smolinski via .NET 247 ASP .Net Web Controls 1 05-04-2004 09:15 PM
How to handle Dynamic Controls within User Controls Michael Ramey ASP .Net 2 02-04-2004 06:46 PM
Struts Static Action Form vs Dyanamic Action Form Joe Bloggs Java 1 08-03-2003 02:30 AM



Advertisments