Retrieve EditItemTemplate controls of GridView at code-behind file

Please support our ASP.NET advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Dec 2008
Posts: 19
Reputation: dfs3000my is an unknown quantity at this point 
Solved Threads: 0
dfs3000my dfs3000my is offline Offline
Newbie Poster

Retrieve EditItemTemplate controls of GridView at code-behind file

 
0
  #1
28 Days Ago
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:
  1. <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">
  2. <Columns>
  3.  
  4. <asp:TemplateField HeaderText="Hurdle Weight" SortExpression="hurdle_weight">
  5. <ItemTemplate>
  6. <asp:Label ID="Label113" runat="server" Text='<%# Bind("hurdle_weight") %>'></asp:Label>
  7. </ItemTemplate>
  8. <ItemStyle Wrap="false" />
  9. <EditItemTemplate>
  10. <asp:TextBox ID="textbox_hurdleWeight1" Width="30px" runat="server" Text='<%# Bind("hurdle_weight") %>' /> %
  11.  
  12. <asp:RangeValidator ID="RangeValidator16" runat="server" ControlToValidate="textbox_hurdleWeight1"
  13. Font-Names="Verdana"
  14. ErrorMessage="The hurdle weight must be less or equals with the available weight"
  15. Display="Static">*
  16. </asp:RangeValidator>
  17. </EditItemTemplate>
  18. </asp:TemplateField>

Code behind:
  1. protected void GridView1_Editing(object sender, GridViewEditEventArgs e)
  2. {
  3. string subjectID = dropdown_subjectCode.SelectedValue.ToString();
  4. int index = Convert.ToInt32(e.NewEditIndex);
  5.  
  6. RangeValidator validator = (RangeValidator)GridView1.Rows[index].NamingContainer.FindControl("RangeValidator16");
  7. validator.MaximumValue = db.getHurdleWeightBalance(subjectID);
  8. }
Last edited by dfs3000my; 28 Days Ago at 12:23 pm.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 19
Reputation: dfs3000my is an unknown quantity at this point 
Solved Threads: 0
dfs3000my dfs3000my is offline Offline
Newbie Poster
 
0
  #2
28 Days Ago
I have found the workaround for this. More details from here.

  1. protected void GridViewRowEventHandler(Object sender, GridViewRowEventArgs e)
  2. {
  3. string subjectID = dropdown_subjectCode.SelectedValue.ToString();
  4.  
  5. if (e.Row.RowState == DataControlRowState.Edit)
  6. {
  7. RangeValidator validator = (RangeValidator)e.Row.FindControl("RangeValidator16");
  8. validator.MaximumValue = db.getHurdleWeightBalance(subjectID);
  9. }
  10. }
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC