C# DotNetNuke Module: GridVIew AutoGenerateEditButton is skipping over a field on update.

Posted by AlexMax on Stack Overflow See other posts from Stack Overflow or by AlexMax
Published on 2011-01-31T15:10:42Z Indexed on 2011/01/31 15:25 UTC
Read the original article Hit count: 266

Filed under:
|
|
|
|

I have a GridView with an automatically generated Edit button. I wanted some customized behavior for the Image column, since I wanted it to be a drop down list of items as opposed to a simple input field, and I also wanted some nice "fallback" in case the value in the database didn't actually exist in the drop down list.

With the code I have done so far, I have gotten the behavior I desire out of the Image field. The problem is that when i attempt to update that particular field, I get an error spit out back at me that it can't find a method to update the form with:

ObjectDataSource 'objDataSource' could not find a non-generic method 'UpdateDiscovery' that has parameters: ModuleId, Visible, Position, Title, Link, ItemId.

That's not good, because I DO have an UpdateDiscovery method. However, between Title and Link, there is supposed to be another param that belongs to the Image field, and it's not being passed. I realize that it's probably the update button doesn't know to pass that field, since it's a TemplateField and not a BoundField, and when I use Bind('image') as the selected value for the drop down list, it seems to update fine...but only as long as the field in the database when I try and edit the row actually exists, otherwise it bombs out and gives me an error about the value not existing in the drop down list.

I have the following GridView defined:

<asp:GridView ID="grdDiscoverys" runat="server" DataSourceID="objDataSource" EnableModelValidation="True" AutoGenerateColumns="false" AutoGenerateEditButton="true" AutoGenerateDeleteButton="true" DataKeyNames="ItemId" OnRowDataBound="cmdDiscovery_RowDataBound">
    <Columns>
        <asp:BoundField DataField="ItemId" HeaderText="#" ReadOnly="true" />
        <asp:BoundField DataField="Visible" HeaderText="Visible" />
        <asp:BoundField DataField="Position" HeaderText="Position" />
        <asp:TemplateField HeaderText="Image">
            <ItemTemplate>
                <asp:Label ID="lblViewImage" runat="server" />
            </ItemTemplate>
            <EditItemTemplate>
                <asp:DropDownList ID="ddlEditImage" runat="server" title="Image" DataValueField="Key" DataTextField="Value" />
            </EditItemTemplate>
        </asp:TemplateField>
        <asp:BoundField DataField="Title" HeaderText="Title" />
        <asp:BoundField DataField="Link" HeaderText="Link" />
    </Columns>
</asp:GridView>

The datasource that this is tied to:

<asp:ObjectDataSource ID="objDataSource" runat="server" TypeName="MyCompany.Modules.Discovery.DiscoveryController" SelectMethod="GetDiscoverys" UpdateMethod="UpdateDiscovery" DeleteMethod="DeleteDiscovery">
    <SelectParameters>
        <asp:QueryStringParameter Name="ModuleId" QueryStringField="mid" />
    </SelectParameters>
    <UpdateParameters>
        <asp:QueryStringParameter Name="ModuleId" QueryStringField="mid" />
    </UpdateParameters>
    <DeleteParameters>
        <asp:QueryStringParameter Name="ModuleId" QueryStringField="mid" />
    </DeleteParameters>
</asp:ObjectDataSource>

The cmdDiscovery_RowDataBound method that gets called when the row's data is bound is the following C# code:

protected void cmdDiscovery_RowDataBound(object sender, GridViewRowEventArgs e)
{
    try
    {
        if (e.Row.RowIndex >= 0)
        {
            int intImage = ((DiscoveryInfo)e.Row.DataItem).Image;

            if (grdDiscoverys.EditIndex == -1)
            {
                // View
                Label lblViewImage = ((Label)e.Row.FindControl("lblViewImage"));

                if (GetFileDictionary().ContainsKey(intImage))
                {
                    lblViewImage.Text = GetFileDictionary()[intImage];
                }
                else
                {
                    lblViewImage.Text = "Missing Image";
                }
            } else {
                // Edit
                DropDownList ddlEditImage = ((DropDownList)e.Row.FindControl("ddlEditImage"));
                ddlEditImage.DataSource = GetFileDictionary();
                ddlEditImage.DataBind();

                if (GetFileDictionary().ContainsKey(intImage))
                {
                    ddlEditImage.SelectedValue = intImage.ToString();
                }
            }
        }
    }
    catch (Exception exc)
    {
        //Module failed to load 
        Exceptions.ProcessModuleLoadException(this, exc);
    }
}

How do I make sure that the Image value in the drop down list is passed to the update function?

© Stack Overflow or respective owner

Related posts about c#

Related posts about ASP.NET