2009-08-06 80 views
3

我有一個GridView控件就像一個TemplateField:瞭解ObjectDataSource控件和選擇參數

<asp:TemplateField ItemStyle-Width="150px"> 
    <ItemTemplate> 
     <asp:DropDownList ID="ddlFields" runat="server" DataSourceID="odsOperator" DataTextField="Text" DataValueField="Value" /> 
     <asp:HiddenField ID="hfFieldType" runat="server" Value='<%# Eval("FieldType")%>' /> 
    </ItemTemplate> 
</asp:TemplateField> 

我有我想從一個ObjectDataSource控件來填充下拉列表中,但對於每一行我想傳遞一個選擇參數等等

:它與正確的價值觀

<asp:ObjectDataSource ID="odsOperator" runat="server" TypeName="OperatorFieldsDAO" 
    SelectMethod="FindByType">  
    <SelectParameters> 
     <asp:ControlParameter ControlID="hfFieldType" Type="String" Name="Type" PropertyName="Value" /> 
    </SelectParameters> 
</asp:ObjectDataSource> 

我OperatorFieldsDAO類是填充

所有這一切都告訴你,我得到一個錯誤:

Could not find control 'hfFieldType' in ControlParameter 'Type'.

我在做什麼錯?

是否需要使用OnRowDataBound方法以編程方式傳遞所選參數?

回答

3

得到這個工作我添加創建兩個方法(一個用於GridView和其他ObjectDataSource),以及將選擇參數從ControlParameter更改爲正常參數。

的想法是每一次設置參數它創建的行...

protected void gvSearch_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
    if (e.Row.RowType == DataControlRowType.DataRow) 
    { 
     DropDownList d = (DropDownList)e.Row.FindControl("ddlFields"); 
     string type = ((HiddenField)e.Row.FindControl("hfFieldType")).Value; 

     _type = type; 
     d.DataBind(); 
    } 
} 
protected void odsOperator_Selecting(object sender, ObjectDataSourceSelectingEventArgs e) 
{ 
    e.InputParameters["Type"] = _type; 
} 

private string _type = ""; 

和ObjectDataSource控件將

<asp:ObjectDataSource ID="odsOperator" runat="server" TypeName="OperatorFieldsDAO" 
    SelectMethod="FindByType" onselecting="odsOperator_Selecting"> 
    <SelectParameters> 
     <asp:Parameter Type="String" Name="Type" /> 
    </SelectParameters> 
</asp:ObjectDataSource> 

我希望它能幫助任何人......

1

既然你:

<asp:HiddenField ID="hfFieldType" runat="server" .../> 

在您的視圖的TemplateField,有可能是沒有,有一個或網頁上該領域的許多實例。因此hfFieldType不會是一個唯一的ID,事實上,實際的ID將在運行時確定。

因此,您的控制參數找不到它,因爲它正在頁面上某處稱爲hfFieldType的控件的屬性中查找其值。

我在幾年內沒有使用ObjectDataSource,但我懷疑你可能想要掛鉤到Selecting事件。

+0

感謝您點亮 – balexandre 2009-08-07 06:36:44

0

ObjectDataSource無法找到用於輸入的控件,除非它在標記中靠近。數據源必須位於包含用於輸入的控件的標籤內。 這似乎是一個範圍界定問題。

+0

歡迎使用計算器!如果可能的話,最好爲您的解決方案提供一個示例代碼,以提高發布的準確性:) – 2012-10-21 12:11:40