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!

Can't Reference GridView RangeValidator in Code-Behind

Status
Not open for further replies.

Tranz2

Programmer
Jan 2, 2003
18
US
Why can't I type something like this in the page_load event:

<code>RangeValidator1.MaximumValue = "10";</code>

It refused to find the word "RangeValidator1" in the intellisense. I've seen other similar examples posted, so it is supposed to work, but it doesn't for me.


<code>
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="ID_KEY"
DataSourceID="SqlDataSource5"
EmptyDataText="There are no data records to display."
onrowediting="GridView1_RowEditing" onload="GridView1_Load"
ondatabinding="GridView1_DataBinding" ondatabound="GridView1_DataBound"
onrowdatabound="GridView1_RowDataBound">
<Columns>
<asp:CommandField ShowEditButton="True" />
<asp:TemplateField HeaderText="QTY_ORD" SortExpression="QTY_ORD">

<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="100" MinimumValue="10"
Type = "Integer"
ControlToValidate="TextBox1">*</asp:RangeValidator>
</EditItemTemplate>

<ItemTemplate>
<asp:Label ID="Label1" runat="server"
Text='<%# Bind("QTY_ORD", "{0:#,###}") %>'></asp:Label>
</ItemTemplate>


<ControlStyle Width="60px" />
<ItemStyle HorizontalAlign="Right" />
</asp:TemplateField>
</Columns>
</code>
 
You can't find the control that way because it is contained within another control, the edititemtemplate of the gridview control. You have to use FindControl() within one of the gridview's events.
 
I may have tried doing that, but no matter what I try, I get the error, "Object reference not set to an instance of an object". So I guess what I really need is a specific C# code snippet, because I'm new to this and not really sure about the specific coding.

By the way, the whole reason I want to set the RangeValidator MaximumValue manually is that I need to transfer a value from a column in the row selected for editing (RowEditing event) to the MaximumValue for the RangeValidator1 - this is because each row has different maximum (and minimum) values - these are stored in the record of the database and appear as bound fields in the GridView itself. But I'm having trouble figuring out how to do that as well (same "object reference" error). I've searched for 2 days, and try this or that, but it always gives the same error. I think if I could figure out how to find the value of the record key of the row being edited, I could then read the record in the database, find the maximum order value, and then use the RangeValidator1.MaximumValue = to set it - but I can't even do that (which is the reason for this post).
 
the control only exists in the edit template, so you need to check the row type. if it's not "edit" than exit the method.
Code:
private void onrowediting(object sender, gridviewroweventargs e)
{
   if(e.Row.RowType != datagridviewrowtype.edit) return;

   var control = e.Row.FindControl("control id") as RangeValidator;
   if(control == null) return;

   control.MinimumValue = min;
   control.MaximumValue = max;
}
this even fires for every row: header, row, alternate row, separator, footer, etc.

Jason Meckley
Programmer

faq855-7190
faq732-7259
 
Thanks very much for your response, I tied it, but I'm getting 2 errors on it when I type it in the code-behind. One error is on "datagridviewrowtype". Since C# is case sensitive, I did change it to "DataGridViewRowType", but I'm still getting the error (a red squiggly line underlines the word), "The name 'DataGridViewRowType' does not exist in the current context".

I'm also getting a red underline on "Row" in both places it is used. The error message is, "System.Web.UI.WebControls.GridViewEditEventArgs' does not contain a definition for 'Row' and no extension method 'Row' accepting a first argument of type 'System.Web.UI.WebControls.GridViewEditEventArgs' could be found (Are you missing a using directive or an assembly reference?)

Also, it's not clear what "control id" should be in your example - should it be the ID of the control validator or the validated field control? I'm assuming it should be that ID of the control validator, in this case "RangeValidator1"

Here is my code:


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


    if (e.Row.RowType != DataGridViewRowType.edit) return;
    var control = e.Row.FindControl("control id") as RangeValidator;
    if (control == null) return;
    control.MinimumValue = "250";
    control.MaximumValue = "500";
    
  }
 
that's possible, i wrote that from memory. all your assumptions are correct.

Jason Meckley
Programmer

faq855-7190
faq732-7259
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top