Hi,

I'm trying to access the RangeValidator control at my code-behind file. I keep on getting "Object reference not set to an instance of an object." exception. Can someone enlighten me?

ASPX:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" DataKeyNames="subject_id" BackColor="White" BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" CellPadding="5" ForeColor="Black" GridLines="Horizontal" OnDataBound="GridView1_DataBound">
   <Columns>
       
       <asp:TemplateField HeaderText="Hurdle Weight" SortExpression="hurdle_weight">
                    <ItemTemplate>
                        <asp:Label ID="Label113" runat="server" Text='<%# Bind("hurdle_weight") %>'></asp:Label>
                    </ItemTemplate>
                    <ItemStyle Wrap="false" />
                    <EditItemTemplate>
                        <asp:TextBox ID="textbox_hurdleWeight1" Width="30px" runat="server" Text='<%# Bind("hurdle_weight") %>' /> % 

                        <asp:RangeValidator ID="RangeValidator16" runat="server" ControlToValidate="textbox_hurdleWeight1" 
                            Font-Names="Verdana"
                            ErrorMessage="The hurdle weight must be less or equals with the available weight" 
                            Display="Static">*
                        </asp:RangeValidator>
                    </EditItemTemplate>
                </asp:TemplateField>

Code behind:

protected void GridView1_Editing(object sender, GridViewEditEventArgs e)
    {
        string subjectID = dropdown_subjectCode.SelectedValue.ToString();
        int index = Convert.ToInt32(e.NewEditIndex);

        RangeValidator validator = (RangeValidator)GridView1.Rows[index].NamingContainer.FindControl("RangeValidator16");
        validator.MaximumValue = db.getHurdleWeightBalance(subjectID);
    }

I have found the workaround for this. More details from here.

protected void GridViewRowEventHandler(Object sender, GridViewRowEventArgs e)
    {
        string subjectID = dropdown_subjectCode.SelectedValue.ToString();

        if (e.Row.RowState == DataControlRowState.Edit) 
        {
            RangeValidator validator = (RangeValidator)e.Row.FindControl("RangeValidator16");
            validator.MaximumValue = db.getHurdleWeightBalance(subjectID);
        }
    }

Would it be very different to access the selected index of a combobox for instance?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.