从同一页面内的另一个formview访问控件

时间:2014-08-27 20:39:53

标签: asp.net

我有几个表单,每个表单都有自己的DDL,我正在页面中使用。我有不同的形式,因为每个DDL需要不同的数据源。当我按下提交按钮时,它给我一个错误,它无法找到控件“ddlCategory”。我认为这是因为它的形式不同。这是标记:

<asp:FormView ID="FormView1" runat="server" DataKeyNames="ID" 
    DataSourceID="AccessDataSource1" DefaultMode="Insert" >
    <InsertItemTemplate>    
        Select a Category:<br />
        <asp:DropDownList ID="ddlCategory" runat="server" AutoPostBack="True"
            DataSourceID="AccessDataSource1" DataTextField="ORG_NAME" 
            DataValueField="ID">
        </asp:DropDownList>
   </InsertItemTemplate>
</asp:FormView>
<asp:AccessDataSource ID="AccessDataSource1" runat="server"
    DataFile="~/App_Data/webvideos.mdb"
    SelectCommand="SELECT * FROM [ORGANIZATIONS]"/>
<br />
<asp:FormView ID="FormView2" runat="server" DataKeyNames="ID" 
    DataSourceID="AccessDataSource2" DefaultMode="Insert" >
    <InsertItemTemplate>
        Select an Organization:<br />
        <asp:DropDownList ID="ddlOrg" runat="server"
            DataSourceID="AccessDataSource2" DataTextField="SectionName"
            DataValueField="ID">
        </asp:DropDownList>
    </InsertItemTemplate>
</asp:FormView>    
<asp:AccessDataSource ID="AccessDataSource2" runat="server"
    DataFile="~/App_Data/webvideos.mdb"
    SelectCommand="SELECT ID,SectionName FROM ORG_SECTIONS WHERE OrgID=@OrgID ">
    <SelectParameters>
        <asp:ControlParameter ControlID="ddlCategory"
            PropertyName="SelectedValue"
            Name="ID" Type="String"
            DefaultValue="" />
    </SelectParameters>
</asp:AccessDataSource>
<br />
<asp:FormView ID="FormView3" runat="server" DataKeyNames="ID" 
    DataSourceID="AccessDataSource3" DefaultMode="Insert" >
    <InsertItemTemplate>
        Select an Attorney:<br />
        <asp:DropDownList ID="ddlAtty" runat="server" 
            DataSourceID="AccessDataSource3" DataTextField="Expr1" DataValueField="ATTY_ID">
        </asp:DropDownList>
    </InsertItemTemplate>
</asp:FormView>
<asp:AccessDataSource ID="AccessDataSource3" runat="server" 
    DataFile="~/App_Data/webvideos.mdb" 
    SelectCommand="SELECT ATTY_ID, NAME & ' ' & INITIAL & ' ' & LASTNAME AS Expr1 FROM ATTORNEYS ORDER BY NAME & INITIAL & ' ' & LASTNAME">
</asp:AccessDataSource>

另外,如果有一种方法可以在一个formview控件中执行,我也想知道。

1 个答案:

答案 0 :(得分:0)

这样做了:

<asp:AccessDataSource ID="AccessDataSource1" runat="server"
    DataFile="~/App_Data/webvideos.mdb"
    SelectCommand="SELECT * FROM [ORGANIZATIONS]"/>

<asp:AccessDataSource ID="AccessDataSource3" runat="server" 
    DataFile="~/App_Data/webvideos.mdb" 
    SelectCommand="SELECT ATTY_ID, NAME & ' ' & INITIAL & ' ' & LASTNAME AS Expr1 FROM ATTORNEYS ORDER BY NAME & INITIAL & ' ' & LASTNAME">
</asp:AccessDataSource>
<br />
<asp:FormView ID="FormView1" runat="server" DataKeyNames="ID" 
    DataSourceID="AccessDataSource1" DefaultMode="Insert" >
    <InsertItemTemplate>    
        Select a Category:<br />
        <asp:DropDownList ID="ddlCategory" runat="server" AutoPostBack="True"
            DataSourceID="AccessDataSource1" DataTextField="ORG_NAME" 
            DataValueField="ID">
        </asp:DropDownList>
    <br />
    <asp:AccessDataSource ID="AccessDataSource2" runat="server"
        DataFile="~/App_Data/webvideos.mdb"
        SelectCommand="SELECT ID,SectionName FROM ORG_SECTIONS WHERE OrgID=@OrgID ">
        <SelectParameters>
            <asp:ControlParameter ControlID="ddlCategory"
                PropertyName="SelectedValue"
                Name="ID" Type="String"
                DefaultValue="" />
        </SelectParameters>
    </asp:AccessDataSource>
        Select an Organization:<br />
        <asp:DropDownList ID="ddlOrg" runat="server"
            DataSourceID="AccessDataSource2" DataTextField="SectionName"
            DataValueField="ID">
        </asp:DropDownList>
<br />
        Select an Attorney:<br />
        <asp:DropDownList ID="ddlAtty" runat="server" 
            DataSourceID="AccessDataSource3" DataTextField="Expr1" DataValueField="ATTY_ID">
        </asp:DropDownList>
    </InsertItemTemplate>
</asp:FormView>
<br />
<asp:Button ID="Button1" runat="server" Text="Submit" OnClick="AddRec" />

背后的代码:

protected void AddRec(object sender, EventArgs e)
{
    DropDownList ddlCategory = (DropDownList)FormView1.FindControl("ddlCategory");
    DropDownList ddlOrg = (DropDownList)FormView1.FindControl("ddlOrg");
    DropDownList ddlAtty = (DropDownList)FormView1.FindControl("ddlAtty");

    string constr = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\webvideos.mdb;";
    string cmdstr = "INSERT INTO [Org_Sec_Atty] ([OrgID], [SecID], [Atty_ID]) VALUES (?, ?, ?)";


    OleDbConnection con = new OleDbConnection(constr);
    OleDbCommand com = new OleDbCommand(cmdstr, con);

    con.Open();
    com.Parameters.AddWithValue("@OrgID", ddlCategory.SelectedValue);
    com.Parameters.AddWithValue("@SecID", ddlOrg.SelectedValue);
    com.Parameters.AddWithValue("@AttyID", ddlAtty.SelectedValue);
    com.ExecuteNonQuery();
    con.Close();
    Response.Redirect("ManageProfAffs.aspx");
}

完成了。