Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

GridView Template RangeValidator MaximumValue/MinimumValue

Status
Not open for further replies.

Tranz2

Programmer
Jan 2, 2003
18
US
PLEASE NOTE BEFORE RESPONDING: The central point of my question is how to transfer other GridView column values to the MaximumValue and MinimumValue. IT IS NOT about validating dates, or about assigning any sort of literals to the MaximumValue and MinimumValue.

Like many people, I'm trying to dynamically set the MaximumValue and MinimumValue of the RangeValidator of a GridView template to other column values in the gridview. After much searching, I found a few examples that I am trying to use, but am getting an "Object reference not set to an instance of an object" on the code-behind line starting with "(e.Row.FindControl("RangeValidator1")...". The remarked-out line above it gives the same error.

Here is the code:

Code:
        <EditItemTemplate>
          <asp:TextBox ID="TextBox1" runat="server" 
            Text='<%# Bind("QTY_ORD", "{0:#,###}") %>'></asp:TextBox>

          <asp:RangeValidator ID="RangeValidator1" Runat="server" 
          ErrorMessage="Order Quantity outside of MIN/MAX range."
          MaximumValue="333" MinimumValue="111" 
          Type = "Integer"  
          ControlToValidate="TextBox1">*</asp:RangeValidator> 
                  
        </EditItemTemplate>

Code:
  protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
  {
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
   //   (e.Row.FindControl("RangeValidator1") as RangeValidator).MaximumValue = e.Row.Cells[5].Text.Trim();
      (e.Row.FindControl("RangeValidator1") as RangeValidator).MaximumValue = "999";
    } 
  }
 


Since your control is in the Edit Template and will only be available when the grid is in edit mode, you'll need to check the DataControlRowState:

Code:
if (e.Row.RowType == DataControlRowType.DataRow && 
        (e.Row.RowState & DataControlRowState.Edit) == DataControlRowState.Edit)
    { 
        // Here you will get the Control  
    }


Mark

"You guys pair up in groups of three, then line up in a circle."
- Bill Peterson, a Florida State football coach
 
I'm not really sure what you mean by "Here you will get the Control". That's exactly what I'm asking about - I need specific code - I need specifics. I tried the following in the GridView1_RowDataBound, thinking maybe this is what you were referring to, but it still gives a "Object reference not set to an instance of an object" error.

Is this what you mean? If it is, it still yields the error:

<code>if (e.Row.RowType == DataControlRowType.DataRow && (e.Row.RowState & DataControlRowState.Edit) == DataControlRowState.Edit)

{

(e.Row.FindControl("RangeValidator1") as RangeValidator).MaximumValue = "999";

}</code>
 

The Gridview1_RowDatabound will use the ItemTemplate, not the EditTemplate unless the grid is in edit mode.

Put the code in the Gridview1 RowEditing method. There you will be able to update the RangeValidator and not have to worry abount the RowType. I use VB, but you can port accordingly:

Code:
Dim rv As RangeValidator = GridView1.Rows(e.NewEditIndex).FindControl("RangeValidator1")
If rv IsNot Nothing Then
    rv.MaximumValue = 999
End If




Mark

"You guys pair up in groups of three, then line up in a circle."
- Bill Peterson, a Florida State football coach
 
Here is how I translated your VB code into C#. The "Rows" has a squiggly red line under it, and when I hover the mouse over it, it gives this error - "Non-invocable member 'System.Web.UI.WebControls.GridView.Rows' cannot be used like a method". Here is the code

Code:
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
  {
    MessageLabel.Text = "";


    RangeValidator rv  = GridView1.Rows(e.NewEditIndex).FindControl("RangeValidator1");


    rv.MaximumValue = "999";


}
 


You may have to use Rows[] vs Rows()


Mark

"You guys pair up in groups of three, then line up in a circle."
- Bill Peterson, a Florida State football coach
 
When I use instead:

Code:
RangeValidator rv  = GridView1.Rows[e.NewEditIndex].FindControl("RangeValidator1");

A red squiggly line underlines most of the statement, and it gives the error, "Cannot implicity convert type 'System.Web.UI.Control' to 'System.Web.UI.WebControls.RangeValidator' An explicit conversion exists (Are you missing a cast?)
 
RangeValidator rv = (RangeValidator) GridView1.Rows[e.NewEditIndex].FindControl("RangeValidator1");
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top