I am using ASP.NET Web Forms. I am trying to invoke a method from a separate cs Class file. Specifically, I have an aspx file Default.aspx and a class file Class1.cs:
Here is my file: Default.aspx
public partial class Default : Page
{
for (int i = 0; i < GridView1.Rows.Count; i++)
{
If(TextBox1.Text == Label1.Text)
{
Convert.ToDouble(GridView1.Rows[i].Cells[0].Text += 1
}
}
else
{
Convert.ToDouble(GridView1.Rows[i].Cells[0].Text += 0
}
/////////////invoke SU//////////
Class1 u = new Class1();
u.SU();
}
Here is my class: Class1.cs
public partial class SU : Default
{
for (int i = 0; i < GridView1.Rows.Count; i++)
{
If(Convert.ToDouble(GridView1.Rows[i].Cells[0].Text != 1)
{
Convert.ToString(GridView1.Rows[i].Cells[1].Text == "empty"
}
}
}
Here is the GridView1 mark up from Default.aspx:
<asp:GridView ID="GridView1" runat="server" Width="219px" Height="258px">
<Columns>
<asp:BoundField AccessibleHeaderText="s1" HeaderText="k1" />
<asp:BoundField AccessibleHeaderText="s2" HeaderText="k2" />
</Columns>
</asp:GridView>;
I made the SU class inherit from the Default page to have the GridView1 control inherited in the SU class.
The problem is that when I invoke the SU class in Default.aspx I get an error message: object reference not set to an instance of an object at this line of code: for (int i = 0; i < GridView1.Rows.Count; i++) in the SU class.
Could someone help me figure out what the cause of the error is?
Well, as noted, you really don't want that class to inherit from the current page, since EACH aspx page has a hard code and generated page class created. If you inherit from ONE page, then the methods and ability of your class will be limited to the one page!!!
However, as noted, in the comments, the suggestion to pass the gridview to your class as a constructor is the road and solution here.
So, say we had this code on our page.
So, we create an instance of our class "su".
So, that class would be in su.cs.
Thus:
Now, I think the above is a "less" then ideal example for a class, but it still gives the idea here.
Pass the object (grid view) as part of the new constructor, and then any methods of that class can be used against the gv. If you only have, and always have the ONE routine to run against the gridview? (that loop)?
Then that does not suggest ANY advantage, nor need for a class in this case.
in that case, all you need is a function to call, and no need to create a object. Thus, you need some "helper" bits of code. Thus, you would and should just create a static class - and pass the gv to that function, and not need to "create" an instance of the class would be necessary here.
However, I do grasp your question was just proof of concept here. So, while this particular example is "lame" and some hard coded code for the first column of the Gridview does not warrant a class, but I suppose a static class/function if you needed that code in say a dozen places in your application, then sure, that could/would justify creating a class - but a static one probably would be better in this case.
in near all cases, passing the object from the current page makes the most sense, since each web page is in fact a defined class of its own, and attempts to inherit would mean the class you created would be married to the one page in question, and thus the resulting class would be of no use to any other web page as a result.
Also, keep in mind you CAN pass the "page" to those routines.
For example, I became VERY tired of typing code over and over to load up controls on a page.
So, I figured, why not take a datatable, and div and pass that to a general code routine that fills out the controls for me.
So, now I do this:
Note that General is a static class.
I have a routine that returns a data table,
And the a routine called FLoader is passed a "div" (called EditReocrd in this example).
What that code does is fill out the controls automatic for me that I dropped into that div on the page.
So, the above fills out the controls from data table automatic for me, and thus I would get say this:
So I can use that general Floader routine against ANY page, since I just pass the control (a div in this example) to that routine, (and I pass it a data table - with one row). So, it operates against the div from that page. Note that example is a static class - no need for such "helper" code to be a non static class.