Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 3.5 > ASP.NET 3.5 Basics
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
ASP.NET 3.5 Basics If you are new to ASP or ASP.NET programming with version 3.5, this is the forum to begin asking questions. Please also see the Visual Web Developer 2008 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 3.5 Basics section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #11 (permalink)  
Old January 22nd, 2009, 01:05 AM
Authorized User
 
Join Date: Jan 2009
Posts: 26
Thanks: 4
Thanked 1 Time in 1 Post
Lightbulb

Thank you for your reply. It got me thinking enough that I spent the rest of the day working with your approach - and got it to work, almost . I'm a bit upset with myself that it's taking me so long to figure this out, but at least I finally understand some of the concepts behind the code better...

Quote:
Originally Posted by Imar View Post
That's exactly what my example was for: if you set the ChosenState property on the User Control, it automatically updates the TextBox control and the DropDownList...
I realized that your code was the flip of my code and decided to go with yours for the property class "ChosenState" - I kept my "SelectedIndexChanged" event, though.

Quote:
Originally Posted by Imar View Post
Binding the data is the same as with other controls in an ItemTemplate:
Code:
 
<ItemTemplate>
  <uc1:NameOfUserControl1 ID="NameOfUserControl1" runat="server" ChoseState='<%# Eval("State") %>' />
