问题源于code-背后一个FormView内找到一个控制 [英] Problem finding a control within a FormView from code-behind

查看:150
本文介绍了问题源于code-背后一个FormView内找到一个控制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这里,code后面......我想检索此控制,所以我可以将项目添加到下拉列表(我检索角色组添加到下拉列表中的code-后面)

Here the code behind... I'm trying to retrieve this control so I can add items to the drop down list (I'm retrieving the Role Groups to add to the drop down list in the code-behind)

Protected Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim DDRoleGroups As DropDownList
    DDRoleGroups = FormView1.FindControl("DDRoleGroup")
End Sub

下面的FormView控件:(我拿出大部分的领域,以便更容易阅读)

Here's the FormView: (I took out most of the fields so it's easier to read)

<asp:FormView ID="FormView1" runat="server" DataKeyNames="ID" 
     DataSourceID="ObjectDataSource_Vendors" 
     DefaultMode="Insert" BorderColor="DarkGray" 
     BorderStyle="Solid" BorderWidth="1px" CellPadding="4" Visible="False"> 
  <EditItemTemplate> 
  </EditItemTemplate> 
  <InsertItemTemplate>                          
    <label class="form_label">Role Group:</label><br /><asp:DropDownList ID="DDRoleGroup" 
               runat="server" Width="175px"
               EnableViewState="False"> 
              </asp:DropDownList>
   </InsertItemTemplate>
</asp:FormView>

难道可能有一个事实,即它是在Page_Load子和控制还没有acctually装了吗?做

Could it possibly have to do with the fact that it's in the Page_Load sub and the control hasn't acctually loaded yet?

谢谢,

马特

Thanks,
Matt

推荐答案

您下拉只存在于插入模式。尝试实施FormView的ModeChanged事件和检索控制,如果CURRENTMODE ==插入:

Your dropdown only exists in Insert mode. Try to implement the formview's ModeChanged event and retrieve the control if CurrentMode == Insert:

protected void FormView1_ModeChanged(object sender, EventArgs e)
{
    if (FormView1.CurrentMode == FormViewMode.Insert)
    {
        DropDownList DDRoleGroups = FormView1.FindControl("DDRoleGroup");
        // fill dropdown
    }
}

您不能在Page_Load中处理这个问题,作为表单尚未切换到插入模式。

You cannot handle this in Page_Load, as the form has not yet switched into Insert mode.

这篇关于问题源于code-背后一个FormView内找到一个控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