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

creating text box dynamically.

Status
Not open for further replies.

nelco

Programmer
Apr 4, 2006
93
US

I have a drop down box with none, name, email,phone.
If the user clicks Email, it should show label enter email and text box to enter email with validator.
Same thing with Phone or name.

I have never done this in ASP.net. Any suggestions will be appreciated.
 
search the web for "webforms dynamically add controls"

Jason Meckley
Programmer

faq855-7190
faq732-7259
 
I wouldn't go the dynamic route. It can become very difficult to code and maintain. I would simply use a MultiView control. Put the ddl outside of that control. Then when the user changes their selection, you change the view(inside of the multiview) that is displayed.
 
+1 for jbenson001's approach. much simpler.

Jason Meckley
Programmer

faq855-7190
faq732-7259
 
Thanks for the response and guidance. I appreciate. I came out with creating panels and I am displaying the panels on selectedIndexChanged. Looks like this works.
Protected Sub SampleDropDownList_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles SampleDropDownList.SelectedIndexChanged
If SampleDropDownList.SelectedValue = "None" Then
Panel1.Visible = False
Panel2.Visible = False
Panel3.Visible = False
Panel4.Visible = False

ElseIf SampleDropDownList.SelectedValue = "Name" Then
Panel1.Visible = True
Panel2.Visible = False
Panel3.Visible = False
Panel4.Visible = False

ElseIf SampleDropDownList.SelectedValue = "Phone" Then
Panel2.Visible = True
Panel1.Visible = False
Panel3.Visible = False
Panel4.Visible = False

ElseIf SampleDropDownList.SelectedValue = "Email" Then
Panel3.Visible = True
Panel1.Visible = False
Panel2.Visible = False
Panel4.Visible = False

ElseIf SampleDropDownList.SelectedValue = "Address" Then
Panel1.Visible = False
Panel2.Visible = False
Panel3.Visible = False
Panel4.Visible = True

End If


End Sub
 
wow, that's a lot of repetitive code and a pain to maintain. a multiview will clean up the code nicely. or at least clean up the visibility code
Code:
Panel1.Visible = false;
Panel2.Visible = false;
Panel3.Visible = false;
Panel4.Visible = false;
if(SampleDropDownList.SelectedValue == "Name")
{
  Panel1.Visible = true;
}
if(SampleDropDownList.SelectedValue == "Phone")
{
  Panel2.Visible = true;
}
if(SampleDropDownList.SelectedValue == "Email")
{
  Panel3.Visible = true;
}
if(SampleDropDownList.SelectedValue == "Address")
{
  Panel4.Visible = true;
}
or using conventions name the panel the same as the value in the drop down list
Code:
None.Visible = false; //empty panel for convetions
Email.Visible = false;
Name.Visible = false;
Address.Visible = false;
Phone.Visible = false;

var id = SampleDropDownList.SelectedValue;
((WebControl)FindControl(id)).Visible = true;

Jason Meckley
Programmer

faq855-7190
faq732-7259
 
I appreciate your suggestions and I will do that.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top