Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Get/Set radio button value in code behind

Status
Not open for further replies.

glyn6

Programmer
Nov 2, 2009
561
0
0
GB
I've snipped some code out but essentially I have 3 radio buttons
<asp:RadioButton ID="radBlue" runat="server" GroupName="radOptions" />
<asp:RadioButton ID="radRed" runat="server" GroupName="radOptions" />
<asp:RadioButton ID="radGold" runat="server" GroupName="radOptions" />
which are layed out seperately within <div>s so I can't use RadioButtonList and ListItem.

I'm trying to get and set the value in the c# code behind, which I believe is done using
radBlue.Checked = true;
which I'm using in the pageload event, but radGold (or whichever is the last of the options) is always checked when the page finally renders.

When I try reading the page when I save via an asp:button, no matter which radio button is actually checked the code thinks that radGold.checked == true.

Can anyone tell me what I'm missing?
Thanks
 
On further study, it's all control's values that are not being updated, including text boxes. This is the front
Code:
<%@ Page Language="C#" AutoEventWireup="false" MasterPageFile="~/mp.Master" CodeBehind="thispage.aspx.cs" Inherits="thisclass.thispage" title="title" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<asp:Label ID="lblBlue" runat="server" Text="Blue" ></asp:Label>
<asp:TextBox ID="txtBlue" runat="server"></asp:TextBox>

<asp:Button ID="btnSave" runat="server" Text="Save" UseSubmitBehavior="true" OnClick="btnSave_Click" />
</asp:Content>

And this the back
Code:
namespace thisclass{
  public partial class thispage : System.Web.UI.Page {
    protected void Page_Load(object sender, EventArgs e) {
      txtBlue.Text = "bob";
    }

    protected void btnSave_Click(object sender, System.EventArgs e) {
        string strMessage;
        strMessage = txtBlue.Text;
    }
    protected override void OnInit(System.EventArgs e)
    {
      InitializeComponent();
      base.OnInit(e);
    }
    private void InitializeComponent() {
      this.Load += new System.EventHandler(this.Page_Load);
    }
  }
}
When I click save with a breakpoint on
strMessage = txtBlue.Text;
the value of txtBlue.Text is always "bob" no matter what text I entered.

I've binned the page once and copied another page that works fine using the same master page, and changed it to the code above, but for some reason this one won't. Any ideas?
 
you need to check IsPostBack in the page load event. research the "asp.net webforms page life cycle". understanding this will help you understand how webforms works and when/where to put code.

Jason Meckley
Programmer

faq855-7190
faq732-7259
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top