1

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

UPDATE:DATABOUND EVENT 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();
}

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

Alex
  • 67
  • 1
  • 10

2 Answers2

1

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.

Community
  • 1
  • 1
Kelsey
  • 47,246
  • 16
  • 124
  • 162
  • My problem is i need to grab a value from the 1st column in order to populate the dropdownlist with only the allowed listitems. How would i be able to grab the values while doing DataBinding? – Alex Sep 10 '14 at 06:08
  • 1
    @Alex Very easy, during the `DataBinding` event you can just do an `Eval` to get the value out of the current row data you are binding. Example: `string yourColumnValue = Eval("YourColumnName").ToString()` – Kelsey Sep 10 '14 at 13:45
  • However, i am getting "Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control." when i attempt to pull a value from one of the columns. Does it need to be a control in order for me to be able to pull the value out of it? – Alex Sep 10 '14 at 14:43
  • @Alex `Eval` will only work in the `DataBinding` event and the `DataBinding` events only apply to data bound controls. – Kelsey Sep 10 '14 at 14:49
  • If you look at the code above, i need to retrieve the value of "wf_status_id". Any way i try to use Eval to get the value produces the same message i mentioned. Im sorry for the hassle. This is something i have never attempted to do and feel it is easy, im just missing a step. If i do Eval("wf_status_id") - doesnt work. I put wf_status_id in a Label control and did not work either. What would your suggestion be as to getting that value from that volumn? Thanks again for your help with this issue. – Alex Sep 10 '14 at 15:01
  • @Alex are you call `Eval` in the `DataBinding` event? Not `DataBound`, it has to be in the `DataBinding` event. If you are getting a context error, it is because you are not using it in the `DataBinding` event. You also want to make sure that you are implementing the `DataBinding` event for your `DropDownList` that is in your grids template. Binding and Bound are very different. Post your `DataBinding` event code. – Kelsey Sep 10 '14 at 15:16
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/60982/discussion-between-alex-and-kelsey). – Alex Sep 10 '14 at 15:24
0

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)

enricoariel
  • 483
  • 2
  • 10