Solved

Validating DropDown List Inside a GridView

Posted on 2013-02-06
4
1,125 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
ID: 38861930
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
ID: 38862589
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
ID: 38863529
0
 

Author Closing Comment

by:sherbug1015
ID: 38865274
Worked perfect.  Thanks for the link
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Initial get page response times and IIS8 15 51
jquery datatables, asp.net MVC 9 41
Getting error in connectionstring with Excel. 30 32
ASP.NET MVC identity 6 26
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 …
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

770 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