problem with displaying radiobuttonlist value in gridview

Discussion in 'ASP .Net Web Controls' started by Ben, May 30, 2008.

  1. Ben

    Ben Guest

    Hi,

    The gridview contains a radiobuttonlist with boolean values (true/false)
    coming from a database. In normal mode, 'True' or 'False' is dispalyed. In
    edit mode, the radiobuttonlist appears.

    What i want is to change True' and 'False' by 'Yes' and 'No' in normal mode.

    I tried this code below and i get Yes/No in normal mode, but also in edit
    mode instead of the radiobuttonlist which has gone ...

    Thanks for help

    Ben

    <asp:TemplateField>
    <EditItemTemplate>
    <asp:RadioButtonList ID="r1" SelectedValue='<%# Bind("myfield") %>'
    runat="server">
    </asp:RadioButtonList>
    </EditItemTemplate>
    <ItemTemplate>
    <asp:Label ID="Label31" runat="server" Text='<%# Bind("myfield")
    %>'></asp:Label>
    </ItemTemplate>
    </asp:TemplateField>


    Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As
    System.Web.UI.WebControls.GridViewRowEventArgs) Handles
    GridView1.RowDataBound
    If (e.Row.RowState And DataControlRowState.Normal) =
    DataControlRowState.Normal Then
    If e.Row.RowType = DataControlRowType.DataRow Then
    Dim st As String
    st = e.Row.Cells(5).Text
    If st = "True" Then
    e.Row.Cells(5).Text = "Yes"
    Else
    e.Row.Cells(5).Text = "No"
    End If
    End If
    End If
    End Sub
     
    Ben, May 30, 2008
    #1
    1. Advertising

  2. Ben

    Stan Guest

    Hi Ben

    The cause of your problem lies in this fragment of code:

    >         If (e.Row.RowState And DataControlRowState.Normal) =
    > DataControlRowState.Normal Then ...


    You'll see why if you look at the list below which is the
    DataControlRowState enumeration:

    Normal = 0 (binary 0000)

    Alternate = 1 (binary 0001)

    Selected = 2 (binary 0010)

    Edit = 4 (binary 0100)

    Insert = 8 (binary 1000)

    This means that

    e.Row.RowState And DataControlRowState.Normal ...

    ... will always evaluate to 0 (x And 0 = 0 whatever x happens to be)
    and hence will always equate to DataControlRowState.Normal,
    which in turn means the overall boolean expression will always return
    true even for rows that are in Edit mode.

    Try this instead

    if(e.Row.RowState And DataControlRowState.Edit) = 0 then ...

    For a row in Edit mode Row.RowState can be 4 or 5 because it may be an
    alternate row. Either way the result will be non-zero and hence return
    false as required.
     
    Stan, May 31, 2008
    #2
    1. Advertising

  3. Ben

    Ben Guest

    Hi, thanks for replying.

    I tried this, but now, i get everywhere "No" in normal mode and the
    radiobuttonlist has gone in edit mode ...


    "Stan" <> schreef in bericht
    news:...
    Hi Ben

    The cause of your problem lies in this fragment of code:

    > If (e.Row.RowState And DataControlRowState.Normal) =
    > DataControlRowState.Normal Then ...


    You'll see why if you look at the list below which is the
    DataControlRowState enumeration:

    Normal = 0 (binary 0000)

    Alternate = 1 (binary 0001)

    Selected = 2 (binary 0010)

    Edit = 4 (binary 0100)

    Insert = 8 (binary 1000)

    This means that

    e.Row.RowState And DataControlRowState.Normal ...

    .... will always evaluate to 0 (x And 0 = 0 whatever x happens to be)
    and hence will always equate to DataControlRowState.Normal,
    which in turn means the overall boolean expression will always return
    true even for rows that are in Edit mode.

    Try this instead

    if(e.Row.RowState And DataControlRowState.Edit) = 0 then ...

    For a row in Edit mode Row.RowState can be 4 or 5 because it may be an
    alternate row. Either way the result will be non-zero and hence return
    false as required.
     
    Ben, May 31, 2008
    #3
  4. Ben

    Stan Guest

    Ben

    I should have spotted this before but there is another problem with
    your code.

    You are reading and assigning to the Text property of the cells. The
    columns are templated. That means you have to retrieve the Text
    property of the label controls that are bound to the data not the Text
    property of the grid cell containing them.

    You should be doing it something like this:

    Dim MyFieldLabel as Label = e.row.cells(5).FindControl("label31")

    If MyFieldLabel.Text = "True" Then
    MyFieldLabel.Text = "Yes"
    Else
    MyFieldLabel.Text = "No"
    End if

    As it is I cannot understand why it worked at all previously since the
    Text property of the underlying table cell could not have yielded a
    meaningful result. I'd have expected "No" everywhere because the test
    for "True" would necessarily have been false.

    Regards
    Stan
     
    Stan, Jun 2, 2008
    #4
  5. Ben

    Stan Guest

    Further to my last post I have thought of an alternative approach to
    this whole problem that would be simpler (I have used this technique
    myself in the past).

    In the item template of the "MyField" column use two labels instead of
    one (One saying "Yes" the other saying "No") and bind their visibily
    property to the Data instead - which I assume is Boolean.

    The binding expressions will be like this:

    <asp:Label ID="YesLabel" ... Text="Yes" Visible='<%# Eval("MyField")
    %>' ...

    <asp:Label ID="NoLabel" ... Text="No" Visible='<%# Not
    Eval("MyField") %>' ...

    No need for separate layout or positioning tags since only one is
    rendered at a time.

    This approach has the following advantages:

    (1) No need for any code
    (2) Only one stage to the DataBinding process instead of two.
    (3) The Edit template controls are unaffected.

    HTH
     
    Stan, Jun 2, 2008
    #5
  6. Ben

    Ben Guest

    Thanks, your solution works.
    But i also found one:

    If Not ((e.Row.RowState And DataControlRowState.Edit) =
    DataControlRowState.Edit) Then
    If e.Row.RowType = DataControlRowType.DataRow Then
    Dim myfield As Boolean = DataBinder.Eval(e.Row.DataItem, "myfield")
    Dim lbl As Label = e.Row.FindControl("Label31")
    .....



    "Stan" <> schreef in bericht
    news:...
    > Further to my last post I have thought of an alternative approach to
    > this whole problem that would be simpler (I have used this technique
    > myself in the past).
    >
    > In the item template of the "MyField" column use two labels instead of
    > one (One saying "Yes" the other saying "No") and bind their visibily
    > property to the Data instead - which I assume is Boolean.
    >
    > The binding expressions will be like this:
    >
    > <asp:Label ID="YesLabel" ... Text="Yes" Visible='<%# Eval("MyField")
    > %>' ...
    >
    > <asp:Label ID="NoLabel" ... Text="No" Visible='<%# Not
    > Eval("MyField") %>' ...
    >
    > No need for separate layout or positioning tags since only one is
    > rendered at a time.
    >
    > This approach has the following advantages:
    >
    > (1) No need for any code
    > (2) Only one stage to the DataBinding process instead of two.
    > (3) The Edit template controls are unaffected.
    >
    > HTH
     
    Ben, Jun 2, 2008
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. mg
    Replies:
    2
    Views:
    32,043
  2. James
    Replies:
    3
    Views:
    2,758
    James
    Dec 15, 2007
  3. Ben
    Replies:
    1
    Views:
    1,533
  4. Andy in South Jersey

    RadioButtonList(HTML) Control in a GridView

    Andy in South Jersey, Jul 7, 2008, in forum: ASP .Net
    Replies:
    0
    Views:
    646
    Andy in South Jersey
    Jul 7, 2008
  5. renuami
    Replies:
    0
    Views:
    3,284
    renuami
    Feb 10, 2009
Loading...

Share This Page