D
David Hubbard
I am using a GridView to display a set of objects that have a parent-child
relationship. Each object, MyBO, has
an ID property that is used to get the children of that object.
class MyBO
{
int _id;
String _name;
public int ID
{
get { return _id; }
set { _id = value; }
}
public String Name
{
get { return _name; }
set { _name = value; }
}
public MyBO ()
{
_id = -1;
_name = "";
}
}
A service is attached to an ObjectDataSource to get the children of the
object:
class MyBOService
{
static int _counter = 0;
public MyBOService ()
{
}
public MyBO [] getMyBOs ( int id )
{
MyBO [] children = new MyBO[5];
_counter++;
for ( int i=0; i<5; i++ )
{
MyBO myBO = children[ i ] = new MyBO();
myBO.ID = (_counter * 10 ) + i;
myBO.Name = "MyBO_" + _counter.ToString() + "_" + id.ToString()
;
}
return children;
}
}
For testing their is a static counter to make the names unique. I have tried
two methods of calling the service. The
first uses a get parameter.
<asp:ObjectDataSource ID="myBODataSource"
Runat="server" TypeName="MyBOService"
SelectMethod="getMyBOs" >
<SelectParameters>
<asp:QueryStringParameter QueryStringField="ID"
DefaultValue="1" Name="ID" Direction="Input" Type="Int32" />
</SelectParameters>
</asp:ObjectDataSource>
<asp:GridView ID="myBOGridView" Runat="server"
DataSourceID="myBODataSource"
AutoGenerateColumns="False"
OnSelectedIndexChanging = "GridView2_OnSelectedIndexChanging" >
<Columns>
<asp:TemplateField HeaderText="ID">
<ItemTemplate>
<asp:HyperLink NavigateUrl='<%# Eval( "ID",
"~/Default.aspx?ID={0}" ) %>' runat="server" Text='<%# Eval( "ID" ) %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField HeaderText="Name" DataField="Name" />
</Columns>
</asp:GridView>
And this works fine. However I would like to use a post parameter by
changing the ObjectDataSource select parameters:
<asparameter DefaultValue="1" Name="ID" Direction="Input" Type="Int32" />
I am not using AutoGenerateSelect, for a cleaner UI, so I cannot hook into
the OnSelectedIndexChanged of the GridView to change the default value.
I can use the selecting event of the ObjectDataSource but how can I get the
ID selected in the GridView?
Thanks
relationship. Each object, MyBO, has
an ID property that is used to get the children of that object.
class MyBO
{
int _id;
String _name;
public int ID
{
get { return _id; }
set { _id = value; }
}
public String Name
{
get { return _name; }
set { _name = value; }
}
public MyBO ()
{
_id = -1;
_name = "";
}
}
A service is attached to an ObjectDataSource to get the children of the
object:
class MyBOService
{
static int _counter = 0;
public MyBOService ()
{
}
public MyBO [] getMyBOs ( int id )
{
MyBO [] children = new MyBO[5];
_counter++;
for ( int i=0; i<5; i++ )
{
MyBO myBO = children[ i ] = new MyBO();
myBO.ID = (_counter * 10 ) + i;
myBO.Name = "MyBO_" + _counter.ToString() + "_" + id.ToString()
;
}
return children;
}
}
For testing their is a static counter to make the names unique. I have tried
two methods of calling the service. The
first uses a get parameter.
<asp:ObjectDataSource ID="myBODataSource"
Runat="server" TypeName="MyBOService"
SelectMethod="getMyBOs" >
<SelectParameters>
<asp:QueryStringParameter QueryStringField="ID"
DefaultValue="1" Name="ID" Direction="Input" Type="Int32" />
</SelectParameters>
</asp:ObjectDataSource>
<asp:GridView ID="myBOGridView" Runat="server"
DataSourceID="myBODataSource"
AutoGenerateColumns="False"
OnSelectedIndexChanging = "GridView2_OnSelectedIndexChanging" >
<Columns>
<asp:TemplateField HeaderText="ID">
<ItemTemplate>
<asp:HyperLink NavigateUrl='<%# Eval( "ID",
"~/Default.aspx?ID={0}" ) %>' runat="server" Text='<%# Eval( "ID" ) %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField HeaderText="Name" DataField="Name" />
</Columns>
</asp:GridView>
And this works fine. However I would like to use a post parameter by
changing the ObjectDataSource select parameters:
<asparameter DefaultValue="1" Name="ID" Direction="Input" Type="Int32" />
I am not using AutoGenerateSelect, for a cleaner UI, so I cannot hook into
the OnSelectedIndexChanged of the GridView to change the default value.
I can use the selecting event of the ObjectDataSource but how can I get the
ID selected in the GridView?
Thanks