Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Validate particular control in datalist

Posted on 2006-11-30
21
Medium Priority
?
392 Views
Last Modified: 2008-08-07
Hi Experts,
   I have a datalist control which lists two types of items. One of the items  displays a dropdown list for selecting a value. The datalist also includes a button and a RequiredFieldValidator. The RequiredFieldValidator validates the dropdown list value. The problem is if the item type showing the drop down list is chosen and a value is not entered then the validation applies to every instance of that type of item. Is it possible to make the RequiredFieldVlidator only validate the selected items control or if not how would this be done using custom code?

Regards
James
0
Comment
Question by:JamesJMcDonnell
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 11
  • 10
21 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 18044697
Can you show me the ItemTemplate for the the DataList?

Bob
0
 

Author Comment

by:JamesJMcDonnell
ID: 18045517
Hi Bob,
 
Here is the item template:

<ItemTemplate>
                  <uc1:VoucherSummeryDisplay ID="VoucherSummeryDisplay1" runat="server"
                        VoucherTypeId='<%#Eval("VoucherTypeId")%>'
                        voucherType='<%#Eval("voucherType")%>'
                        Description='<%#Eval("Description")%>'
                        TermsConditionLink='<%#Eval("TermsConditionLink")%>'
                        Price='<%#Eval("Price")%>'
                        DelegateRequired='<%#Eval("DelegateRequired")%>'
                        Units='<%#Eval("Units")%>'
               />
               <div id="DivDelegateInfo" runat ="server" visible ="false">
                Select Delegate:   <asp:DropDownList ID="DDLDelegateID" runat="server"></asp:DropDownList>    
            <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="DDLDelegateID"
                Display="Dynamic" ErrorMessage="Delegate Required" SetFocusOnError="True" Width="167px"></asp:RequiredFieldValidator>
                </div>
              <asp:Label ID="LblVoucherTypeId" runat="server"  Visible="false"  Text ='<%#Eval("VoucherTypeId")%>'></asp:Label>
              <asp:Label ID="lblDelegateRequired" runat="server"  Visible="false" Text ='<%#Eval("DelegateRequired")%>'></asp:Label>
              <asp:Button ID="ButtonAddtocart" runat="server" Text="Add to cart" />
                  </ItemTemplate>

James
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 18045576
So, you only want to validate the DropDownList controls that you have tried to select items and didn't?

Bob
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:JamesJMcDonnell
ID: 18045661
Only one item is selected - by clicking on button, and that is the only one I want to validate the dropdownlist for - though only when it is a certain type of record (the dropdownlist list only appears if it is a certain type of record)

James
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 18046727
What does <ButtonAddtocart> do?

Bob
0
 

Author Comment

by:JamesJMcDonnell
ID: 18047213
<ButtonAddtocart> adds selected item to a shopping cart, the selected item in this case is a set of vouchers. Vouchers can either be for use by anyone or for a particular person. The ones for a particular person need the dropdownlist value selected to indicate the person. The code worked when there was only one record of each type but I added extra records to have different numbers of voucher 'blocks' available for each voucher type.
When the AddToCart button is clicked the following code is run:

Protected Sub dtVouchers_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataListItemEventArgs) Handles dtVouchers.ItemDataBound
        Dim DynamicDDLDelegateID As DropDownList = CType(e.Item.FindControl("DDLDelegateID"), DropDownList)
        Dim lblDelegateRequired As Label = CType(e.Item.FindControl("lblDelegateRequired"), Label)
        Dim DivDelegateInfo As HtmlGenericControl = CType(e.Item.FindControl("DivDelegateInfo"), HtmlGenericControl)
        Dim butAdd As Button = CType(e.Item.FindControl("ButtonAddtocart"), Button)
        butAdd.CausesValidation = False
        If System.Boolean.Parse(lblDelegateRequired.Text.ToString()) Then
            If Not DynamicDDLDelegateID Is Nothing Then
                DynamicDDLDelegateID.DataSource = DelegateCollection
                DynamicDDLDelegateID.DataTextField = "text"
                DynamicDDLDelegateID.DataValueField = "value"
                DynamicDDLDelegateID.DataBind()
                butAdd.CausesValidation = True
            End If
            DivDelegateInfo.Visible = True
        End If
    End Sub

James
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 18047733
Would it be possible to set CausesValidation = False for the default DropDownList rendering, and then set it to True in dtVouchers_ItemDataBound?

Bob
0
 

Author Comment

by:JamesJMcDonnell
ID: 18052539
Well I tried putting CausesValidation = False on the HTML tag for the button, it is already set to True in dtVouchers_ItemDataBound - but this does not make any difference.

James
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 18053131
Not the button, the DropDownList, since that is what you are trying to validate.

Bob
0
 

Author Comment

by:JamesJMcDonnell
ID: 18054358
I amended the dropdownlist tag to: <asp:DropDownList ID="DDLDelegateID" runat="server" CausesValidation="false"></asp:DropDownList>

and in dtVouchers_ItemDataBound  replaced butAdd.CausesValidation = True with:
        Dim butAdd As Button = CType(e.Item.FindControl("ButtonAddtocart"), Button)
        ddDelegate.CausesValidation = True

