sumit
sumit

Reputation:

Need help in radiobutton within gridview

Thru radio button i am selecting the data from gridview. Here all radio buttons are selectable at a time. Actually it should not happen at a time user can select only one button which i could not do. My second problem is when i am selecting particular radiobutton that details should be displayed in text box. I have Itemid, ItemName, Quantity, Rate and Total field in gridview. These values i have inserted thru textbox so i have all the corresponding text box for all. So once i select particular radiobutton those details should be displayed in corresponding textbox. I have done the insertion coding for this but couldn't do selecting thru radiobutton and dispalying in textbox. Pls somebody help me in coding for this problem.

Thanks, sumit

Upvotes: 2

Views: 4917

Answers (3)

RedSunBeer
RedSunBeer

Reputation: 1

I read several articles on the net but none were suitable. I finally figured out my own solution without using either HTMLControls radiobutton nor using Javascript. This works for my requirement.


My Gridview html settings were as follows

    <asp:GridView ID="grdVersion" runat="server" 
        AutoGenerateColumns="false" AllowPaging="true"
        AutoGenerateEditButton="false" PageSize="10" Width="400px" 
        EmptyDataText="No records available."
        OnRowDataBound="grdVersion_RowDataBound"
        AutoGenerateSelectButton="false">
        <Columns>
            <asp:BoundField DataField="versionid" HeaderText="Version No." ItemStyle-Width="50px"
                ItemStyle-Wrap="false" HtmlEncode="true" ReadOnly="true" />
            <asp:BoundField DataField="version_date" HeaderText="Version Date" ItemStyle-Width="100px"
                ItemStyle-Wrap="false" HtmlEncode="true" ReadOnly="true" />
            <asp:BoundField DataField="remarks" HeaderText="Remarks" ItemStyle-Width="150px"
                ItemStyle-Wrap="true" HtmlEncode="true" ReadOnly="true" />

            **<asp:TemplateField HeaderText="Admin" HeaderStyle-Width="100px">
                <ItemTemplate>
                    <asp:RadioButton ID="rdCurrent" runat="server" 
                    Checked="false" Enabled="true" GroupName="rgVersion" 
                    AutoPostBack="true" 
                    OnCheckedChanged="rdCurrent_CheckChanged" />
                </ItemTemplate>**

             </asp:TemplateField>
        </Columns>
    </asp:GridView>

The server code (C#) was as follows,

   DataTable dtDataSpaceVersions; //place this inside the codebehind page class
   protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            dtDataSpaceVersions = ListDataSpaceVersions();
            ViewState["dtDataSpaceVersions"] = dtDataSpaceVersions;

            PopulateGridVersion();
        }
    }

    protected void PopulateGridVersion()
    {
        grdVersion.DataSource = dtDataSpaceVersions;
        grdVersion.DataBind();
    }



    protected void rdCurrent_CheckChanged(object sender, EventArgs e)
    {

        Control selectedVersion = ((Control)sender).Parent;

        if (ViewState["dtDataSpaceVersions"] != null)
            dtDataSpaceVersions = (DataTable)ViewState["dtDataSpaceVersions"];

        foreach (DataRow dtr in dtDataSpaceVersions.Rows)
        {
            if (dtr["versionid"].ToString() == ((System.Web.UI.WebControls.GridViewRow)selectedVersion.Parent).Cells[0].Text)
                dtr[3] = "Y";
            else
                dtr[3] = "N";
        }
        PopulateGridVersion();
    }


    protected void grdVersion_RowDataBound(object sender, GridViewRowEventArgs e)
    {

        DataRowView drv;
        if (e.Row.RowType == DataControlRowType.DataRow && e.Row.DataItem != null)
        {
            drv = (DataRowView)e.Row.DataItem;
            if ((RadioButton)(e.Row.FindControl("rdCurrent")) != null)
                if (drv.Row.ItemArray[3].ToString() == YesNo.N.ToString())
                    ((RadioButton)(e.Row.FindControl("rdCurrent"))).Checked = false;
                else
                    ((RadioButton)(e.Row.FindControl("rdCurrent"))).Checked = true;

            //setGridUserPermissionCheckBoxState(e.Row, drv);
        }
    }


   public DataTable ListDataSpaceVersions()
    {
        string sql = string.Empty;
        DataTable dt = new DataTable();
        dt.Columns.Add("versionid", typeof(String));
        dt.Columns.Add("version_date", typeof(String));
        dt.Columns.Add("remarks", typeof(String));
        dt.Columns.Add("is_current", typeof(String));

        DataRow dtr;
        dtr = dt.NewRow();
        dtr[0] = "1.1";
        dtr[1] = "12-Dec-2005";
        dtr[2] = "Campaign Information";
        dtr[3] = "N";
        dt.Rows.Add(dtr);

        dtr = dt.NewRow();
        dtr[0] = "1.2";
        dtr[1] = "06-Mar-2006";
        dtr[2] = "Sales corrections";
        dtr[3] = "N";
        dt.Rows.Add(dtr);

        dtr = dt.NewRow();
        dtr[0] = "1.3";
        dtr[1] = "24-Aug-2009";
        dtr[2] = "Invoice reconciliation";
        dtr[3] = "Y";
        dt.Rows.Add(dtr);

        dtr = dt.NewRow();
        dtr[0] = "1.4";
        dtr[1] = "30-May-2010";
        dtr[2] = "Invoices verification";
        dtr[3] = "N";
        //dtr[0][0] = "";

        dt.Rows.Add(dtr);
        return dt;
    }

Upvotes: 0

Jim Schubert
Jim Schubert

Reputation: 20357

Sumit, Don't use the html control, use the asp control:

<asp:RadioButton ID="RadioSelector" runat="server" GroupName="RadioSelectors" />

I had a similar problem in an ASP.NET class, and I followed this tutorial which worked perfectly.

Upvotes: 2

hollystyles
hollystyles

Reputation: 5039

Sounds like the classic master/detail pattern see here:

Tutorial 10: Master/Detail Using a Selectable Master GridView with a Details DetailView

You are fighting the intended workings of ASP.NET databound controls by using radio buttons. I don't like having select links either they're not exactly Web 2.0! but they can be quite easily replaced with a row click by doing this (or variation of same):

Select a row in an asp:GridView without using a Select Command

Upvotes: 1

Related Questions