Issue in validating dropdownlist in gridview

Last post 01-15-2008 4:06 AM by Som Nath Shukla. 1 replies.

Sort Posts:

  • Issue in validating dropdownlist in gridview

    01-14-2008, 7:51 PM
    • Loading...
    • newbeee007
    • Joined on 01-15-2008, 12:43 AM
    • Posts 1

    I'm working on .NET 2.0
    I've a simple test webform with a GridView. The grid has a few bound columns, a template field with a drop down list (with hard coded values) and a asp:Button field.
    It also has a RequiredFieldValidator, to ensure that the user has chosen a value other than default, before he submits the form and a ValidationSummary control.
    The first value in the drop down list is "choose one of the following ...".
    The intention here is to force user to choose a value from the drop down for the row in which he clicks the button.

    My problem is, validation is happening for all the rows instead of happening only on the row he clicked!! So, even when he chooses a value in the dropdown and clicks the submit button on a row, the validation fails as the dropdown in other rows still have their default value selected!

    I think this is a pretty simple scenario. Can someone please point what I might be doing wrong?

    The code looks like this

    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>Untitled Page</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ID"
                DataSourceID="SqlDataSource1" OnRowCommand="GridView1_RowCommand">
                <Columns>
                    <asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" ReadOnly="True"
                        SortExpression="ID" />
                    <asp:BoundField DataField="FirstName" HeaderText="FirstName" SortExpression="FirstName" />
                    <asp:BoundField DataField="LastName" HeaderText="LastName" SortExpression="LastName" />
                    <asp:BoundField DataField="Age" HeaderText="Age" SortExpression="Age" />
                    <asp:TemplateField>
                        <ItemTemplate>
                            <asp:DropDownList ID="DropDownList1" runat="server">
                                <asp:ListItem Value="-1" Selected="True">choose one of the following ...</asp:ListItem>
                                <asp:ListItem Value="1">item1text</asp:ListItem>
                                <asp:ListItem Value="2">item2text</asp:ListItem>
                                <asp:ListItem Value="3">item3text</asp:ListItem>
                                <asp:ListItem Value="4">item4text</asp:ListItem>
                            </asp:DropDownList>
                            <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="DropDownList1"
                                ErrorMessage="RequiredFieldValidator" InitialValue="-1" ValidationGroup="testGroup">*</asp:RequiredFieldValidator>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:ButtonField ButtonType="Button" Text="Button" ValidationGroup="testGroup" CausesValidation="True" />
                </Columns>
            </asp:GridView>
            <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:db1ConnectionString %>"
                ProviderName="<%$ ConnectionStrings:db1ConnectionString.ProviderName %>" SelectCommand="SELECT [ID], [FirstName], [LastName], [Age] FROM [PersonalDetails]">
            </asp:SqlDataSource>
            <br />
            <asp:Label ID="lblResult" runat="server" Text="Label"></asp:Label><br />
            <br />
            <br />
            <asp:ValidationSummary ID="ValidationSummary1" runat="server" ValidationGroup="testGroup" />
       
        </div>
        </form>
    </body>
    </html>


    //Code behind is doing nothing significant
    codebehind file

       protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
            {
                string commandName = (string)e.CommandName;
                string commandArgument = (string)e.CommandArgument;

                lblResult.Text = "Command complete";
                
            }

  • Re: Issue in validating dropdownlist in gridview

    01-15-2008, 4:06 AM
    Answer

    hi plz add the control to particual cell at run time . ur problem is basically validationgroup propery of required field validator  and button. give the same validation group name to whom u want to validate  seprately.

    see i am using rowdataboud event a static varaible for assing id

    add control at run time to particular cell of datagrid. 

    static int i=0; 

     

    protected
    void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)

    {

    ListItem li = new ListItem();

    li.Value = "-1";

    li.Text = "Select Item";

    li.Selected = true;

    ListItem li1 = new ListItem();

    li1.Value = "1";

    li1.Text = " Item1";

    DropDownList dl = new DropDownList();

    dl.ID = "Drop" + i;

    dl.Items.Add(li);

    dl.Items.Add(li1);

    RequiredFieldValidator rq = new RequiredFieldValidator();

    rq.ID = "rq"+i;

    rq.ValidationGroup = "test" +i;

    rq.ControlToValidate = dl.ID.ToString();

    rq.InitialValue =
    "-1";

    rq.Text = "*";

    Button bt = new Button();

    bt.ID = "bt"+i;

    bt.ValidationGroup = "test" + i;

    bt.Text = "Button";

    bt.CausesValidation = true;

    e.Row.Cells[7].Controls.Add(dl);

    e.Row.Cells[7].Controls.Add(rq);

    e.Row.Cells[8].Controls.Add(bt);

    i++;

     

    }

     

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="empid"

    DataSourceID="SqlDataSource1" Style="position: relative" OnRowDataBound="GridView1_RowDataBound">

    <Columns>

    <asp:BoundField DataField="empid" HeaderText="empid" ReadOnly="True" SortExpression="empid" />

    <asp:BoundField DataField="managerid" HeaderText="managerid" SortExpression="managerid" />

    <asp:BoundField DataField="salary" HeaderText="salary" SortExpression="salary" />

    <asp:BoundField DataField="deptid" HeaderText="deptid" SortExpression="deptid" />

    <asp:BoundField DataField="rating" HeaderText="rating" SortExpression="rating" />

    <asp:BoundField DataField="startdate" HeaderText="startdate" SortExpression="startdate" />

    <asp:BoundField DataField="ename" HeaderText="ename" SortExpression="ename" />

    <asp:TemplateField HeaderText="Select">

    <ItemTemplate>

    </ItemTemplate>

     

    </asp:TemplateField>

    <asp:TemplateField HeaderText="Button">

    <ItemTemplate>

    </ItemTemplate>

     

    </asp:TemplateField>

    </Columns>

    </asp:GridView>

    Thanks and Regards
    Som Nath
    Please click “Mark as Answer” on the post if it helps you,
Page 1 of 1 (2 items)
Microsoft Communities
Page view counter