如何使用单选按钮列表? [英] How to use radio button list?
问题描述
嗨程序员!
我在使用单选按钮列表时遇到了困难。我在互联网上搜索了这个但我无法理解,因为我是初学者。现在,我有一个单选按钮列表,选择是和否。如果我选择是,它将保存到数据库为0,如果否,它将保存为1.但是当我调用包含它们的数据库时,我的下一页有我的gridview,它将显示为是和否,而不是0和1.如何做到这一点?那里有人吗?
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?
推荐答案
http://runnable.com/UjnnaN9UjN8SAAAV/asp-net-how-to-use-radiobutton-list [ ^ ]
< a href =http://asp-net-example.blogspot.com/2008/10/radiobuttonlist-example-how-to-use.html> http://asp-net-example.blogspot.com/2008 /10/radiobuttonlist-example-how-to-use.html [ ^ ]
http://www.w3schools.com/aspnet/control_radiobuttonlist.asp [ ^ ]
http://runnable.com/UjnnaN9UjN8SAAAV/asp-net-how-to-use-radiobutton-list[^]
http://asp-net-example.blogspot.com/2008/10/radiobuttonlist-example-how-to-use.html[^]
http://www.w3schools.com/aspnet/control_radiobuttonlist.asp[^]
使用下面的代码
第一页的ASPX代码(插入):
Use Below code
ASPX code for first page(Insertions):
<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代码:
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代码:
ASPX Code for Display grid page:
<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>
< br $> b $ b
CS代码:
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();
}
}
这很简单。在GridView
中,你需要使用TemplateField
并在里面定义如下......
This is easy. InGridView
, you need to useTemplateField
and inside that define like below...
<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>
这里 FieldName
是来自数据库的字段或列的名称。
Here FieldName
is the name of the field or column coming from the Database.
这篇关于如何使用单选按钮列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!