Solved

Validating DropDown List Inside a GridView

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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Suggested Solutions

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
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.…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

752 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