</ItemTemplate>
Ummm, I was only able to get it to work when I changed the Eval to a Bind (I googled the heck out of it before deciding that was the way to go and that it wasn't some other aspect of the code that was messing up). I still am not sure I understand the difference between Eval and Bind, though. But I did end up understanding how to read the whole statement, which I thought meant: assign the value of State to ChosenState as opposed to the reverse. (Which was why I couldn't understand how it was going to know what to set the State to. D'oh!)

Quote:
Originally Posted by Imar View Post
You can access the User Control similar to how you refer to other controls: using FindControl. Not sure where you're going to use it
OK, so now we come to the part that still doesn't work - the control does not know to display the value of State when the detailsview is loaded with an existing record. I can describe in English how I think it could happen, but I'm not sure how to go about dealing with it...
Currently in the dropdownlist of the control there is a ListItem with a value of "" and the contents of "Please select a state..."
The property AppendDataBoundItems for the control is set to True, so this is what the user sees when the detailsview loads. Is there a way to access the text and change it to the value of the underlying record? I think that avoids the issue of it being a value that's not in the list... I just don't know when to do this (PageLoad event of the page? databinding of the detailsview?) and the syntax for changing the listitem text.

Quote:
Originally Posted by Imar View Post

Code:
 
Dim myControl As NameOfUserControl = CType(GridView1.Rows _
                        (e.RowIndex).FindControl("NameOfUserControl1"),
FWIW, I never defined a var as a type "user control" before - google indicated that it be prefaced with ASP. - is that correct?

Quote:
Originally Posted by Imar View Post
I wasn't trying to confuse you with the lingo.
Nah, I knew you weren't I'm taking this class online, and I think a major drawback has been lack of interaction between classmates and also the professor. Sometimes a classroom discussion can shed light on a concept that you didn't realize was not clear, that you thought you understood because the book made it sound so simple. So I thank you greatly for your input (and for writing the book too :-) )


Quote:
Originally Posted by Imar View Post
Hope this clarifies things a bit...
It did more than a bit It seemed insurmountable yesterday and I was ready to give up and just use a drop down list and forget about allowing the users to enter their own State, but I decided if you were nice enough to answer I should be willing to try again. So, if I can get the data to display when the record loads I'l be one happy camper (errr, student)

Thanks,
Mrs. CB Spira
  #12 (permalink)  
Old January 23rd, 2009, 01:13 AM
Authorized User
 
Join Date: Jan 2009
Posts: 26
Thanks: 4
Thanked 1 Time in 1 Post
Question

Quote:
Originally Posted by cbspira View Post
OK, so now we come to the part that still doesn't work - the control does not know to display the value of State when the detailsview is loaded with an existing record. I can describe in English how I think it could happen, but I'm not sure how to go about dealing with it...
Currently in the dropdownlist of the control there is a ListItem with a value of "" and the contents of "Please select a state..."
The property AppendDataBoundItems for the control is set to True, so this is what the user sees when the detailsview loads. Is there a way to access the text and change it to the value of the underlying record? I think that avoids the issue of it being a value that's not in the list... I just don't know when to do this (PageLoad event of the page? databinding of the detailsview?) and the syntax for changing the listitem text.
I'm almost there, but I still need some help.
I can do this to access the item list text:
Code:
Dim myControl As ASP.ComboBox = CType(ComboBox1, ASP.ComboBox)
    Dim ddlUC As DropDownList = myControl.FindControl("ddlStates")

    Dim something As String = "New value of List Item"


    ddlUC.SelectedItem.Text = something
And that will indeed change the "Please select" to the value of something.

What I am stuck with is: how do I (programmatically?) retrieve the desired value from the field so it can be displayed? The datasource for the detailsview is a linqdatasource. This is probably elementary, but I'm not finding how to do it - I would have thought that my linqdatasource control would have a .field.index.value or some such element - but if it does I haven't seen it... Anyone know the answer to this?

Thank you so much,
Mrs. CB Spira
  #13 (permalink)  
Old January 24th, 2009, 04:54 AM
Imar's Avatar
Wrox Author
Points: 72,055, Level: 100
Points: 72,055, Level: 100 Points: 72,055, Level: 100 Points: 72,055, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,086
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

It all depends on your actual implementation, but here's what I would do.

1. Bind the chosen state to your user control as I showed you earlier:
Code:
<ItemTemplate>
  <uc1:NameOfUserControl1 ID="NameOfUserControl1" runat="server" 
                      ChosenState='<%# Eval("State") %>' />
</ItemTemplate>
When this code is fired, the State from your LINQ query object is taken and assigned to the ChosenState property of the User Control.

2. Inside this property in the User Control, make sure your DropDownList contains the item from the database, then try to find the chosen state and select it. If you can't find it in the list, either add it or preselect Choose an Item and set the State in the TextBox. This should work:

Code:
Public Property State() As String
  Get
    If DropDownList1.SelectedIndex < 0 Or DropDownList1.SelectedValue = "Other" Then
      ' the other item is selected, or no option is chosen yet
      Return TextBox1.Text
    Else
      Return DropDownList1.SelectedValue
    End If
  End Get
  Set(ByVal Value As String)
    Dim myItem As ListItem = DropDownList1.Items.FindByValue(Value)
    If myItem Is Nothing Then
      ' Item not found in the list, so it must be a custom item
      TextBox1.Text = Value
    Else
      myItem.Selected = True
    End If
  End Set
End Property
Instead of assigning the unknown state to the TextBox you can also add a brand new ListItem like this:

DropDownList1.Items.Add(Value)

You need to call this in the Setter of the property.

If they DropDownList doesn't cointain any items yet when the property is set, you can force the list to be populated by calling DataBind from within the propeprty setter:

Code:
  Set(ByVal Value As String)
    DropDownList1.DataBind()
    Dim myItem As ListItem = DropDownList1.Items.FindByValue(Value)
    If myItem Is Nothing Then
      ' Item not found in the list, so add it
      DropDownList1.Items.Add(Value)

    Else
      myItem.Selected = True
    End If
  End Set
Does this help?

Quote:
I just don't know when to do this (PageLoad event of the page? databinding of the detailsview?) and the syntax for changing the listitem text.
IMO, a clean place to do this is in the proeprty setter as I have shown you, as that's exactly where all the action takes place. E.g. you get a value from an outside control and use that value to either preselect an item in the list, or populate the TextBox....

Hope this helps,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
The Following User Says Thank You to Imar For This Useful Post:
cbspira (January 24th, 2009)
  #14 (permalink)  
Old January 24th, 2009, 09:07 PM
Authorized User
 
Join Date: Jan 2009
Posts: 26
Thanks: 4
Thanked 1 Time in 1 Post
Thumbs up

Thanks for replying - I was at an impasse and really did not know where to turn...

Quote:
Originally Posted by Imar View Post

1. Bind the chosen state to your user control as I showed you earlier:
Done.

Quote:
Originally Posted by Imar View Post
2. Inside this property in the User Control, make sure your DropDownList contains the item from the database, then try to find the chosen state and select it. If you can't find it in the list, either add it or preselect Choose an Item and set the State in the TextBox.
So I had already done that, but for some reason it was not finding the value even though it had been set originally through the control. What was happening was that the "Please select" was staying selected and the value being added underneath it. So, not having time to debug it as thoroughly as I'd like because the project was due Jan 14, decided to just add the following line to the If statement in the Setter:
Code:
ddlStates.SelectedItem.Text = Value
which changes the "Please select" to the value. I no longer assign the value to the textbox, and it also eliminates the need to add it to the list (as per your next suggestion)

Quote:
Originally Posted by Imar View Post
Hope this helps,
More than you know - thank you SOOOOOO much! My control now seems to function as a true combobox, where a user can either pick an item from the list or enter a different one, and it also displays the correct value if the record already contains one. Yes, there are still bugs, but for now this will suffice.

Thank you again,
Mrs. CB Spira
  #15 (permalink)  
Old January 25th, 2009, 04:51 AM
Imar's Avatar
Wrox Author
Points: 72,055, Level: 100
Points: 72,055, Level: 100 Points: 72,055, Level: 100 Points: 72,055, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,086
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Excellent, you're welcome. Glad it is working now...

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
  #16 (permalink)  
Old January 27th, 2010, 11:07 AM
Registered User
 
Join Date: Jan 2010
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by Old Pedant View Post
The limitation in ASP.NET is simply reflecting that same limitation in HTML.

What you are looking for is something that MS refers to as a "combo box", where you can *either* choose from a list or type in a new value.

But HTML has no such control now, really, anything similar.

Now, many people have used DHTML (that is, javascript in the browser) to implement this same functionality. And you can surely locate what you want in some JS library out on the net someplace. But integrating it with ASP.NET *probably* means either (1) NOT using an <asp:xxx> tag but instead just using HTML tags, which means you'd then use Request.Form to get the values instead of treating them as if they were elements in the ASP.NET page object or (2) finding or creating your own CUSTOM ASP.NET tag that does all the above for you, behind the scenes.

I would start by googling for something like "ASP.NET combo box custom tag". See what you come up with. Oh, what the heck...

I just did that, and the very first hit is something from "CodeProject" that looks pretty promising. Several other hits, so look around.

*****

Oops...that CodeProject combobox doesn't work right in FireFOx.

This one works better, though even it needs some tweaking of the CSS to make it look better:
http://www.metabuilders.com/Tools/ComboBox.aspx

Anyway, look around.
This MetaBuilders combobox looks like it will serve my needs, but I agree that its appearance could be better, namely that the dropdown arrow is misaligned with the textbox portion. Any idea whether that could be easily fixed?

- Robert -
  #17 (permalink)  
Old February 1st, 2010, 04:28 AM
Authorized User
 
Join Date: Jan 2010
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to elizas
Default DropDownList Validation problem in ASP.NET

Suppose in your application you are using a DropDownList in a Form and depending upon the item selection you are showing/hiding some views inside a Multiview and you want that the user must select one of the item from the DropDownList before submitting a Form.

So you can write code like the following to validate the DropDown :

<asp:DropDownList ID="ddlExpertLevel" runat="server" AutoPostBack="True" onselectedindexchanged="ddlExpertLevel_SelectedInd exChanged" >

<asp:ListItem Text="--Select--" Value="0" />

<asp:ListItem Text="Beginner" Value="1" />

<asp:ListItem Text="Intermediate" Value="2" />

<asp:ListItem Text="Advanced" Value="3" />

</asp:DropDownList>


<asp:RequiredFieldValidator ID="reqValQuestionType" runat="server" ErrorMessage="Level is required." Text="*" ControlToValidate="ddlExpertLevel" InitialValue="0">
</asp:RequiredFieldValidator>
This code will work fine but it has an issue. The issue is - If page validation returns false and after that you are trying to change the DropDown's index then it will not work for first time.
Because when you submit the form it will do Form validation. If Validation returns false [indicates not to submit the Form], then you can can't go to server side code. The problem lies here. Since you have used "SelectedstateChanged" event for the Dropdown, the code inside the event handler function will not execute after form validation[when validation will return false].

So How to handle this problem. The solution is very simple, you have to do the following things -

Make CausesValidation="false" for the DropDown.

Remove any validation group for that DropDown, i.e ValidationGroup="none"

Handle client side change event to avoid Blocking of Submit on DropDown item change, i.e onchange="Page_BlockSubmit = false;"

So the final code would look like :
<asp:DropDownList ID="ddlExpertLevel" runat="server" AutoPostBack="True"

CausesValidation="false" ValidationGroup="none" onchange="Page_BlockSubmit = false;"

onselectedindexchanged="ddlExpertLevel_SelectedInd exChanged" >

<asp:ListItem Text="--Select--" Value="0" />

<asp:ListItem Text="Beginner" Value="1" />

<asp:ListItem Text="Intermediate" Value="2" />

<asp:ListItem Text="Advanced" Value="3" />

</asp:DropDownList>


<asp:RequiredFieldValidator ID="reqValQuestionType" runat="server" ErrorMessage="Level is required." Text="*" ControlToValidate="ddlExpertLevel" InitialValue="0">
</asp:RequiredFieldValidator>
Hope this tip is useful.
Any suggestions are welcomed
Thanks
__________________
Cheers,
Eliza

Mindfire: India's Only Company to be both Apple Premier & Microsoft Gold certified.
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
What is the statement to select limited values? Mangore SQL Language 1 December 1st, 2008 02:42 AM
Limited users connected at once. salemkoten SQL Server 2005 5 October 10th, 2006 09:26 PM
Dynamic in a limited environment? fs22 Javascript How-To 1 February 15th, 2006 07:51 PM
how to display limited records per screen avats General .NET 2 June 4th, 2005 12:32 AM
text in limited area alittlebitter32 Dreamweaver (all versions) 4 July 8th, 2004 10:28 AM



All times are GMT -4. The time now is 05:38 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.