Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Validating DropDown List Inside a GridView

Posted on 2013-02-06
4
Medium Priority
?
1,239 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 2000 total points
ID: 38863529
0
 

Author Closing Comment

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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

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…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…

971 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