Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Programmers!

I am having a hard time in using the radio button list. I searched over the internet about that but I cannot understand since I am a beginner. Now, I have a radio button list with the choices of Yes and No. If I select Yes, it will save to the database as 0 and if No, it will save as 1. But when I call the database that contains them,on my next page that has my gridview, it will display as Yes and No and not 0 and 1. How to do that? Anyone there?
Posted
Updated 12-Aug-14 22:25pm
v2

Use Below code

ASPX code for first page(Insertions):

XML
<table>
            <tr>
                <td>
                    Name:
                </td>
                <td>
                    <asp:TextBox ID="txtName" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    Age:
                </td>
                <td>
                    <asp:TextBox ID="txtAge" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    Yes/No:
                </td>
                <td>
                    <asp:RadioButtonList ID="RadioButtonList1" runat="server">
                        <asp:ListItem Selected="False" Value="1">Yes</asp:ListItem>
                        <asp:ListItem Selected="False" Value="0">No</asp:ListItem>
                    </asp:RadioButtonList>
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    <asp:Button ID="btnSave" runat="server" Text="Save" OnClick="btnSave_Click" />
                </td>
            </tr>
        </table>



CS CODE:

protected void btnSave_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection("Your Connection String");
        con.Open();
        SqlCommand cmd = new SqlCommand("Insert into tablename values('" + txtName.Text + "'," + txtAge.Text + "," + RadioButtonList1.SelectedValue + ")", con);
        cmd.ExecuteNonQuery();
        con.Close();
        Response.Redirect("~/DisplayGrid.aspx");
    }



ASPX Code for Display grid page:

XML
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
                <Columns>
                    <asp:BoundField HeaderText="Name" DataField="Name" />
                    <asp:BoundField HeaderText="Age" DataField="Age" />
                    <asp:TemplateField HeaderText="Yes/No">
                        <ItemTemplate>
                            <%# (Eval("Yes/No").ToString()) == "1" ? "Yes" : "No" %>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>



CS CODE:

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            SqlConnection con = new SqlConnection("Your connection String");
            SqlDataAdapter da = new SqlDataAdapter("select * from tablename", con);
            DataSet ds = new DataSet();
            da.Fill(ds);
            GridView1.DataSource = ds.Tables[0];
            GridView1.DataBind();
        }
    }
 
Share this answer
 
Comments
DarkDreamer08 13-Aug-14 21:42pm    
I tried your logic and it runs great. But I have a problem.In my grid view, I add there a Command edit. Now, I did also the same thing in the radio button list but it is not change whenever I edited something. Why?
ClimerChinna 14-Aug-14 0:25am    
I couldn't understand your problem,could you explain it more clearly...
DarkDreamer08 14-Aug-14 0:37am    
the logic you have above is like for inserting new events which is correct. Now, what if the records I have in my grid view needed to be edited? Same form, so I copied it and put it inside my code and I change the query to update,. But nothing happens.
ClimerChinna 14-Aug-14 0:58am    
can you post your code for editing so that i can change them accordingly..
DarkDreamer08 14-Aug-14 1:07am    
CS CODE
//checklist editing
protected void checklist_modalpopup_save_Click(object sender, EventArgs e)
{
connection();
amicassaCon.Open();
SqlCommand cmd = new SqlCommand("UPDATE checklist SET checklist_code='" + lblchecklist_popup_code.Text + "',order_no='" + lblchecklist_popup_orderno.Text + "',checklist_status='" + rrd_checklist_popup_status.SelectedIndex + "',repeatable = '" + rrdchecklist_popup_repeatable.SelectedIndex + "' , checklist_type='" + rrdchecklist_popup_type.SelectedIndex + "' WHERE checklist_name = '" + lblchecklist_popup_name.Text + "'", amicassaCon);
SqlDataReader dr = cmd.ExecuteReader();
dr.Close();
amicassaCon.Close();
connection();
checklist();
}

MARK UP Language
<%-- edit checklist popup --%>
<asp:LinkButton Text="" ID="lnkFake2" runat="server" />
<cc1:ModalPopupExtender ID="checklist_popup" runat="server" PopupControlID="checklistpnlPopup" TargetControlID="lnkFake2"
CancelControlID="btnClosechecklist" BackgroundCssClass="checklistmodalBackground">

<asp:Panel ID="checklistpnlPopup" runat="server" CssClass="checklist_modalPopup" Style="display: none">
<div class="header">
Edit Checklist
</div>
<div class="body">
<table border="0" style="text-align: left" align="center">
<tr>
<td style="vertical-align: top;">
<asp:Label ID="Label12" runat="server" Text="Checklist Code:">
</td>
<td style="vertical-align: top;">
<asp:TextBox ID="lblchecklist_popup_code" runat="server">

</td>
</tr>
<tr>
<td style="vertical-align: top;">
<asp:Label ID="Label13" runat="server" Text="Order Number:">
</td>
<td style="vertical-align: top;">
<asp:TextBox ID="lblchecklist_popup_orderno" runat="server">

</td>
</tr>
<tr>
<td style="vertical-align: top;">
<asp:Label ID="Label14" runat="server" Text="ChecklistName:">     
</td>
<td style="vertical-align: top;">
<asp:Label ID="lblchecklist_popup_name" runat="server" Text="" Font-Bold="true">
</td>
</tr>
<tr>
<td style="vertical-align:top">
<asp:Label ID="lblCheckStatus" runat="server">
</td>
<td style="vertical-align:top">
<asp:RadioButtonList ID="rrd_checklist_popup_status" runat="server">
<asp:ListItem Selected="False" Value="1">Active
<asp:ListItem Selected="False" Value="0">Inac
This is easy. In GridView, you need to use TemplateField and inside that define like below...
XML
<asp:templatefield xmlns:asp="#unknown">
    <itemtemplate>
        <asp:RadioButtonList runat="server" ID="rblSomeName" SelectedValue='<%# Bind("FieldName") %>' RepeatDirection="Horizontal">
            <asp:ListItem Value="1" Text="No" />
            <asp:ListItem Value="0" Text="Yes" />
        </asp:RadioButtonList>
    </itemtemplate>
</asp:templatefield>

Here FieldName is the name of the field or column coming from the Database.
 
Share this answer
 
Comments
DarkDreamer08 13-Aug-14 4:11am    
But only 0 or 1 can be found at the database when you select either yes or no at the form. How am I going to do that yes and no will display at my grid view. Another thing, if I selected Yes, it will save to my database as 0 and if No, it will be 1 after clicking the SAVE button. Also, the radio button list should not be inside my grid view. what is inside my grid view are only the records that has been called from the database.
I am confused. Don't getting you. Please explain clearly. Where you want to show?
DarkDreamer08 13-Aug-14 5:24am    
I am sorry. At first page, I have 2 textbox for name and age , a radio button list that has 2 choices: Yes and No and a Save button. Now, If I select Yes in the radio button list, it will be saved as 0 in the database, if No- it will save as 1. Now on my second page, I have a grid view. Here, it will display all the records saved in the database. It should display the name, age and Yes/No.
Okay, so you don't want to have RadioButtonList inside the Grid? Do you want to simply show on a Label inside GridView?
DarkDreamer08 13-Aug-14 5:30am    
No, there should NOT be a radio button list inside my grid view. What I need is a tutorial on how to create a function that if I select Yes, it will save as 0 in my database and if No, it will save as 1. And what will display at my grid view is something like this:

Name Age Yes/No
Billy 19 Yes
Sarah 18 Yes
Channing 22 Yes
Cha 18 No

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900