pepr
pepr

Reputation: 20752

How to parametrize datasource in the user control based on ASPxGridView?

Summary: The same user control displays notes at different levels of ASPxGridView nesting. How to parametrize the user control?

Details: I want to display a ASPxGridView for Projects. Each DetailRow for the project displays the nested ASPxGridView with Milestones and another ASPxGridView Notes at the same level (inside the same DetailRow).

The Milestones grid view row can also be expanded to show details. The detail of the milestone contains also Notes -- at a different level of nesting. See the picture:nesting of gridviews

The green-framed ASPxGridView with its SqlDataSource (named dsNotes) is the user control named NotesControl.

The upper-level NotesControl displays the UNION of notes of both the project and of all the project milestones. If the user wants, he/she can expand the milestone to see only the notes related to the milestone.

The SqlDataSource of the NotesControl uses the stored procedure sp_notes with arguments @idproj and @idmilestone. The default value -1 says that the argument should not apply. This way the sp_notes can return or all notes for all projects, all notes for a given project (that is the project and its milestones), or only the given milestone notes.

Need to know: How should I correctly pass the idproj and idmilestone to the NotesControl user component?

What I tried: Earlier, my NotesControl used ..._BeforePerformDataSelect event handler to fill Session["idproj"] and Session["idmilestone"]. The SqlDataSource arguments of the dsNotes were bound to the Session. However, the Session lookup table is global, and it is not capable to store the argument values for separate instances of the NotesControl.

What is the recommended way to do that? Possibly some alternatives?

Update: Adopting the advice to introduce the NotesControl-class properties named ProjectID and MilestoneID, and having the following definition of the dsNotes datasource in the NotesControl.ascx... Can I modify the HTML fragment so, that it pass the property values somehow?

<asp:SqlDataSource ID="dsNotes" runat="server" ConnectionString="<%$ ConnectionStrings:app_db_ConnectionString %>" SelectCommand="sp_notes" SelectCommandType="StoredProcedure">
    <SelectParameters>
        <asp:SessionParameter DefaultValue="" Name="idproj" SessionField="idproj" Type="Int32" />
        <asp:SessionParameter DefaultValue="" Name="idmilestone" SessionField="idmilestone" Type="Int32" />
    </SelectParameters>
</asp:SqlDataSource>

Upvotes: 1

Views: 692

Answers (1)

Mahesh
Mahesh

Reputation: 8892

You need to create two public properties in the user control. Then set them when you want to access the user control and then use that properties to bind the datasource.

Example-

 namespace projects.WebApplication.mySystem.Controls
 {
  public partial class myusercontrol: System.Web.UI.UserControl
  {
    public event EventHandler Close;
    public event EventHandler Show;

    public int MilestoneID{get;set;}
    public int ProjectID{get;set;}

    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
        {
          yourgridview.Datasource= MyDatabaseHelper.GetGridData(MilestoneID,ProjectID);
          yourgridview.DataBind();
        }
    }
  }
 } 

UPDATE

There are two ways to achieve what you want

1) Going by your current HTML code and using session variables just update the session variables in properties set

  private int _mileStoneID = int.MinValue;
  public int MilestoneID{get{return _mileStoneID;}set{
    Session["idmilestone"] = value;
    _mileStoneID = value;
  }}

and do the same for the other property

2) You can change your ASP tag script code as below,

 <SelectParameters>
    <asp:Parameter Name="MilestoneID" Type="Int32" DefaultValue=0/>
    <asp:Parameter Name="ProjectID" Type="Int32" DefaultValue=0/>
</SelectParameters>

and then in pageload

   protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
        {
          dsNotes.SelectParameters["MilestoneID"].DefaultValue = MilestoneID.ToString();
          dsNotes.SelectParameters["ProjectID"].DefaultValue = ProjectID.ToString();
        }
    }

NOTE - Code is not tested might need to tweak as your need.

Upvotes: 1

Related Questions