Validate that a GridView has a selected item

Last post 05-29-2007 12:13 PM by MShanahan. 4 replies.

Sort Posts:

  • Validate that a GridView has a selected item

    05-29-2007, 7:24 AM
    • Loading...
    • ilivewithian
    • Joined on 03-27-2007, 4:32 AM
    • Birmingham, UK
    • Posts 186

    Is it possible to use a validator to ensure that a user has selected an item in a gridview? If I point a validator to a gridview it throws an exception at runtime.

    Any thoughts?

    My wife is an opera singer.
    Help the homeless, buy a two too.
    Filed under: , ,
  • Re: Validate that a GridView has a selected item

    05-29-2007, 7:52 AM
    • Loading...
    • MShanahan
    • Joined on 05-25-2007, 1:53 PM
    • Posts 60

    I'm not sure about using a Validator, but you could check in the code behind, example:

    if (grd1.SelectedIndex != -1)

    {

    //Do Work here

    }

  • Re: Validate that a GridView has a selected item

    05-29-2007, 9:57 AM
    • Loading...
    • ilivewithian
    • Joined on 03-27-2007, 4:32 AM
    • Birmingham, UK
    • Posts 186

    I could do it that way, but I use the Page.IsValid property extensively, it would be much cleaner to validate the GridView directly. If I could get a custom validator to work, that would be great. Is there a way that I can extend the validators? I've had a look, but I could see how to do what I was looking for.

    My wife is an opera singer.
    Help the homeless, buy a two too.
  • Re: Validate that a GridView has a selected item

    05-29-2007, 12:01 PM
    • Loading...
    • MShanahan
    • Joined on 05-25-2007, 1:53 PM
    • Posts 60

    The following code will set the Page.IsValid to false;

    if (grd1.SelectedIndex != -1)

    {

    CustomValidator val = new CustomValidator();

    val.IsValid = false;

    Page.Controls.Add(val);

    }

     

  • Re: Validate that a GridView has a selected item

    05-29-2007, 12:13 PM
    Answer
    • Loading...
    • MShanahan
    • Joined on 05-25-2007, 1:53 PM
    • Posts 60

    Here ya go, I'll do you one better.  I have a hidden text field that will get validated.  In the SelectedIndexChanged event, I set the value of that textbox.  Here's the markup...

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml" >

    <head runat="server">

    <title>Untitled Page</title>

    <style type="text/css">

    .hdn

    {

    visibility:hidden;

    }

    </style>

    </head>

    <body>

    <form id="form1" runat="server">

    <div><asp:TextBox CssClass="hdn" ID="hdn" runat="Server"></asp:TextBox>

    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="hdn"

    ErrorMessage="RequiredFieldValidator" ValidationGroup="val"></asp:RequiredFieldValidator>

    <asp:GridView ID="grd1" runat="server" OnSelectedIndexChanged="dataGrid_SelectedIndexChanged">

    <Columns><asp:CommandField ShowSelectButton="true" /></Columns>

    </asp:GridView>

    <br />

    <asp:Button ValidationGroup="val" ID="Button1" runat="server" OnClick="Button1_Click" Text="Submit" /></div>

    </form>

    </body>

    </html>

    And here's the code-behind

    protected void dataGrid_SelectedIndexChanged(object sender, EventArgs e)

    {

    hdn.Text = grd1.SelectedIndex.ToString();

    }

Page 1 of 1 (5 items)
Microsoft Communities
Page view counter