Is it possible to Use aspx c# customvalidator to call action?

End Goal

I have a textbox where the user enters data, and have a "CustomValidator" that checks to see if the textbox entry exist in database. If so, I want data from the database to populate other text boxes, If not, I want to enable a textbox that allows the user to manually enter the data in the other fields.

Example: User Enters a code, validator checks if code is valid. If code is valid, the items associated with that code populate fields, if the code is invalid they get an error message warning them that the code is not valid, but they may continue manually

I know I can use textbox.enabled to change status of the field. I know how to change the error message

What I don't know is how to do is:

  1. Use custom validator to open a sqlconnection and validate against a list generated by it.
  2. Perform actions based on validations pass/fail status.

Just a kick in the pants to find the information about those two would thing be gratefully appreciate.

Google has not been my friend concerning those two things.

Upvotes: -1

Views: 152

Answers (1)

Albert D. Kallal
Albert D. Kallal

Reputation: 49319

Well, in most cases, you just drop in a validator control, and it wires up the client side JavaScript all automatically for you. This works great for say email text box, or numbers, or whatever. And this type of validation is nice, since no server side code, or even a round trip occurs here.

While dropping in a validator control DOES have and allow server side code (code behind)? I don't think you get much of any advantage here then just coding this out.

I mean, user types something into text box. They will have to tab, or hit some search button or what not, right?

So, pull the data (or find out the data doesn't exist).

And your grouping of controls? Just put them all inside of a panel.

Now, you can simple enable or disable the whole panel and group of controls with great ease.

So, say I have a hotel "ID" prompt. You type in a number. If hotel exists, then we display the information and DISABLE the panel. User can see the panel, but not edit.

Or, if no value found, then display message, but ALLOW the user to edit those text fields. So, thinking about this problem, I don't think it is a job for validators, and worse you need the means to enable/disable a whole group of controls.

So, say we have this markup up:

        Enter Hotel ID: 
        <asp:TextBox ID="HotelID" runat="server"></asp:TextBox>
        <asp:Button ID="cmdSearch" runat="server" Text="Verify" 
            style="margin-left:15px" CssClass="btn"
            />
        <br />
        <asp:Label ID="lblmsg" runat="server" Text=""></asp:Label>
        <br />


        <asp:Panel ID="HotelInfo" runat="server" Visible="false">
        <div style="float:left;width:15%">
            <div style="text-align:right">
                First Name :<asp:TextBox ID="txtFirstname" runat="server" Width="150"></asp:TextBox> <br />
                Last Name :<asp:TextBox ID="txtLastname" runat="server" Width="150"></asp:TextBox> <br />
                Hotel Name :<asp:TextBox ID="txtHotel" runat="server" Width="150"></asp:TextBox> <br />
                City  :<asp:TextBox ID="txtCity" runat="server" Width="150"></asp:TextBox> <br />
                Active :<asp:CheckBox ID="Active" runat="server" Width="150"></asp:CheckBox>
            </div>
        </div>
        </asp:Panel>

So, the panel visible = false.

The user will see this this (non valid ID).

enter image description here

So, user gets a message - can edit the controls.

But, if we type in a valid id, then they user sees this:

enter image description here

Note how the data display - but we can NOT edit - since we disabled the panel.

I suppose you might have some additional buttons like continue etc., but you get the general idea here.

The code for the button to verify?

This:

Protected Sub cmdSearch_Click(sender As Object, e As EventArgs) Handles cmdSearch.Click

    Dim strSQL = "SELECT * FROM tblHotels WHERE ID = @ID"

    Using conn = New SqlConnection(My.Settings.TEST4)
        Using cmdSQL = New SqlCommand(strSQL, conn)
            cmdSQL.Parameters.Add("@ID", SqlDbType.Int).Value = HotelID.Text

            Dim rstData = New DataTable
            conn.Open()
            rstData.Load(cmdSQL.ExecuteReader)

            If rstData.Rows.Count = 0 Then
                ' not found. Display message
                lblmsg.Text = "Not found - enter values or search again"
                ' display panel
                HotelInfo.Visible = True
                HotelInfo.Enabled = True ' allow edit of controls
            Else
                ' hotel found
                lblmsg.Text = "Hotel found"
                ' display panel
                HotelInfo.Visible = True
                HotelInfo.Enabled = False ' disable edits

                ' now fill controls with data 

                Dim OneRow As DataRow = rstData.Rows(0)

                txtHotel.Text = OneRow("HotelName")
                txtCity.Text = OneRow("City")
                txtFirstname.Text = OneRow("FirstName")
                txtLastname.Text = OneRow("LastName")

            End If

        End Using
    End Using

End Sub

Edit

Sorry - I see this was for C#. Here is a C# version:

        string strSQL = "SELECT * FROM tblHotels WHERE ID = @ID";
        using (SqlConnection conn = new SqlConnection(Properties.Settings.Default.TEST4))
        {
            using (SqlCommand cmdSQL = new SqlCommand(strSQL, conn))
            {
                cmdSQL.Parameters.Add("@ID", SqlDbType.Int).Value = HotelID.Text;
                DataTable rstData = new DataTable();
                conn.Open();
                rstData.Load(cmdSQL.ExecuteReader());

                if (rstData.Rows.Count == 0)
                {
                    // not found - display message
                    lblmsg.Text = "Not found - enter values or search again";
                    // display panel
                    HotelInfo.Visible = true;
                    HotelInfo.Enabled = true;   // allow edit of controls
                }
                else
                {
                    // hotel found
                    lblmsg.Text = "Hotel found";
                    // display panel
                    HotelInfo.Visible = true;
                    HotelInfo.Enabled = false;  // disable edits

                    //now fill controls with data 
                    DataRow OneRow = rstData.Rows[0];

                    txtHotel.Text = OneRow["HotelName"].ToString();
                    txtCity.Text = OneRow["City"].ToString();
                    txtFirstname.Text = OneRow["FirstName"].ToString();
                    txtLastname.Text = OneRow["LastName"].ToString();

                }
            }
        }

Edit: Using a validator

Note that you ARE free to drop in a custom validator control. and that control WILL require that the textbox has auto-postback = true.

So at this point in time? All the extra work of using a custom valuator will STILL result in a post-back, will STILL require that the textbox has autopostback = true.

Hence given that you want to call server side code, then you might as well just set the text box auto postback=true, and use the text changed event of that text box. In both cases, you going to suffer a post-back, and thus the concept of using a client side validator to prevent the page post-back is not something you gain, or achieve by using a custom validator + server side code for that validator.

Upvotes: 0

Related Questions