如何参数化的基础上ASPxGridView用户控件的数据源? [英] How to parametrize datasource in the user control based on ASPxGridView?

查看:197
本文介绍了如何参数化的基础上ASPxGridView用户控件的数据源?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

摘要:是在不同层次ASPxGridView嵌套相同的用户控件显示笔记。如何参数化用户控件?

详细内容:我要显示ASPxGridView对于项目。每个DetailRow项目显示与里程碑嵌套ASPxGridView 并另一个ASPxGridView 备注是在同一水平(同DetailRow内)。

里程碑网格视图行也可以展开,以显示细节。里程碑的细节也包含备注 - 在不同的级别嵌套。看到图片:

绿色框ASPxGridView其的SqlDataSource(名为 dsNotes )是 NotesControl 命名为用户控件。

上层 NotesControl 显示两个项目的说明和所有的项目里程碑联盟。如果用户需要,他/她可以展开里程碑只能看到有关里程碑的音符。

NotesControl 使用存储过程 sp_notes 带参数 @idproj @idmilestone 。默认值为 1 说,该说法不应该适用。这样, sp_notes 可以返回或所有笔记的所有项目,所有音符特定项目(即项目及其里程碑),或者只给定的里程碑笔记。

需要知道的:我应该如何正确地传递 idproj idmilestone 来在 NotesControl 用户组件?

我想什么:早些时候,我的 NotesControl 使用 ..._ BeforePerformDataSelect 事件处理器填写会话[idproj] 会话[idmilestone] 。的SqlDataSource的参数 dsNotes 被绑定到会话。但是,会话查找表是全球性的,它不能存储参数值的 NotesControl 。

什么是做到这一点的推荐的方法?可能一些替代品?

更新:采用的建议引入 NotesControl 级命名属性专案编号 MilestoneID ,并且具有 dsNotes 数据源中的 NotesControl.ascx <下面的定义/ code> ...我可以修改HTML片段的话,那它在某种程度上通过属性值?

&LT; ASP:SqlDataSource的ID =dsNotes=服务器的ConnectionString =&LT;%$的ConnectionStrings:app_db_ConnectionString %&gt;中的SelectCommand =sp_notesSelectCommandType =StoredProcedure的&GT;
    &LT; SelectParameters&GT;
        &LT; ASP:SessionParameter默认值=NAME =idprojSessionField =idprojTYPE =的Int32/&GT;
        &LT; ASP:SessionParameter默认值=NAME =idmilestoneSessionField =idmilestoneTYPE =的Int32/&GT;
    &LT; / SelectParameters&GT;
&LT; / ASP:SqlDataSource的&GT;


解决方案

您需要创建在用户控件的两个公共属性。
然后,当你要访问的用户控制设置它们,然后使用该属性绑定的数据源

示例 -

 命名空间projects.WebApplication.mySystem.Controls
 {
  公共部分类的MyUserControl:System.Web.UI.UserControl
  {
    公共事件的EventHandler关闭;
    公共事件的EventHandler显示;    公众诠释MilestoneID {获取;设置;}
    公众诠释专案编号{获取;设置;}    保护无效的Page_Load(对象发件人,EventArgs的发送)
    {
        如果(!的IsPostBack)
        {
          yourgridview.Datasource = MyDatabaseHelper.GetGridData(MilestoneID,专案编号);
          yourgridview.DataBind();
        }
    }
  }
 }


更新

有两种方式来实现你想要什么

1)当前的HTML code去,并使用会话变量只是更新设置属性会话变量

 私人INT _mileStoneID = int.MinValue;
  公众诠释MilestoneID {{返回_mileStoneID;}集合{
    会话[idmilestone] =值;
    _mileStoneID =价值;
  }}

和的其他财产做同样的

2)你可以改变你的ASP脚本标记code如下,

 &LT; SelectParameters&GT;
    &LT; ASP:参数名称=MilestoneIDTYPE =的Int32默认值= 0 /&GT;
    &LT; ASP:参数名称=专案编号类型=的Int32默认值= 0 /&GT;
&LT; / SelectParameters&GT;

,然后在页面加载

 保护无效的Page_Load(对象发件人,EventArgs的发送)
    {
        如果(!的IsPostBack)
        {
          dsNotes.SelectParameters [MilestoneID] =默认值MilestoneID.ToString()。
          dsNotes.SelectParameters [专案编号] =默认值ProjectID.ToString()。
        }
    }

注意 - code未测试可能需要调整作为您的需要。

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:

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>

解决方案

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.

这篇关于如何参数化的基础上ASPxGridView用户控件的数据源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