but got same result
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 18054406
Yes, that isn't going to work, since the ItemDataBound is applied to all the items, so you are just resetting all the CausesValidation for each DropDownList control.  What you would need to do is to only set CausesValidation = True when the DropDownList is changed.

Bob
0
 

Author Comment

by:JamesJMcDonnell
ID: 18054610
The validation will only occur if the DropDownList has not changed i.e. a value for ddlDelegate has not been selected!
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 18063393
James,

I don't understand how that is happening.  Could you explain that again, please?  I don't see where anything is happening when the SelectedIndex is changed for the DropDownList.

Bob
0
 

Author Comment

by:JamesJMcDonnell
ID: 18063733
Bob,

The DropDownList is contained in a DataList and is visible only for a particular voucher type - one that is for a named individual. The Datalist also includes (as shown in the HTML above) a control which contains a description of the voucher, a label and a button. When the SelectedIndex is changed for the DropDownList no code is run, the only thing that happens is that the value changes. The action happens when the button is clicked. The code for the ItemDataBound is above. Here is the code for the ItemCommand:

Protected Sub dtVouchers_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataListCommandEventArgs) Handles dtVouchers.ItemCommand
        'if there are attributes, yank them from Request.Form
        'This will be redone  for 2.0 Release. It's Kantona's fault.
        Dim Validdata As Boolean
        Dim DelegateID As Integer
        Dim DelegateName As String
        Dim VoucherTypeID As Integer

        'Response.Write("aaaaaaaaaa")
        'Dim dd As DropDownList = dtVouchers.Items(e.Item.ItemIndex).FindControl("DDLDelegateID")
        'Response.Write(dd.SelectedValue)
       
        Dim lblVoucherTypeID1 As Label = CType(e.Item.FindControl("LblVoucherTypeId"), Label)
        Dim DynamicDDLDelegateID As DropDownList = CType(e.Item.FindControl("DDLDelegateID"), DropDownList)
        Validdata = True
        'make sure there's a current order queued up
        InitCurrentOrder()
        If Not lblVoucherTypeID1 Is Nothing Then
            VoucherTypeID = Integer.Parse(lblVoucherTypeID1.Text)
        End If

        Dim VoucherItem As New SiteOrderVoucher(VoucherTypeID)

        If VoucherItem.DelegateRequired Then
            If Not DynamicDDLDelegateID Is Nothing Then
                DelegateID = Integer.Parse(DynamicDDLDelegateID.SelectedValue.ToString())
                DelegateName = DynamicDDLDelegateID.SelectedItem.Text.ToString()
            End If
            If DelegateID <= 0 Then
                Validdata = False
            End If
        End If

        'add the item to the current order and redirect them to
        'the add page
        If Validdata Then
            'Response.Write("valid data")
            VoucherItem.DelegateID = DelegateID
            VoucherItem.DelegateName = DelegateName
            VoucherItem.BasketID = Guid.NewGuid()
            VoucherItem.Quantity = Integer.Parse(1)
           
            Profile.CurrentOrder.AddVoucher(VoucherItem)
            Response.Redirect("VoucherBasket.aspx")
        End If

    End Sub

James

 
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 18067870
Are we talking about this DropDownList?

  Dim DynamicDDLDelegateID As DropDownList = CType(e.Item.FindControl("DDLDelegateID"), DropDownList)

If so, I believe that you should be changing the CausesValidation here for the DropDownList.

Bob
0
 

Author Comment

by:JamesJMcDonnell
ID: 18068163
But that is in the itemdatabound event. I tried it earlier and it didn't work - it applied to all rows of the datalist where the dropdown was appropriate. My understanding is that the itemdatabound event occurs for every row in the datalist before the page is rendered to the client. But I just want it to apply to the row that is selected when a button on a datalist row is clicked, so I think it must be in the itemdata event but perhaps I  need to usecustom code rather than using a validation control.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 18068228
What you have is a non-standard configuration, since normally you would have a RequiredFieldValidator to validate every row in the DataList.

What you need is for the embedded DropDownList to have CausesValidation = False, until a certain condition occurs, and then it would change to True.  That condition appears to be in the ItemCommand event handler that you just showed me.

Bob
0
 

Author Comment

by:JamesJMcDonnell
ID: 18068476
Bob,

Dim DynamicDDLDelegateID As DropDownList = CType occurs in both itemcommand and itemdatabound, I have CausesValidation set to false in the html for the DropDownList and in itemdatabound and set to true in itemcommand, but it still tries to validate all the instances where the DropDownList is shown.  
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 1000 total points
ID: 18076442
James,

I don't know if I know just the right correction to make this work.  It is a non-standard implementation of validators, and everything that I can think of won't work.

Bob
0
 

Author Comment

by:JamesJMcDonnell
ID: 18132610
Hi Bob,

Eventually I discovered a way to do it using the new Validation Groups feature in asp.net 2.0:

put  ValidationGroup='<%#Eval("VoucherTypeId")%>' in the RequiredFieldValidator tag and put

ValidationGroup = '<%#Eval("VoucherTypeId")%>' in the AddToCart button.

This causes the validation to be performed on the selected item only as there is a separate Validation Group name for each datalist item

Thanks for your help, I hope you will be satisfied with a lower grade for this one
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 18132761
Very nice, thank you for pointing out the discovery.

Bob
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question