简体   繁体   English

如何使用ASP.NET从另一个用户控件访问一个UserControl

[英]How To Access One UserControl from Another Using ASP.NET

I have created a user control UserVote that has property to find total vote on particular question. 我创建了一个用户控件UserVote ,该控件具有属性,可以查找特定问题的总票数。

Now I want to call it from an aspx page. 现在,我想从aspx页面调用它。

The control code-behind ( UserVote.ascx.cs ) looks like: 后面的控制代码( UserVote.ascx.cs )如下所示:

public partial class UserVote : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        DataTable dtVoteInfo = ShowVoteDetail(objectType, objectId);
        if (dtVoteInfo != null)
        {
            if (dtVoteInfo.Rows.Count > 0)
            {
                int.TryParse(dtVoteInfo.Rows[0]["TOTAL_VOTE"].ToString(), out currentVote);
                int.TryParse(dtVoteInfo.Rows[0]["MY_VOTING"].ToString(), out myVoting);
            }
        }
        ltrVotes.Text = currentVote.ToString();
        hfCurrentVote.Value = currentVote.ToString();
        hfMyVote.Value = myVoting.ToString();

        // ...snipped for brevity...
    }
}

The control markup ( UserVote.ascx ) looks like: 控件标记( UserVote.ascx )如下所示:

<div class="vote-cell" style="width: 46px; height: 92px">
  <img src ="~/UserControls/Vote/Images/Arrow Up.png" 
        id = "voteupoff" 
        runat = "server" alt ="vote up" 
        class="voteupImage" 
        style="height: 45px; width: 45px"/>
  <div class="vote" style ="text-align:center; color:#808185;font-weight:bold;">
    <asp:Literal ID="ltrVotes" runat="server" ></asp:Literal>
  </div>
  <img  src ="~/UserControls/Vote/Images/arrow_down.png" 
        id ="votedownoff" 
        runat = "server" alt = "vote down"
        class = "votedownImage" 
        style="height: 45px; width: 44px; margin-left: 0px;" />
</div>

My page code-behind ( viewanswer.aspx.cs ) looks like: 我的页面代码隐藏( viewanswer.aspx.cs )如下所示:

UserVote Voteing = (UserVote) **what i write here...**.findcontrol(voting)
Voteing.objectType =300;
Voteing.object id= 2;

My page markup ( viewanswer.aspx ) looks like: 我的页面标记( viewanswer.aspx )如下所示:

<klmsuc:Voteing ID="Voteing" runat="server" />

You have to declare public properties in the user control which you then use in your Page_Load event handler like this: 您必须在用户控件中声明公共属性,然后在Page_Load事件处理程序中使用它,如下所示:

public partial class UserVote : System.Web.UI.UserControl
{
    public int ObjectType { get; set; }
    public int ObjectId { get; set; }

    protected void Page_Load(object sender, EventArgs e)
    {
        DataTable dtVoteInfo = ShowVoteDetail(ObjectType, ObjectId);
        ...
    }
}

In your aspx page assign values to those properties from the markup: 在aspx页面中,为标记中的这些属性分配值:

<klmsuc:UserVote ID="Voteing" runat="server" ObjectId="2" ObjectType="300" />

Or from the code-behind: 或从后面的代码中:

Voteing.ObjectType = 300;
Voteing.ObjectId = 2;

Not sure if you're trying to access the currentVote value in the usercontrol from your ASPX page but if you are: 不知道您是否要尝试从ASPX页面访问usercontrol中的currentVote值,但是如果您是:

public partial class UserVote : System.Web.UI.UserControl
{
    private int _currentVode;
    private int _myVoting;

    // Move data access to OnInit because this otherwise Page_Load on page
    // fires before control Page_Load.
    protected override void OnInit(EventArgs e)
    {
        DataTable dtVoteInfo = ShowVoteDetail(objectType, objectId);
        if (dtVoteInfo != null)
        {
            if (dtVoteInfo.Rows.Count > 0)
            {
                int.TryParse(dtVoteInfo.Rows[0]["TOTAL_VOTE"].ToString(), 
                            out _currentVote);
                int.TryParse(dtVoteInfo.Rows[0]["MY_VOTING"].ToString(), 
                            out _myVoting);
            }
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        ltrVotes.Text = _currentVote.ToString();
        hfCurrentVote.Value = _currentVote.ToString();
        hfMyVote.Value = _myVoting.ToString();

        // set img src snipped for brevity....
    }

    public int CurrentVote
    {
        get { return _currentVote; }
    }

    public int MyVoting
    {
        get { return _myVoting; }
    }
}

On the ASPX page add the following directive to register the control: 在ASPX页面上,添加以下指令以注册控件:

<%@ register src="~/UserVote.ascx" tagprefix="klmsuc" tagname="Voteing" %>

<!-- You had this already -->
<klmsuc:Voteing ID="Voteing" runat="server" />

In your ASPX code-behind: 在您的ASPX代码中:

int currentVote = Voteing.CurrentVote;
int myVote = Voteing.MyVoting;

If you're using VS2008 or later you shouldn't have to use FindControl . 如果您使用的是VS2008或更高版本,则不必使用FindControl If you do then see Willem's answer . 如果您这样做,请参阅Willem的答案

I've used the code below for my project. 我在项目中使用了以下代码。 It worked as well. 它也很好。 You may set ClientIDMode as static. 您可以将ClientIDMode设置为静态。

myusercontrol myUserControl = 
(myusercontrol)this.Parent.Page.FindControl("ctl00$ContentPlaceHolder1$myusercontrol1");
uservote Voteing = (uservote) Page.FindControl("voting");

If that doesn't work you can try some homemade version of "FindControlRecursive", for example if the Page has a MasterPage. 如果这样不起作用,则可以尝试使用某些自制版本的“ FindControlRecursive”,例如,如果Page具有MasterPage。 My version (actually used as an extension): 我的版本(实际上用作扩展):

public Control FindControlRecursive(Control control, string id) {
    Control x = control.FindControl(id);
    foreach (Control c in control.Controls) {
        if (x == null) {
            x = c.FindControlRecursive(id);
        }
        else {
            break;                
        }                 
    }
    return x; 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用asp.net从另一个usercontrol中调用usercontrol的方法? - How can i call method from usercontrol from another usercontrol using asp.net? 如何从一个UserControl访问另一个UserControl的占位符? - How to access Placeholder from one UserControl from another UserControl? 从asp.net中的另一个用户控件调用usercontrol中的方法 - To call a method in usercontrol from another user control in asp.net Asp.Net:在更新面板中从用户控件重定向到另一个页面 - Asp.Net : Redirect to another page from usercontrol in update panel 在ASP.Net中通过循环在一个Web窗体中使用多个相同的用户控件 - Using multiple same usercontrol in one webform by a loop in ASP.Net 如何从asp.net的父aspx页中找到保留在另一个usercontrol中的用户控件? - How to find user control kept inside another usercontrol from parent aspx page in asp.net? 使用asp.net页从另一台服务器访问文件 - Access file from another server using asp.net page Asp.Net usercontrol 代码隐藏:从另一个用户控件引用嵌套的 class - Asp.Net usercontrol codebehind: reference nested class from another usercontrol 使用ASP.NET MVC 2在另一个UserControl中注册一个UserControl - Register an UserControl in another UserControl with ASP.NET MVC 2 ASP.NET母版页:-如何从母版页中删除用户控件 - ASP.NET masterpage :- how to remove usercontrol from masterpage
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM