简体   繁体   中英

Populate DropDownList inside Grid Template Column

I have a dropdownlist inside a template column on a obout grid. Currently i am populating the dropdownlist on page load with a sqldatasource. However, i now have to load the dropdownlist dependent on the value of a certain column. For example: If status = 1, i populate the dropdownlist with a list of available options that pertain to status 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//
     }
}

When i attempt to execute this code, it shows that ddlStatus is null everytime. I have a tried a multitude of ways to get this and for some reason cannot seem to get it. Perhaps aother set of eyes or other ideas could help me out. Please let me know what it is i am doing wrong. Thank you ahead of time

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

I added the DataSource because i do not see another way to have the databinding event triggered.

I don't really know anything about obout controls but I have to assume they act very similar to the asp.net controls and have just been extended. With that assumption in mind, I will try and answer your question.

Your RowDataBound event has a few coding issues... for example, I am not sure why you are defining a new DropDownList and then trying to overwrite it with the next line. Also it sounds like the next line is returning null anyways.

First off I suggest not using the DataBound event at the row level. Use the control's DataBinding event as it will localize your code a lot better because you can trigger off the specific control's DataBinding and therefore not have to search for it. If your code changes (markup or codebehind) it is also a lot easier to change as it will not affect other things and there is less room for introducing bugs.

So I would make the following changes to address this:

Change your DropDownList definition to implement DataBinding :

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

Then implement the OnDataBinding event (get rid of your DataBound event if you weren't using it for anything else):

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

You can also see this other question I answered a long time ago to see if it helps as it is doing the same thing but with a Repeater but it is pretty much the same thing as a grid:

Populating DropDownList inside Repeater not working

EDIT: Changed the code to use OboutDropDownList in the DataBinding .

you can find plenty of samples on obout knowledge base and examples. These should help you out: http://www.obout.com/combobox/aspnet_integration_grid.aspx , http://www.obout.com/grid/aspnet_ajax_cascading_comboboxes.aspx

(they refer to comboBox, but you can easily adapt them do a ddl)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM