c# - How To Access One UserControl from Another Using ASP.NET -
i have created user control uservote
has property find total vote on particular question.
now want call 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 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 write here...**.findcontrol(voting) voteing.objecttype =300; voteing.object id= 2;
my page markup (viewanswer.aspx
) looks like:
<klmsuc:voteing id="voteing" runat="server" />
not sure if you're trying access currentvote
value in usercontrol aspx page if are:
public partial class uservote : system.web.ui.usercontrol { private int _currentvode; private int _myvoting; // move data access oninit because 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 brevity.... } public int currentvote { { return _currentvote; } } public int myvoting { { return _myvoting; } } }
on aspx page add following directive register control:
<%@ register src="~/uservote.ascx" tagprefix="klmsuc" tagname="voteing" %> <!-- had --> <klmsuc:voteing id="voteing" runat="server" />
in aspx code-behind:
int currentvote = voteing.currentvote; int myvote = voteing.myvoting;
if you're using vs2008 or later shouldn't have use findcontrol
. if see willem's answer.
Comments
Post a Comment