2014-09-04 70 views
1

我在obout網格上的模板列中有一個下拉列表。目前我正在使用sqldatasource填充頁面加載下拉列表。但是,我現在必須根據某個列的值加載下拉列表。例如:如果狀態= 1,我與涉及到的狀態1.在網格模板列中填充DropDownList列表

<obout:Column ID="colStatus" DataField="wf_status_id" Align="center" HeaderText="Status" HeaderAlign="center" Width="130px" Wrap="true" runat="server" AllowGroupBy="true" AllowFilter="true"> 
    <TemplateSettings EditTemplateId="tmpStatusIDEdit" TemplateId="tmpStatusID" /> 
</obout:Column> 

<obout:GridTemplate runat="server" ID="tmpStatusID" > 
    <Template> 
     <%# Container.DataItem["Status"]%> 
    </Template> 
</obout:GridTemplate> 
<obout:GridTemplate runat="server" ID="tmpStatusIDEdit" ControlID="ddlStatus" ControlPropertyName="value"> 
    <Template> 
     <obout:OboutDropDownList runat="server" ID="ddlStatus" Width="100%" Height="200" MenuWidth="215" DataSourceID="sdsStatus" DataTextField="wf_status_text" DataValueField="wf_status_id" /> 
    </Template> 
</obout:GridTemplate> 

public void OnGridRowDataBound(object sender, Obout.Grid.GridRowEventArgs e) 
{ 
    if (e.Row.RowType == Obout.Grid.GridRowType.DataRow) 
    { 
      DropDownList ddlStatus = new DropDownList(); 
      ddlStatus = (DropDownList)e.Row.FindControl("ddlStatus"); 
      //LOAD DROP DOWN HERE// 
    } 
} 

當我嘗試執行此代碼可用選項的列表填充下拉列表,它表明ddlStatus爲空每次。我嘗試了很多方法來獲得這個,並由於某種原因似乎無法得到它。也許另一組眼睛或其他想法可以幫助我。請讓我知道我做錯了什麼。謝謝你的時間提前

UPDATE:DataBound事件CODE

<obout:OboutDropDownList runat="server" ID="ddlStatus" Width="100%" Height="200" MenuWidth="215" OnDataBinding="ddlStatus_DataBinding" DataSourceID="sdsStatus" DataTextField="wf_status_text" DataValueField="wf_status_id" /> 

protected void ddlStatus_DataBinding(object sender, EventArgs e) 
{ 
    Obout.Interface.OboutDropDownList ddl = (Obout.Interface.OboutDropDownList)(sender); 
    string statusID = Eval("wf_status_id").ToString(); 
} 

我添加了數據源,因爲我沒有看到另一種方式擁有數據綁定事件觸發。

回答

1

我真的不知道obout控件的任何內容,但我必須假設他們的行爲非常類似於asp.net控件,並且已經被擴展。考慮到這一假設,我會盡力回答你的問題。

您的RowDataBound事件有一些編碼問題...例如,我不確定爲什麼您要定義newDropDownList然後嘗試用下一行覆蓋它。不管怎樣,它聽起來像是下一行返回null

首先我建議不要在行級使用DataBound事件。使用控件的DataBinding事件,因爲它會使您的代碼更好地本地化,因爲您可以觸發特定控件的DataBinding,因此不必搜索它。如果你的代碼改變了(標記或代碼隱藏),它也很容易改變,因爲它不會影響其他東西,並且引入錯誤的空間更小。

所以我會做如下修改來解決這個:

更改DropDownList定義來實現DataBinding

<obout:OboutDropDownList runat="server" ID="ddlStatus" Width="100%" Height="200" 
    MenuWidth="215" OnDataBinding="ddlStatus_DataBinding" /> 

然後實現OnDataBinding事件(擺脫你DataBound事件,如果你間沒有」 t使用它爲別的):

protected void ddlStatus_DataBinding(object sender, System.EventArgs e) 
{ 
    // This will point to ddlStatus on the current row that is DataBinding so you 
    // don't have to search for it. 
    OboutDropDownList ddl = (OboutDropDownList)(sender); 

    // Now you can fill the DropDownList and set the default value how ever you like 
    ... 
} 

你也可以看到這個ot她的問題我回答了一個很久以前,看它是否有助於它在做同樣的事情,但有Repeater但它幾乎是同樣的事情作爲一個網格:

Populating DropDownList inside Repeater not working

編輯:改變在DataBinding中使用OboutDropDownList的代碼。

+0

我的問題是我需要抓住以填充從第一列中的值僅允許使用列表項目的下拉列表。如何在DataBinding中獲取值? – Alex 2014-09-10 06:08:28

+1

@Alex非常簡單,在DataBinding事件中,您可以執行一個'Eval'來從當前綁定的行數據中獲取值。示例: 'string yourColumnValue = Eval(「YourColumnName」)。ToString()' – Kelsey 2014-09-10 13:45:40

+0

非常感謝您的幫助Kelsey !! – Alex 2014-09-10 14:33:16