我创建了一个用户控件UserVote
,该控件具有查找特定问题的总投票的属性。
现在我想从aspx页面调用它。
控制代码隐藏(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...
}
}
控制标记(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>
我的页面代码隐藏(viewanswer.aspx.cs
)如下所示:
UserVote Voteing = (UserVote) **what i write here...**.findcontrol(voting)
Voteing.objectType =300;
Voteing.object id= 2;
我的网页标记(viewanswer.aspx
)如下所示:
<klmsuc:Voteing ID="Voteing" runat="server" />
答案 0 :(得分:1)
您必须在用户控件中声明公共属性,然后在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);
...
}
}
在您的aspx页面中,从标记中为这些属性分配值:
<klmsuc:UserVote ID="Voteing" runat="server" ObjectId="2" ObjectType="300" />
或者来自代码隐藏:
Voteing.ObjectType = 300;
Voteing.ObjectId = 2;
答案 1 :(得分:1)
不确定您是否尝试从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; }
}
}
在ASPX页面上添加以下指令以注册控件:
<%@ register src="~/UserVote.ascx" tagprefix="klmsuc" tagname="Voteing" %>
<!-- You had this already -->
<klmsuc:Voteing ID="Voteing" runat="server" />
在ASPX代码隐藏中:
int currentVote = Voteing.CurrentVote;
int myVote = Voteing.MyVoting;
如果您使用的是VS2008或更高版本,则不必使用FindControl
。如果您这样做,请参阅Willem's answer。
答案 2 :(得分:0)
我已将以下代码用于我的项目。它运作良好。您可以将ClientIDMode设置为静态。
myusercontrol myUserControl =
(myusercontrol)this.Parent.Page.FindControl("ctl00$ContentPlaceHolder1$myusercontrol1");
答案 3 :(得分:-1)
uservote Voteing = (uservote) Page.FindControl("voting");
如果这不起作用,您可以尝试一些自制版本的“FindControlRecursive”,例如,如果Page有MasterPage。我的版本(实际用作扩展名):
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;
}