Solved

Attributes for Radio Button List in datagrid

Posted on 2004-08-25
4
306 Views
Last Modified: 2008-01-16
I have a datagrid that is listing donation options.  Each donation option has a few recommended amounts.  The recommended amounts is stored in the database as a comma seperated list.  The list is then split and the resulting array is used to bind to the radio button list.  Also in each row of the datagrid is a textbox.  This is to allow values other than the recommended amounts.  What I want to happen is.....when someone selects a radio button the value is placed into the text box.  Also, when you focus on the textbox, the radio button should be deselected.  I also want this to happen without posting back.

This was fairly easy in ASP classic.....not sure how to get it to go in .NET.

What is the easiest way to accomplish this?

Thanks for your help.

Daniel
0
Comment
Question by:dbashley1
  • 2
4 Comments
 
LVL 8

Expert Comment

by:shovavnik
ID: 11892337
What you need to do is create a couple of generic javacript functions on the client-side, exactly the same way you would if you were working with classic ASP.

You'll need a function for selecting a radio button, which will update the associated textbox, and another function for the onfocus event of the textbox to deselect the radio button.  If you need help with the javascript, let us know.

The hard part is associating a set of radio buttons with a textbox.  One way that I've found convenient, is to add a "fake" attribute to my radio buttons and the textbox that gives me quick access to the associated controls on the client-side.  For example:
<asp:RadioButton id="myradios1" base="1" Text="hello" value="1" selected="true"></asp:RadioButton>
<asp:RadioButton id="myradios1" base="1" Text="hello again" value="2"></asp:RadioButton>
<asp:RadioButton id="myradios1" base="1" Text="and again" value="3"></asp:RadioButton>
<asp:TextBox id="textbox1" base="1" Text="hello"></asp:TextBox>

On the client-side, you'd response to the events:
<script for="textbox1" event="onfocus">
var obj = event.srcElement;
var base = obj.base;
var radios = document.getElementsByName( 'myradios' + base );
for( index = 0; index < radios.length; index++ ) {
  radios[ index ].checked = false;
}
</script>

That's the general idea.
0
 

Author Comment

by:dbashley1
ID: 11892407
shovanik,

That is an interesting solution....

I need a little more info.....because the number of donation options is not fixed, how do I create a script that handles all the possible text boxes?  I have attaced the code below.

<asp:DataGrid HorizontalAlign="Center" ShowHeader=False Width="660px" id="dgrCategoryOptions" AutoGenerateColumns=False DataSource='<%# getOptions(databinder.eval(container.dataItem, "idDonationCategory"))%>' Runat=server>
                                                                                    <Columns>
                                                                                          <asp:TemplateColumn>
                                                                                                <ItemTemplate>
                                                                                                      <table style="text-indent:15pt;" align="center" width="660px">
                                                                                                            <tr>
                                                                                                                  <td align="left">
                                                                                                                        <asp:RadioButtonList RepeatDirection=Horizontal DataSource='<%# getOptionIncrements(container.dataItem("DonationIncrements"))%>' Runat=server>
                                                                                                                        </asp:RadioButtonList></td>
                                                                                                                  <td align="right">
                                                                                                                        Amount:&nbsp;
                                                                                                                        <asp:TextBox ID="txtAmount" Runat="server"></asp:TextBox>
                                                                                                                  </td>
                                                                                                            </tr>
                                                                                                            <tr style="text-indent:15pt;">
                                                                                                                  <td><%# container.dataItem("DonationDescription") %></td>
                                                                                                            </tr>
                                                                                                      </table>
                                                                                                </ItemTemplate>
                                                                                          </asp:TemplateColumn>
                                                                                    </Columns>
                                                                              </asp:DataGrid>



Thanks
0
 
LVL 8

Accepted Solution

by:
shovavnik earned 500 total points
ID: 11893026
Ah. Right. I forgot about that part.

You can create a handler for the DataGrid.OnItemDataBound event and then in the code behind file add the following attribute to each textbox, in this way:

Assuming column 0 is the column with all these controls (as it is in your code):

Control textBox = e.Item.Cells[ 0 ].FindControl( "txtAmount" );
textBox.Attributes[ "onfocus" ] = "focusTextBox();";
textBox.Attributes[ "id" ] = "txtAmount" + e.Item.DataItem[ "ID" ]; // or whatever your identity column is named
textBox.Attributes[ "base" ] = e.Item.DataItem[ "ID" ]; // or whatever your identity column is named

And apply similar logic to the RadioButtonList after you add an identity (or group name?) attribute to the RadioButtonList in your aspx file:
Control radioList = e.Item.Cells[ 0 ].FindControl( "radioListGroupName" );
radioList.Attributes[ "onclick" ] = "selectRadio();";
radioList.Attributes[ "id" ] = "radioList" + e.Item.DataItem[ "ID" ]; // or whatever your identity column is named
radioList.Attributes[ "base" ] = e.Item.DataItem[ "ID" ]; // or whatever your identity column is named


And then modify my script block as follows:
<script>
function focusTextBox() {
  var obj = event.srcElement;
  var base = obj.base;
  var radios = document.getElementsByName( 'radioList' + base );
  for( index = 0; index < radios.length; index++ ) {
    radios[ index ].checked = false;
  }
}
function selectRadio() {
  var obj = event.srcElement;
  var base = obj.base;
  var textbox = document.getElementByID( 'txtAmount' + base );
  textBox.value = obj.value;
}
</script>
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Concerto provides fully managed cloud services and the expertise to provide an easy and reliable route to the cloud. Our best-in-class solutions help you address the toughest IT challenges, find new efficiencies and deliver the best application expe…
Delivering innovative fully-managed cloud services for mission-critical applications requires expertise in multiple areas plus vision and commitment. Meet a few of the people behind the quality services of Concerto.

943 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

5 Experts available now in Live!

Get 1:1 Help Now