积极答复者
关于formview中FindControl的问题

问题
-
<asp:FormView ID="FormView1" runat="server" DataKeyNames="Id" DataSourceID="ObjectDataSourcefbfy" DefaultMode="Insert" Width="650px"> <InsertItemTemplate> <asp:Label ID="Labelfbrid" runat="server" Text='<%# Bind("发布人ID") %>'></asp:Label> </td> <td> </td> <td> </td> </tr> <tr> <td>发布时间:</td> <td> <asp:Label ID="Labelshijian" runat="server" Text='<%# Bind("发布时间") %>'></asp:Label> </td> <asp:Button ID="InsertButton" runat="server" CausesValidation="True" CommandName="Insert" Text="插入" /> </td> <td> <asp:Button ID="InsertCancelButton" runat="server" CausesValidation="False" CommandName="Cancel" Text="取消" /> </InsertItemTemplate> <InsertRowStyle HorizontalAlign="Left" /> </asp:FormView> <asp:FormView ID="FormView2" runat="server" BorderColor="#FFCC66" BorderStyle="Dashed" BorderWidth="2px" DataKeyNames="Id" DataSourceID="ObjectDataSourcefbrxx" DefaultMode="Insert"> <EditItemTemplate> Id: <asp:Label ID="IdLabel" runat="server" Text='<%# Eval("Id") %>' /> <br /> 发布人ID: <asp:TextBox ID="发布人IDTextBox" runat="server" ReadOnly="True" Text='<%# Bind("发布人ID") %>' /> <br /> 所属类别: <asp:TextBox ID="所属类别TextBox" runat="server" ReadOnly="True" Text='<%# Bind("所属类别") %>' /> <br /> 固定电话: <asp:TextBox ID="固定电话TextBox" runat="server" Text='<%# Bind("固定电话") %>' /> <br /> 手机号码: <asp:TextBox ID="手机号码TextBox" runat="server" Text='<%# Bind("手机号码") %>' /> <br /> 联系地址: <asp:TextBox ID="联系地址TextBox" runat="server" Text='<%# Bind("联系地址") %>' /> <br /> 联系备注: <asp:TextBox ID="联系备注TextBox" runat="server" Text='<%# Bind("联系备注") %>' /> <br /> <br /> <asp:Button ID="UpdateButton" runat="server" CausesValidation="True" CommandName="Update" Text="更新联系方式" /> <asp:Button ID="UpdateCancelButton" runat="server" CausesValidation="False" CommandName="Cancel" Text="取消更新信息" /> </EditItemTemplate> <EmptyDataTemplate> <br /> 感谢您注册,但系统还没有您的相关联系方式,请点击下面的链接添加您的联系方式。<br /> <br /> <br /> <asp:Button ID="Button3" runat="server" CommandName="New" Text="添加我的联系方式" /> </EmptyDataTemplate> <InsertItemTemplate> 发布人ID: <asp:Label ID="faburen" runat="server" Text='<%# Bind("发布人ID") %>'></asp:Label> <br /> 所属类别: <asp:Label ID="sslb" runat="server" Text='<%# Bind("所属类别") %>'></asp:Label> <br /> 固定电话: <asp:TextBox ID="固定电话TextBox" runat="server" Text='<%# Bind("固定电话") %>' /> <br /> 手机号码: <asp:TextBox ID="手机号码TextBox" runat="server" Text='<%# Bind("手机号码") %>' /> <br /> 联系地址: <asp:TextBox ID="联系地址TextBox" runat="server" Text='<%# Bind("联系地址") %>' /> <br /> 联系备注: <asp:TextBox ID="联系备注TextBox" runat="server" Text='<%# Bind("联系备注") %>' /> <br /> <br /> <br /> <asp:Button ID="InsertButton" runat="server" CausesValidation="True" CommandName="Insert" Text="添加联系信息" /> <asp:Button ID="InsertCancelButton" runat="server" CausesValidation="False" CommandName="Cancel" Text="取消添加" /> </InsertItemTemplate> </asp:FormView>
protected void Page_Load(object sender, EventArgs e) {
//formview1的控制
string yonghuming = Page.User.Identity.Name; string nowtime = System.DateTime.Now.ToString(); Label fid = (Label)FormView1.FindControl("Labelfbrid"); Label fbsj = (Label)FormView1.FindControl("Labelshijian"); fid.Text = yonghuming; fbsj.Text = nowtime; TextBox1.Text = yonghuming; if (Roles.IsUserInRole("个人用户")) { Label1.Text = "个人用户"; } else if (Roles.IsUserInRole("其他用户")) { Label1.Text = "其他用户"; } } //formview2的控制 Label fbr = (Label)FormView2.FindControl("faburen"); Label fss = (Label)FormView2.FindControl("sslb"); if (fbr != null) { fbr.Text = yonghuming; if (Roles.IsUserInRole("个人用户")) { fss.Text = "个人用户"; } else if (Roles.IsUserInRole("其他用户")) { fss.Text = "其他用户"; } }
请大家帮我看下 为什么(//formview2的控制)以下的代码无法起效,感觉好像它找不到id为faburen和sslb的这两个控件。formview1的代码执行正常。
但是当我把formview2的控件和cs的代码移到一个全新的页面后他们就能正常。
这是为什么啊,是个bug??
另外我想把formview2的DefaultMode="edit“ 让它在转换到Insert模板时再调入(//formview2的控制),因为DefaultMode="edit“下pageload的(//formview2的控制)根本不起效(这里起不起效请高手解答下),个人觉得edit下根本就没有id为faburen和sslb的这两个控件。为确保(//formview2的控制)起效我应该如何做,加到那个事件中。
谢谢
绝不不懂装懂,不因为自己的问题低级而感到不好意思,踏踏实实的虚心学习。
- 已编辑 沉默种子 2012年9月19日 12:07