User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the ASP.NET section within the Web Development category of DaniWeb, a massive community of 329,169 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,931 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our ASP.NET advertiser: Lunarpages ASP Web Hosting

Radiobutton Problem

Join Date: Mar 2008
Posts: 20
Reputation: ravichandra is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 4
ravichandra ravichandra is offline Offline
Newbie Poster

Re: Radiobutton Problem

  #8  
Mar 27th, 2008
aspx
 <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False"
        BackColor="White" BorderColor="#CC9966" BorderStyle="None" BorderWidth="1px"
        CellPadding="4" DataKeyNames="ProductID" DataSourceID="SqlDataSource1" PageSize="5"
        Style="z-index: 100; left: 241px; position: absolute; top: 65px" OnRowCreated="GridView1_RowCreated">
        <FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />
        <RowStyle BackColor="White" ForeColor="#330099" />
        <Columns>
            <asp:TemplateField HeaderText="Select">
            <ItemTemplate>
            <asp:Literal ID="RadioButtonMarkup" runat="server"></asp:Literal>
            </ItemTemplate>
            </asp:TemplateField>
            <asp:BoundField DataField="ProductID" HeaderText="ProductID" ReadOnly="True" SortExpression="ProductID" />
            <asp:BoundField DataField="ProductName" HeaderText="ProductName" SortExpression="ProductName" />
        </Columns>
        <PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" />
        <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" />
        <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" />
    </asp:GridView>

I have used Template field and within the template instead of a radiobutton i have used a Literal control which will be converted into RadioButton in code behind.

The literal control is converted into a HTML radio button and to make the ids of each radiobutton unique i am passing the rowindex of the grid.This makes all the radiobutton unique and user can select any one of the radiobutton.

When the page is displayed no radio button is checked initially.(Note* if you need you can make the first radiobutton as checked by default by removing the commented code //(!Page.IsPostBack && e.Row.RowIndex==0).

aspx.cs

 protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Literal output = (Literal)e.Row.FindControl("RadioButtonMarkup");
            output.Text=string.Format(@"<input type=radio name='ProductGroup' "+@"id='RowSelector{0}' value='{0}' 

",e.Row.RowIndex);

            if((ProductsSelectedIndex==e.Row.RowIndex))// ||(!Page.IsPostBack && e.Row.RowIndex == 0))

                output.Text += @"checked='checked' ";
            output.Text += "/>";
        

        }
    }

The following property ProductsSelectedIndex is accessed to check whether any of the radiobutton is checked, if none is

checked then it returns -1 or it return an integer value and if this value is equal to the row index then that radiobutton is

made checked.

private int ProductsSelectedIndex
    {
        get
        {
            if (string.IsNullOrEmpty(Request.Form["ProductGroup"]))
                return -1;
            else
                return Convert.ToInt32(Request.Form["ProductGroup"]);
        }
    }


Finally you may click on Button to perform whatever is required for your requirement.

Have alook at the following sample code:

protected void Button1_Click(object sender, EventArgs e)
    {
        if (ProductsSelectedIndex < 0)
        {
            Label2.Text = "Please make a selection";
            Label2.Visible = true;
        }
        else
        {
            int productId = Convert.ToInt32(GridView1.DataKeys[ProductsSelectedIndex].Value);
            Label1.Text = productId.ToString();
            Label2.Visible = false;
        }


    }

If the ProductSelectedIndex < 0, it means none of the radio button is selected and i have used a label control to display
the message "Please make a selection"
If any one the radio button is checked i am retrieving the key values and displaying it in the Label1.

If still you have doubt, i can clarify the issue.


Thanks Mr Scott Mitchel(Microsoft)
Good Luck.

ravichandra
Reply With Quote  
All times are GMT -4. The time now is 9:27 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC