【问题标题】:Populate DropDownList inside Grid Template Column在网格模板列中填充 DropDownList
【发布时间】:2014-09-04 18:04:15
【问题描述】:

我在 obout 网格的模板列中有一个下拉列表。目前我正在使用 sqldatasource 填充页面加载的下拉列表。但是,我现在必须根据某个列的值加载下拉列表。例如:如果状态 = 1,我使用与状态 1 相关的可用选项列表填充下拉列表。

<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 每次都为空。我尝试了多种方法来获得它,但由于某种原因似乎无法获得它。也许另一双眼睛或其他想法可以帮助我。请让我知道我做错了什么。提前谢谢你

更新:数据绑定事件代码

<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();
}

我添加了 DataSource,因为我没有看到触发数据绑定事件的另一种方法。

【问题讨论】:

    标签: c# asp.net gridview rowdatabound obout


    【解决方案1】:

    我对@9​​87654322@ 控件一无所知,但我不得不假设它们的行为与asp.net 控件非常相似,并且刚刚被扩展。考虑到这一假设,我将尝试回答您的问题。

    您的 RowDataBound 事件有一些编码问题...例如,我不确定您为什么要定义 new DropDownList 然后尝试用下一行覆盖它。而且听起来下一行无论如何都会返回null

    首先我建议不要在行级别使用DataBound 事件。使用控件的DataBinding 事件,因为它可以更好地本地化您的代码,因为您可以触发特定控件的DataBinding,因此不必搜索它。如果您的代码更改(标记或代码隐藏),更改也容易得多,因为它不会影响其他事情,并且引入错误的空间更小。

    所以我会做出以下更改来解决这个问题:

    更改您的DropDownList 定义以实现DataBinding

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

    然后实现OnDataBinding 事件(如果您没有将它用于其他任何事情,请删除您的DataBound 事件):

    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
        ...
    }
    

    您还可以查看我很久以前回答的另一个问题,看看它是否有帮助,因为它正在做同样的事情,但使用 Repeater,但它与网格几乎相同:

    Populating DropDownList inside Repeater not working

    编辑:将代码更改为在DataBinding 中使用OboutDropDownList

    【讨论】:

    • 我的问题是我需要从第一列中获取一个值,以便仅使用允许的列表项填充下拉列表。在进行 DataBinding 时如何获取值?
    • @Alex 非常简单,在DataBinding 事件期间,您只需执行Eval 即可从您绑定的当前行数据中获取值。示例:string yourColumnValue = Eval("YourColumnName").ToString()
    • 非常感谢凯尔西的帮助!!
    • 但是,我收到“Eval()、XPath() 和 Bind() 等数据绑定方法只能在数据绑定控件的上下文中使用。”当我尝试从其中一列中提取值时。为了让我能够从中提取价值,它是否需要成为一个控件?
    • @Alex Eval 仅适用于 DataBinding 事件,DataBinding 事件仅适用于数据绑定控件。
    【解决方案2】:

    您可以在 obout 知识库和示例中找到大量示例。这些应该可以帮助您:http://www.obout.com/combobox/aspnet_integration_grid.aspx, http://www.obout.com/grid/aspnet_ajax_cascading_comboboxes.aspx

    (它们指的是组合框,但您可以轻松地将它们改编为 ddl)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-10-17
      • 1970-01-01
      • 1970-01-01
      • 2021-11-17
      • 2012-03-27
      • 1970-01-01
      • 2015-08-22
      相关资源
      最近更新 更多