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.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" <EMiclaus@.binsky.com> wrote in message
news:u0PQBHJTEHA.204@.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.
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" <memmarinov@.mecrossroad.bg> wrote in message
news:uT2nsOJTEHA.1732@.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" <EMiclaus@.binsky.com> wrote in message
> news:u0PQBHJTEHA.204@.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.
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" <EMiclaus@.binsky.com> wrote in message
news:uJJrfvJTEHA.3768@.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" <memmarinov@.mecrossroad.bg> wrote in message
> news:uT2nsOJTEHA.1732@.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" <EMiclaus@.binsky.com> wrote in message
> > news:u0PQBHJTEHA.204@.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.
> > >
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,"BidResponse");
if(bid.Equals("Y")){rbl.SelectedIndex=0;}
if(bid.Equals("N")){rbl.SelectedIndex=1;}
if(bid.Equals("NR")){rbl.SelectedIndex=2;}
}}}
"Martin Marinov" <memmarinov@.mecrossroad.bg> wrote in message
news:Ogp3FRUTEHA.3664@.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" <EMiclaus@.binsky.com> wrote in message
> news:uJJrfvJTEHA.3768@.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" <memmarinov@.mecrossroad.bg> wrote in message
> > news:uT2nsOJTEHA.1732@.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" <EMiclaus@.binsky.com> wrote in message
> > > news:u0PQBHJTEHA.204@.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.
> > > > > > > >
0 comments:
Post a Comment