Link to home
Start Free TrialLog in
Avatar of suresh pondicherry
suresh pondicherryFlag for United States of America

asked on

get radiobutton list from C# xml

Hi Experts,
In my aspx page , i need to display radio button lists from the xml input.
My xml is
<gas><level>Required</level><list><listitem i='0'>adress1, street1, state, zip  </listitem><listitem i='1'>adress1, street1, state1, zip1  </listitem><listitem i='2'>adress2, street2, state2, zip2  </listitem><listitem i='3'>adress3, street3, state3, zip3  </listitem><listitem i='4'>adress4, street4, state4, zip4  </listitem></list></gas>


i need to get
radio button1   adress1, street1, state1, zip1  
 radio button2   adress2, street2, state2, zip2  
radio button3   adress3, street3, state3, zip3  
radio button4   adress4, street4, state3, zip4  

if Radiobutton1 is selected , should display respective fields in
4 text boxes,
similarly for other radio buttons clicks.

Kind Regards,
Pooja
Avatar of nmarun
nmarun
Flag of India image

Here's a working model.

Arun

public class Data
{
    public string Key { get; set; }
    public string Value { get; set; }
}

XDocument doc = XDocument.Load(Server.MapPath("MyXml.xml"));

List<Data> dataSource = (from listitem in doc.Descendants("listitem")
                         select new Data
                                    {
                                        Key = listitem.Attribute("i").Value,
                                        Value = listitem.Value
                                    }).ToList();

rbList.DataSource = dataSource;
rbList.DataValueField = "Key";
rbList.DataTextField = "Value";
rbList.DataBind();

<asp:RadioButtonList ID="rbList" runat="server" />

Open in new window

Hello Pooja,

Please check the link:
http://niitdeveloper.blogspot.com/2010/08/generating-dynamic-controls-based-on.html

I tried for the code and hope I am able to do as you needed. I am trying to upload the code but network speed going slow. So it would take time but the code there is easy to understand.

Let us know back if your problem solved or not.

Regards,
VSS


Avatar of suresh pondicherry

ASKER

Hi Vs00saini,
Please find my code, I could get the event generated.

 private void CreateRadioButtons(ArrayList elements)      
        {
            for (int i = 0; i < elements.Count; i++)            
            {                  
                RadioButton rbutton = new RadioButton();  
               
                string itemName = string.Empty;
                string txtItem = string.Empty;
                txtItem = elements[i].ToString();
                itemName = itemName + (rbList.Items.Count + 1);

                ListItem newListItem = new ListItem(string.Format("<b>{0}</b>", txtItem), itemName, true);
               
                rbList.Items.Add(newListItem);
             
               
                rbutton.CheckedChanged += new System.EventHandler(RadioButton_CheckedChanged);    


            }        
        }
 
        private void RadioButton_CheckedChanged(object sender, EventArgs e)        
        {            
            RadioButton checkRadio = (RadioButton)sender;
            String[] arrValues=new String[4];            
            arrValues = checkRadio.Text.Split(',');              
            if (arrValues.Length > 0)              
            {                
                //txtAddress.Text = arrValues[0].ToString();                
                //txtStreet.Text = arrValues[1].ToString();                
                //txtState.Text = arrValues[2].ToString();                
                //txtZip.Text = arrValues[3].ToString();            
            }        
        }

Kind Regards,
Pooja
Hi Pooja,

Do you need some clarification for code you written or if you are showing it as the objective you achieved.

Regards,
VSS
Hi VS00saini,
I need some help and clarification. I some what dropped an idea of using xml since it ruled out my time . so i could get the source in dictionary and i could poup up the radio button list. i need to get the respective radio text selected when any any of the radio get clicked. i can send my code if u want.

Kind Regards,
Pooja
Hi,

Some doubts regarding your query:

(1) Popup the radiobutton list ... Do you want popup window to show radiobuttonlist.
(2) Get the source in dictionary ..... You mean to say the source of radiobuttonlist would come from Systen.Collections.Generic.Dictionary.

