473,395 Members | 1,629 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Setting the SelectedIndex value of a RadioButtonList

Can somebody tell me what would be the syntax for having an if statement and
setting the selected index of a radiobuttonlist?
This is my first project using ASP.net and I use C#.
I have a repeater with like a table layout and in the last column I want to
have three radio buttons (for each row in repeater). The value of the radio
button should be calculated from a value from the dataset.
How can I do that? When I try to use a variable inside the <asp:listitem
..... runat=server/> tag I get an error saying that I cannnot use varibles in
a server control.
Sample code (it will be inside a table which I do no show in this sample,
for clarity)
<asp:repeater id="rptEstimates" runat="server">
<HeaderTemplate>............</HeaderTemplate>
<ItemTemplate>
<%#DataBinder.Eval(Container,"DataItem.ProjectName ")%>
...here I have a few more bound columns

...here I want to have an if statement to check the value from the daset
and to set the selected index value of the radiobuttonlist
<script runat=server>
string val=(string)ds.Fields.Item["BidResponse"];
if(val.Equals("Y"){rb1.SelectedIndex=0;}
</script> (this does not work)

<asp:RadioButtonList id="rb1" SelectedIndex=selIndex Runat=server>
<asp:ListItem Value=Y>Yes</asp:ListItem>
<asp:ListItem Value=N>No</asp:ListItem>
<asp:ListItem Value=NR>Nr</asp:ListItem>
</asp:RadioButtonList>
</ItemTemplate>
</asp:reapeater>

thank you.

Nov 18 '05 #1
4 9623
You can use
RadioButtonList.SelectedValue =(string)ds.Fields.Item["BidResponse"];
but you have to be sure that ds.Fields.Item["BidResponse"]; will return Y, N
or NR otherwise the application will throw ArgumentOutOfRangeException

Regards
Martin

"Emil" <EM******@binsky.com> wrote in message
news:u0*************@TK2MSFTNGP10.phx.gbl...
Can somebody tell me what would be the syntax for having an if statement and setting the selected index of a radiobuttonlist?
This is my first project using ASP.net and I use C#.
I have a repeater with like a table layout and in the last column I want to have three radio buttons (for each row in repeater). The value of the radio button should be calculated from a value from the dataset.
How can I do that? When I try to use a variable inside the <asp:listitem
.... runat=server/> tag I get an error saying that I cannnot use varibles in a server control.
Sample code (it will be inside a table which I do no show in this sample,
for clarity)
<asp:repeater id="rptEstimates" runat="server">
<HeaderTemplate>............</HeaderTemplate>
<ItemTemplate>
<%#DataBinder.Eval(Container,"DataItem.ProjectName ")%>
...here I have a few more bound columns

...here I want to have an if statement to check the value from the daset
and to set the selected index value of the radiobuttonlist
<script runat=server>
string val=(string)ds.Fields.Item["BidResponse"];
if(val.Equals("Y"){rb1.SelectedIndex=0;}
</script> (this does not work)

<asp:RadioButtonList id="rb1" SelectedIndex=selIndex Runat=server>
<asp:ListItem Value=Y>Yes</asp:ListItem>
<asp:ListItem Value=N>No</asp:ListItem>
<asp:ListItem Value=NR>Nr</asp:ListItem>
</asp:RadioButtonList>
</ItemTemplate>
</asp:reapeater>

thank you.

Nov 18 '05 #2
OK, and where should I have this statement?
If i try<asp:RadioButtonList ID="rbl"
SelectedIndex=<%=(string)ds.Fields.Item["BidResponse"];%>
i get an error msg: Server tags cannot contain <% ... %> constructor.
If I use it befor to have the <asp:RadioButtonList> tag like,
<% rbl.SelectedIndex=(string)ds.Fields.Item["BidResponse"]; %> I get an
error:

Compiler Error Message: CS0246: The type or namespace name 'rbl' could not
be found (are you missing a using directive or an assembly reference?)

And besides that the field contains either "Y", "N", "NR" or nulls.
Anyway thank you for your response.
"Martin Marinov" <me********@mecrossroad.bg> wrote in message
news:uT**************@TK2MSFTNGP09.phx.gbl...
You can use
RadioButtonList.SelectedValue =(string)ds.Fields.Item["BidResponse"];
but you have to be sure that ds.Fields.Item["BidResponse"]; will return Y, N or NR otherwise the application will throw ArgumentOutOfRangeException

Regards
Martin

"Emil" <EM******@binsky.com> wrote in message
news:u0*************@TK2MSFTNGP10.phx.gbl...
Can somebody tell me what would be the syntax for having an if statement and
setting the selected index of a radiobuttonlist?
This is my first project using ASP.net and I use C#.
I have a repeater with like a table layout and in the last column I want

to
have three radio buttons (for each row in repeater). The value of the

radio
button should be calculated from a value from the dataset.
How can I do that? When I try to use a variable inside the <asp:listitem
.... runat=server/> tag I get an error saying that I cannnot use varibles in
a server control.
Sample code (it will be inside a table which I do no show in this

sample, for clarity)
<asp:repeater id="rptEstimates" runat="server">
<HeaderTemplate>............</HeaderTemplate>
<ItemTemplate>
<%#DataBinder.Eval(Container,"DataItem.ProjectName ")%>
...here I have a few more bound columns

...here I want to have an if statement to check the value from the daset and to set the selected index value of the radiobuttonlist
<script runat=server>
string val=(string)ds.Fields.Item["BidResponse"];
if(val.Equals("Y"){rb1.SelectedIndex=0;}
</script> (this does not work)

<asp:RadioButtonList id="rb1" SelectedIndex=selIndex Runat=server> <asp:ListItem Value=Y>Yes</asp:ListItem>
<asp:ListItem Value=N>No</asp:ListItem>
<asp:ListItem Value=NR>Nr</asp:ListItem>
</asp:RadioButtonList>
</ItemTemplate>
</asp:reapeater>

thank you.


Nov 18 '05 #3

because the radiobuttonlist is a nested control you have to use
ItemDataBound event of the repeater control
you can use this :

<asp:repeater id="rptEstimates" runat="server" ItemDataBound="DataBoundItem>
....

<script runat="server">
void DataBoundItem(Object Sender, RepeaterItemEventArgs e) {

// This event is raised for the header, the footer, separators,
and items.

// Execute the following logic for Items and Alternating Items.
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
ListItemType.AlternatingItem) {
RadioButtonList rbl =
(RadioButtonList)e.Item.FindControl("rptEstimates" );
if ( rbl != null )
{
rbl.SelectedValue =
(string)((DataRow)e.Item.DataItem)["BidResponse"];
}
}
}

Regards,
Martin Marinov

</script>
"Emil" <EM******@binsky.com> wrote in message
news:uJ**************@TK2MSFTNGP11.phx.gbl...
OK, and where should I have this statement?
If i try<asp:RadioButtonList ID="rbl"
SelectedIndex=<%=(string)ds.Fields.Item["BidResponse"];%>
i get an error msg: Server tags cannot contain <% ... %> constructor.
If I use it befor to have the <asp:RadioButtonList> tag like,
<% rbl.SelectedIndex=(string)ds.Fields.Item["BidResponse"]; %> I get an
error:

Compiler Error Message: CS0246: The type or namespace name 'rbl' could not
be found (are you missing a using directive or an assembly reference?)

And besides that the field contains either "Y", "N", "NR" or nulls.
Anyway thank you for your response.
"Martin Marinov" <me********@mecrossroad.bg> wrote in message
news:uT**************@TK2MSFTNGP09.phx.gbl...
You can use
RadioButtonList.SelectedValue =(string)ds.Fields.Item["BidResponse"];
but you have to be sure that ds.Fields.Item["BidResponse"]; will return Y,
N
or NR otherwise the application will throw ArgumentOutOfRangeException

Regards
Martin

"Emil" <EM******@binsky.com> wrote in message
news:u0*************@TK2MSFTNGP10.phx.gbl...
Can somebody tell me what would be the syntax for having an if statement
and
setting the selected index of a radiobuttonlist?
This is my first project using ASP.net and I use C#.
I have a repeater with like a table layout and in the last column I
want to
have three radio buttons (for each row in repeater). The value of the

radio
button should be calculated from a value from the dataset.
How can I do that? When I try to use a variable inside the

<asp:listitem .... runat=server/> tag I get an error saying that I cannnot use

varibles
in
a server control.
Sample code (it will be inside a table which I do no show in this

sample, for clarity)
<asp:repeater id="rptEstimates" runat="server">
<HeaderTemplate>............</HeaderTemplate>
<ItemTemplate>
<%#DataBinder.Eval(Container,"DataItem.ProjectName ")%>
...here I have a few more bound columns

...here I want to have an if statement to check the value from the daset and to set the selected index value of the radiobuttonlist
<script runat=server>
string val=(string)ds.Fields.Item["BidResponse"];
if(val.Equals("Y"){rb1.SelectedIndex=0;}
</script> (this does not work)

<asp:RadioButtonList id="rb1" SelectedIndex=selIndex Runat=server> <asp:ListItem Value=Y>Yes</asp:ListItem>
<asp:ListItem Value=N>No</asp:ListItem>
<asp:ListItem Value=NR>Nr</asp:ListItem>
</asp:RadioButtonList>
</ItemTemplate>
</asp:reapeater>

thank you.



Nov 18 '05 #4
Thank you Martin,
based on your solution I did something that works.
public void rbl_OnItemDataBound(Object Sender, RepeaterItemEventArgs e){

if(e.Item.ItemType==ListItemType.Item||e.Item.Item Type==ListItemType.Alterna
tingItem){
RadioButtonList rbl=(RadioButtonList)e.Item.FindControl("rbl");
if(rbl !=null ){

//rbl.SelectedValue=(string)((DataRow)e.Item.DataIte m)["BidResponse"];
//this statement issues an error

string bid="";

bid=(string)DataBinder.Eval(e.Item.DataItem,"BidRe sponse");

if(bid.Equals("Y")){rbl.SelectedIndex=0;}

if(bid.Equals("N")){rbl.SelectedIndex=1;}

if(bid.Equals("NR")){rbl.SelectedIndex=2;}


}}}
"Martin Marinov" <me********@mecrossroad.bg> wrote in message
news:Og**************@TK2MSFTNGP12.phx.gbl...

because the radiobuttonlist is a nested control you have to use
ItemDataBound event of the repeater control
you can use this :

<asp:repeater id="rptEstimates" runat="server" ItemDataBound="DataBoundItem> ...

<script runat="server">
void DataBoundItem(Object Sender, RepeaterItemEventArgs e) {

// This event is raised for the header, the footer, separators,
and items.

// Execute the following logic for Items and Alternating Items.
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
ListItemType.AlternatingItem) {
RadioButtonList rbl =
(RadioButtonList)e.Item.FindControl("rptEstimates" );
if ( rbl != null )
{
rbl.SelectedValue =
(string)((DataRow)e.Item.DataItem)["BidResponse"];
}
}
}

Regards,
Martin Marinov

</script>
"Emil" <EM******@binsky.com> wrote in message
news:uJ**************@TK2MSFTNGP11.phx.gbl...
OK, and where should I have this statement?
If i try<asp:RadioButtonList ID="rbl"
SelectedIndex=<%=(string)ds.Fields.Item["BidResponse"];%>
i get an error msg: Server tags cannot contain <% ... %> constructor.
If I use it befor to have the <asp:RadioButtonList> tag like,
<% rbl.SelectedIndex=(string)ds.Fields.Item["BidResponse"]; %> I get an
error:

Compiler Error Message: CS0246: The type or namespace name 'rbl' could not
be found (are you missing a using directive or an assembly reference?)

And besides that the field contains either "Y", "N", "NR" or nulls.
Anyway thank you for your response.
"Martin Marinov" <me********@mecrossroad.bg> wrote in message
news:uT**************@TK2MSFTNGP09.phx.gbl...
You can use
RadioButtonList.SelectedValue =(string)ds.Fields.Item["BidResponse"];
but you have to be sure that ds.Fields.Item["BidResponse"]; will return
Y,
N
or NR otherwise the application will throw ArgumentOutOfRangeException

Regards
Martin

"Emil" <EM******@binsky.com> wrote in message
news:u0*************@TK2MSFTNGP10.phx.gbl...
> Can somebody tell me what would be the syntax for having an if

statement and
> setting the selected index of a radiobuttonlist?
> This is my first project using ASP.net and I use C#.
> I have a repeater with like a table layout and in the last column I want to
> have three radio buttons (for each row in repeater). The value of
the radio
> button should be calculated from a value from the dataset.
> How can I do that? When I try to use a variable inside the

<asp:listitem > .... runat=server/> tag I get an error saying that I cannnot use

varibles
in
> a server control.
> Sample code (it will be inside a table which I do no show in this

sample,
> for clarity)
> <asp:repeater id="rptEstimates" runat="server">
> <HeaderTemplate>............</HeaderTemplate>
> <ItemTemplate>
> <%#DataBinder.Eval(Container,"DataItem.ProjectName ")%>
> ...here I have a few more bound columns
>
> ...here I want to have an if statement to check the value from the

daset
> and to set the selected index value of the radiobuttonlist
> <script runat=server>
> string val=(string)ds.Fields.Item["BidResponse"];
> if(val.Equals("Y"){rb1.SelectedIndex=0;}
> </script> (this does not work)
>
> <asp:RadioButtonList id="rb1" SelectedIndex=selIndex

Runat=server>
> <asp:ListItem Value=Y>Yes</asp:ListItem>
> <asp:ListItem Value=N>No</asp:ListItem>
> <asp:ListItem Value=NR>Nr</asp:ListItem>
> </asp:RadioButtonList>
> </ItemTemplate>
> </asp:reapeater>
>
> thank you.
>
>
>



Nov 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: | last post by:
Hi, I am setting the NumericUpDown .Value property and the ValueChanged event is NOT being fired. Does this ONLY get fired when I change it on the UI and not programatically? Thanks
2
by: Alexandre Jaquet | last post by:
Hi, Under IE I could not point why I can't get the selected value from a function here the code function Lvl_P2P(url,closeIt,delay){ //ver1.0 4LevelWebs var fabricant_box; var...
2
by: Anand | last post by:
Hi All, I have a combobox with style as DropDown List. When I set the combobox.selectedIndex = 0, the first value is not showing up in the combobox and it is simply blank. Only if I...
1
by: bill yeager | last post by:
I have a listbox whose autopostback property is set to 'true'. When this event fires, I have code in the 'SelectedIndexChanged' event to capture the selectedindex of the listbox to be reset on the...
1
by: jm | last post by:
I know how to set the ddl.selectedindex = x. That is not the problem. I have a dynamically populated DropDownList populated from a database. I use the onitemdatabound to populate the list when...
2
by: ABC | last post by:
How to pre-set combo textbox's selectedindex value from querystring passed from another page? I pass the querystring to set combo text box (change selectedindex on Page_Init). Source code as: ...
1
by: swarnap | last post by:
I have a control on me screen of type <FILE>. I want to set some value to that object from the database. I tried the following code using html. <html > <head> <script type="text/javascript"> ...
8
by: Efi Merdler | last post by:
Hi, In my code I'm doing some xml transformation, at the end I receive a fully transformed html file. I'm setting the Value property of an hidden field control to have the content of this html...
2
by: comerica1 | last post by:
I have the basic structure as follows: 2 drop downs A & B. A is populated using (datasource methods - from database). B is poulated using A's selected value as input and retrieve the list...
7
by: Brad Pears | last post by:
I have something strange going on - pretty sure it used to work before - and now it does not... Why does the following code not clear a combo box? Me.cboLocation.Text = String.Empty OR ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.