C#单选按钮值保存到数据库 [英] C# radio button value saved to database

查看:196
本文介绍了C#单选按钮值保存到数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个在线足球池,用户将选择他们认为会赢的球队。我正在使用单选按钮。



我正在使用'价值'因为我展示了团队Div中的名字。所以我试图抓住' Value '并将其保存到我的数据库中。



' ToString()'只生成True或False。如果我在我的单选按钮中将我的代码从' Value '更改为' Text ',那么请更改我的代码隐藏 ToString() to Text it works但它在我的首页上显示团队名称,我不想这样做。



那么有没有办法可以从我的单选按钮中获取' Value '并保存到我的数据库?



我的尝试:



I'm trying to create a online football pool where users will select a team they think will win. I'm using radio buttons for this.

I'm using 'Value' because I display the team name in a Div. So I'm trying to grab the 'Value' and save it to my database.

The 'ToString()' only produces True or False. If I change my code from 'Value' to 'Text' in my Radio Button then change my Code-behind from ToString() to Text it works but it displays the team name on my front page and I don't want to do that.

So is there a way I can grab the 'Value' from my Radio Button and save it to my Database?

What I have tried:

<asp:RadioButton ID="Team_1" GroupName="Game_1" Value="eagles" runat="server" />
 <asp:RadioButton ID="Team_2" GroupName="Game_1" Value="patriots" runat="server" />



 if (Team_1.Checked)
        {
            comm.Parameters.Add("@Pick_1", System.Data.SqlDbType.NVarChar, 50);
            comm.Parameters["@Pick_1"].Value = Team_1.ToString();
        }
        else
        {
            comm.Parameters.Add("@Pick_1", System.Data.SqlDbType.NVarChar, 50);
            comm.Parameters["@Pick_1"].Value = "Not Picked";
        }

推荐答案

您需要使用 CheckBox.Checked属性(System.Web.UI.WebControls) [ ^ ]。



关于MSDN的更多细节: RadioButton Properties(System.Web.UI.WebControls) [ ^ ]



祝你好运!
You need to use CheckBox.Checked Property (System.Web.UI.WebControls)[^].

More details on MSDN: RadioButton Properties (System.Web.UI.WebControls)[^]

Good luck!


这篇关于C#单选按钮值保存到数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