0

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);
    }

Edited by dfs3000my: n/a

2
Contributors
2
Replies
6
Views
8 Years
Discussion Span
Last Post by MystikShadows
0

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);
        }
    }
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.