Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Web Controls > problem with displaying radiobuttonlist value in gridview

Reply
Thread Tools

problem with displaying radiobuttonlist value in gridview

 
 
Ben
Guest
Posts: n/a
 
      05-30-2008
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


 
Reply With Quote
 
 
 
 
Stan
Guest
Posts: n/a
 
      05-31-2008
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.

 
Reply With Quote
 
 
 
 
Ben
Guest
Posts: n/a
 
      05-31-2008
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:e35ae60c-0deb-42d2-ba11-...
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.


 
Reply With Quote
 
Stan
Guest
Posts: n/a
 
      06-02-2008
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
 
Reply With Quote
 
Stan
Guest
Posts: n/a
 
      06-02-2008
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
 
Reply With Quote
 
Ben
Guest
Posts: n/a
 
      06-02-2008
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:c9029efc-0cee-4229-acca-...
> 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



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
GridView RadioButtonList Values renuami ASP .Net 0 02-10-2009 09:48 PM
RadioButtonList(HTML) Control in a GridView Andy in South Jersey ASP .Net 0 07-07-2008 02:18 PM
can't change radiobuttonlist displayed value in normal mode of gridview Ben ASP .Net 1 05-29-2008 07:22 PM
Updating Gridview with Radiobuttonlist James ASP .Net 3 12-15-2007 01:28 PM
Newbie problem - Changing Gridview field value (i.e. calculate a value) before clicking update VS2005 ASPX AAJ ASP .Net 0 09-06-2006 08:31 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57