Solved

Attributes for Radio Button List in datagrid

Posted on 2004-08-25
4
320 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

691 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