Solved

Validating DropDown List Inside a GridView

Posted on 2013-02-06
4
1,191 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
[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
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

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

623 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