简体   繁体   中英

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.

Now I want to call it from an aspx page.

The control code-behind ( UserVote.ascx.cs ) looks like:

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:

<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:

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

My page markup ( viewanswer.aspx ) looks like:

<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:

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:

<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:

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:

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

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

In your ASPX code-behind:

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

If you're using VS2008 or later you shouldn't have to use FindControl . If you do then see Willem's answer .

I've used the code below for my project. It worked as well. You may set ClientIDMode as static.

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. 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; 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM