gridview's select command and controls validation

Last post 05-03-2008 1:30 PM by viktors. 1 replies.

Sort Posts:

  • gridview's select command and controls validation

    05-03-2008, 10:21 AM
    • Loading...
    • viktors
    • Joined on 01-11-2007, 6:48 AM
    • Posts 13

    Hi,

    I am having a problem with the combination of gridview's select command and controls validation.

    I have enabled (row) selection for a gridview which automatically generates a (select) link control. I have also a textbox control contained in the gridview's template field with a validation control attached to it.

    The problem is that validation is not performed on the client side when the row selection link is clicked and a postback occurs even though CausesValidation property for the link is set to True by default and I set it to True explicitly <asp:CommandField ShowSelectButton="True" CausesValidation="True"/>.

    I also have <asp:ButtonField CausesValidation="True" Text="Button" /> in my gridview that really causes validation on the client side before a postback but I would like to keep the row selection command to access the gridview's SelectedValue property. On the other hand, I need validation as well.

    When moving the mouse over the two mentioned links I see that they call two different JavaScript functions, with <asp:CommandField ShowSelectButton="True" CausesValidation="True"/> calling _doPostBack apparently without validation on the client side. Maybe there is a way to force validation on the client side before all postbacks? Otherwise, to keep validation, I will probably have to extract which row was selected from the request object.

    Viktors

  • Re: gridview's select command and controls validation

    05-03-2008, 1:30 PM
    Answer
    • Loading...
    • viktors
    • Joined on 01-11-2007, 6:48 AM
    • Posts 13

    Sorted it out.

    1.  Added a template field with a linkbutton to the gridview.

    <asp:TemplateField>

    <ItemTemplate>

    <asp:LinkButton ID="LinkButton1" runat="server" OnClick="LinkButton1_Click">LinkButton</asp:LinkButton>

    </ItemTemplate>

    </asp:TemplateField>

    2.  Added the following code to handle the linkbutton's click event to extract the value ("SelectedValue") of the gridview's row linkbutton of which was clicked.

    Protected Sub LinkButton1_Click(ByVal sender As Object, ByVal e As System.EventArgs)Dim rinda As GridViewRow = sender.bindingcontainer

    Result.Text = GridView1.DataKeys(rinda.RowIndex).Value

    End Sub

     3. CausesValidation property of the linkbutton is True by default and client side validation works.

    Viktors

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