如何在代码中传递Control对象 [英] How to pass Control object in code behind

查看:154
本文介绍了如何在代码中传递Control对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C#/。Net 4.0,VS2013:
我在一个aspx页面(tab标签中的tabpanel)有3个标签控件,每个包含一个ReportViewer控件,下面的示例代码。我把标签id通过PageMethods传递给后面的代码,因为'onserverclick'is not为我工作。我得到的标签,基于哪个标签被点击。基于tabid,我在选项卡中填充相应的报告。

C#/.Net 4.0, VS2013: I have 3 tab controls in an aspx page (tabpanel in div tags), each contains a ReportViewer control, sample code below. I'm passing the tab id through PageMethods to the code behind because the 'onserverclick' isnt working for me. I'm getting the tabid based on which tab is clicked. Based on the tabid, I have populate the respective report in the tab.

  <!-- Nav tabs -->
  <ul class="nav nav-tabs" role="tablist" id="reportTabs" runat="server" >
      <li role="presentation" id="tab1" class="active" runat="server" ><a  href="#tab1" aria-controls="tab1" role="tab" data-toggle="tab" >Tab 1</a></li>
      <li role="presentation" id="tab2" runat="server" ><a href="#tab2" aria-controls="tab2" role="tab" data-toggle="tab" >Tab 2</a></li>
      <li role="presentation" id="tab2" runat="server"><a href="#tab2" aria-controls="tab3" role="tab" data-toggle="tab" >Tab 3</a></li>
 </ul>`

<div role="tabpanel" class="tab-pane" id="rep1" runat="server">
  <asp:UpdatePanel>
   <ContentTemplate>
       <rsweb:ReportViewer ID="rv1" runat="server"
            Width="100%" Height="100%"
            WaitMessageFont-Size="14pt" PageCountMode="Actual" AsyncRendering="false"
            SizeToReportContent="true"
            ShowPrintButton="False"  >
        </rsweb:ReportViewer>
    </ContentTemplate>
</asp:UpdatePanel>

</div>

由于PageMethods必须是一个静态方法,所以我不能将所有方法转换为静态,同一个类的Singleton对象。在Page_Load中,我将报表ID分配给此单例实例的报表ID。我可以看到rdlc reportpath。到目前为止好。

Since PageMethods has to be a static method, I couldnt be converting all methods to static, I created a Singleton object of the same class. In Page_Load, I'm assigning the report id to this singleton instance's report id. I could see the rdlc reportpath. So far good.

 private static Reports instance = new Reports();

 protected void Page_Load(object sender, EventArgs e)
 {
     if (!IsPostBack)
     {
         this.rv1.LocalReport.ReportPath = "Report1.rdlc";
         instance.rv1 = this.rv1;
     }
 }

在另一种方法中,当我添加DataSource时, rv1变为null。

In another method, when I add the DataSource, the rv1 becomes null.

instance.rv1.LocalReport.DataSources.Add(rds);  // rv1 is null.

我做错了什么。任何其他方式来实现这一点。

What am I doing wrong. Any other ways to achieve this.

以下是WebMethod:

Here's the WebMethod:

public static  TabChanged(string liElement)//object sender, EventArgs e)
    {
            switch (liElement)
            {
                case "cpContent_tab3":
                    break;
                case "cpContent_tab1":
                    instance.PopulateRep1();
                    break;
                case "cpContent_tab2":
                    instance.PopulateRep2();
                    break;
            }
    }

    protected void PopulateRep1()
    {
        dt = GetReportDetails();
        if (dt != null)
        {
            ReportDataSource rds = new ReportDataSource("RepSummary", dt);
         // instance.rv1.LocalReport.ReportPath = "Rep1.rdlc";          
            Instance.rv1.LocalReport.DataSources.Clear();
            instance.rv1.LocalReport.DataSources.Add(rds);
        }
    }

感谢任何帮助。

推荐答案

我会备份并整理'onserverclick'问题。这将触发回发。这是至关重要的,因为它会发布你的webform和viewstate回到服务器,以便您可以与您想要的方式在服务器上的这些控件进行交互。

I would back up and sort out the 'onserverclick' issue. That will trigger a postback. That's critical because it's going to post your webform and viewstate back to the server so that you can interact with those controls on the server the way you want to.

该回发,页面的viewstate将被发送回服务器。然后服务器将知道页面上的控件的状态,然后它可以操纵它们。

When you do that postback, the page's viewstate will be sent back to the server. The server will then "know" the state of the controls on the page and then it can manipulate them.

这可能是一些小小的,你可以整理,以保持你的点击事件的触发。一旦你得到固定的休息将落到位。但是你不能绕过,并找到另一种方式与这些服务器控件交互。没有一个。

It's probably something minor you can sort out that's keeping your click event from firing. Once you get that fixed the rest will fall into place. But you can't "go around" that and find another way to interact with those server controls. There isn't one.

这篇关于如何在代码中传递Control对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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