locked
GridView Bind weird issue - can't retrieve value RRS feed

  • Question

  • User-1716951449 posted

    I'm trying to do:

    CommandArgument='<%#Bind("Id")%>' OnCommand="Obj_Upd_Command"

    in LinkButton within Item/EditItemTemplate

    problem is this always yield blank.

    At the event handler, e.CommandArgument is null

    if I do this:

    <asp:TemplateField>
    <ItemTemplate>
    <asp:Label ID="xx" runat="server" Text='<%#Bind("Id")%>' />
    </ItemTemplate>
    </asp:TemplateField>

    I can see the Id value being rendered.

    *** ***

    When I purposely changed the Id to a non-exist property field (eg: Id2), I will get this expected error:

    DataBinding: 'System.Data.Entity.DynamicProxies.EmployeeLeave_....' does not contain a property with the name 'Id2'.

     This hinted the object created by EntityFramework is a DynamicProxies.

    LazyLoading is already set to false, I already tried play with Configuration.ProxyCreationEnabled = false.

    But I can't figure out what event I can use to include this code.

    Using EnitityFramework 6.1.3, running on classical .Net 4.5.2

    I also try include .AsNoTracking() in my query lamba

    all doesn't work.

    it's funny the itemTemplate label is working, accessing the field Id, but CommandArguement just keep yielding blank.

    Any suggestions/workaround?

    freaking tired

    Tuesday, July 4, 2017 12:10 PM

Answers

All replies

  • User-1716951449 posted

    1)

    now I can set the ProxyCreationEnabled = false, just within the entity retrieval method

    DynamicProxies is no longer the cause.

    but still same issue, Bind on CommandArguement yield blank value

    2)

    I remove the CommandArguement Bind in the aspx markup, doing it in code at RowDataBound event, the section that I manipulate with other controls within the row:

    (row.FindControl("MyLinkButton01") as LinkButton).CommandArgument = data.Id.ToString();

    during debugging, it is given expected value, CommandArguement is storing what I expected

    but the final render html still blank

    Wednesday, July 5, 2017 1:26 AM
  • User-1716951449 posted

    as at current troubleshooting, i believe the issue is not about the Bind, I even hardcode/fixed the value of CommandArguement

    the problem is when the postback onclick being generated, it's missing the CommandArguement

    Wednesday, July 5, 2017 3:00 AM
  • User-707554951 posted

    Hi Kelmen

    Are you solved your problem?

    if not, you could try use the gridview's rowcommand event to get ur command argument for this.

     void ProductsGridView_RowCommand(Object sender, GridViewCommandEventArgs e)
      {
        // If multiple buttons are used in a GridView control, use the
        // CommandName property to determine which button was clicked.
        if(e.CommandName=="AddPizza_Click")
        {
       int index = Convert.ToInt32(e.CommandArgument);
         //do something here
              }
      }
    

    After that, if you still have problem.

    would your please provide us with more code so that we could reproduce your problem.

    Best regards

    Cathy

    Wednesday, July 5, 2017 6:38 AM
  • User-1716951449 posted

    I already did/tried this.

    one of my earlier post about doing it in "OnDataBound", it's typo error. it is "OnRowDataBound"

    funny thing is i have few other pages using this feature in similar way, but no issues with those.

    I'm going to work on a custom control, whack at the onclick attribute when being render.

    Wednesday, July 5, 2017 7:43 AM
  • User-1716951449 posted

    I think i found what causing the issue.

    by removing the gridview OnDataBound, it's working as expected

    the codes in OnDataBound is attempt to create multiple rows of headers

    ref: https://www.aspsnippets.com/Articles/ASPNet-GridView-Group-Header-Row-Columns-and-display-Multiple-Columns-under-Single-Column.aspx

    the header manipulation does worked.

    but for whatever reason, this seems to cause the issue i'm facing. guess whatever the base doing is kind of hardcoding something with the rows/columns

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 5, 2017 9:30 AM