Solved

Validating DropDown List Inside a GridView

Posted on 2013-02-06
4
1,105 Views
Last Modified: 2013-02-07
I have a gridview with 8 or 9 rows.  Each row has a dropdown and a link button.  I need to validate that the user selected something in the dropdown they clicked on.

Does anyone have a JavaScript that will validate a dropdown located inside a gridview.

The examples that I have found require a value to be in all dropdowns and I only require a value to be in the dropdown that was clicked.

I think I need to pass the function the row that was clicked.  

Can someone help me with this.  I am not sure how to trap the row number the user is on.

Thanks.
0
Comment
Question by:sherbug1015
4 Comments
 
LVL 26

Expert Comment

by:Alan Warren
Comment Utility
Possibly something like this:
<%@ Page Language="VB" title="MyGridview"%>

<!DOCTYPE html>

<script runat="server">

    Protected Sub DropDownList1_SelectedIndexChanged(sender As Object, e As EventArgs)
        If sender.SelectedItem.ToString = "" Then
            ClientScript.RegisterStartupScript(Me.GetType(), "Required Field", "alert('Please select an item from the list.');", True)
        End If
    End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>

        <asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1" AutoGenerateColumns="False">
            <Columns>
                <asp:BoundField DataField="ID" HeaderText="ID" ReadOnly="True" SortExpression="ID" />
                <asp:TemplateField>
                    <ItemTemplate>
                   <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
                        <asp:ListItem></asp:ListItem>
                        <asp:ListItem Value="1">Choice 1</asp:ListItem>
                        <asp:ListItem Value="2">Choice 2</asp:ListItem>
                    </asp:DropDownList>
                    </ItemTemplate>
                </asp:TemplateField>

            </Columns>
        </asp:GridView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:cnYourConnection %>" SelectCommand="select 1 as ID union all select 2 as ID"></asp:SqlDataSource>
    </div>
    </form>
</body>
</html>

Open in new window

Alan
0
 
LVL 20

Expert Comment

by:informaniac
Comment Utility
What if he clicked by mistake and doesn't want to select a value?
0
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 500 total points
Comment Utility
0
 

Author Closing Comment

by:sherbug1015
Comment Utility
Worked perfect.  Thanks for the link
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
How do I bind the results to a grid 3 22
Expando 4 33
Limiting string to two deciamls 18 30
Unable  to create new object 9 19
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now