Regards,
V.S.Saini
Hi Pooja,

I think I have done the coding as you mentioned in your statement. Modify it according to your requirements.

Regards,
V.S.Saini



-----------------------------
//Default.aspx
-----------------------------
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Dictionary Collection</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <h2>Passing values from here to there</h2><hr />
    <asp:Button ID="btnGet" runat="Server" Text="Go to Second" OnClick="btnGet_Click" />
    </div>
    </form>
</body>
</html>

--------------------------
//Default.aspx.cs
--------------------------
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Specialized; //Namespace added

public partial class _Default : System.Web.UI.Page
{
    HybridDictionary hd;
    protected void Page_Load(object sender, EventArgs e)
    {
        hd = new HybridDictionary(4, true);
        hd.Add("first", "address1, street1,state,zip");
        hd.Add("second", "address2, street2,state,zip");
        hd.Add("third", "address3, street3,state,zip");
        hd.Add("fourth", "address4, street4,state,zip");
    }

    public HybridDictionary myValues
    {
        get
        {
            return hd;
        }
    }

    protected void btnGet_Click(object sender, EventArgs e)
    {
        Server.Transfer("~/Second.aspx?Dic=" + hd, true);
    }
}

------------------------------------
//Second.aspx
-------------------------------------
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Second.aspx.cs" Inherits="Second" %>
<%@ PreviousPageType VirtualPath="~/Default.aspx" %> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Create RadioButtonList</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <h2>Generating radiobuttonlist dynamically</h2><hr />
    <asp:Panel ID="panelRadio" runat="Server" BackColor="Orange"></asp:Panel>
    <br />
    Address: <asp:TextBox ID="txtAddress" runat="server"></asp:TextBox><br />
    Street: <asp:TextBox ID="txtStreet" runat="server"></asp:TextBox><br />
    State: <asp:TextBox ID="txtState" runat="server"></asp:TextBox><br />
    Zip:<asp:TextBox ID="txtZip" runat="server"></asp:TextBox>    
    </div>
    </form>
</body>
</html>

----------------------------
Second.aspx.cs
----------------------------
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Specialized;
using System.Collections.Generic;

public partial class Second : System.Web.UI.Page
{
    static int counter;
    static HybridDictionary hd2;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            hd2 = PreviousPage.myValues;
        }

            ArrayList elements = new ArrayList();

            // Loop through list
            foreach (object k in hd2.Values)
            {
               elements.Add(k);
            }              
           
            if (elements.Count > 0)
            {
                CreateRadioButtons(elements);
                counter++;
            }
    }    

    private void CreateRadioButtons(ArrayList elements)
    {
        RadioButtonList rbl = new RadioButtonList();        
        rbl.EnableViewState = true;
        rbl.AutoPostBack = true;
        rbl.SelectedIndexChanged += new EventHandler(rbl_SelectedIndexChanged);

        this.panelRadio.Controls.Add(rbl);

        if (rbl.Items.Count > 0)
        {
            rbl.Items.Clear();
        }

        for (int i = 0; i < elements.Count; i++)
        {
            // Create Radio Button
            ListItem item = new ListItem();
            item.Text = elements[i].ToString();
            item.Value = elements[i].ToString();        

            // Adding to container
            if (panelRadio.Controls.Count>0)
            {
               rbl.Items.Add(item);
            }
        }
        
    }
    protected void rbl_SelectedIndexChanged(object sender, EventArgs e)
    {
        RadioButtonList rbl2 = (RadioButtonList)sender;

        String[] arrValues = new String[4];
        arrValues = rbl2.Text.Split(',');

        if (arrValues.Length > 0)
        {
            txtAddress.Text = arrValues[0].ToString();
            txtStreet.Text = arrValues[1].ToString();
            txtState.Text = arrValues[2].ToString();
            txtZip.Text = arrValues[3].ToString();
        }
    }
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Vikram Singh Saini
Vikram Singh Saini
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial