newbie question - capture user control property values (ascx) to an aspx file

  • Thread starter Mr Not So Know It All
  • Start date
M

Mr Not So Know It All

newbie question - capture user control property values (ascx) to an
aspx file
sorry if this is a fundamental question. im new to c# and .Net.

is there a way to expose a property from an ascx file (user control) to
the parent aspx file. for example, let's say i have an user control
file called radiobuttons.ascx. radiobuttons.ascx inherits a compiled cs
file called radiobuttons. in radiobuttons.cs, i have a property called
radioValue. in radiobuttons.ascx i have a radiobuttonlist with two
buttons (values equal 1 and 2).

i loadcontrol the radiobuttons file (with proper casting) into an aspx
file. scenario 1 is this
i want the parent aspx file to capture the radiobutton value from
radiobuttons.ascx file.

scenario 2 is this
radiobutton.ascx is loaded into the aspx file. you click the button. it
fires an event that calls a function that gets a value from a database.
i set that value to the radioValue property from the radiobuttons.cs
file. how do i get the aspx file to capture that radioValue property
value.

thx in advance and i look forward to any help.
 
M

Mark Fitzpatrick

All you need to do is create public properties on the user control that
expose the values you want. You could map them directly to a control, or you
could perform some processing in the get accessor to determine what the
result should be.

For example:
To expose the selected value of a radiobutton list might look something
like:

public string SelectedValue
{
get
{
if(myRadioButtonList.SelectedIndex >= 0)
return myRadioButtonList.SelctedValue;
else
return string.Empty;
}
}
 
M

Mr Not So Know It All

thx Mike.
i'm trying that now. i'll let you know how it comes out.

thanks again. i really appreciate it

ric
 
M

Mr Not So Know It All

thanks again mike. i took your suggestion and kind of got it to work.
here's the code :

ASPX file -

<html xmlns="http://www.w3.org/1999/xhtml" >
<script runat="server">
void Page_Load(object s, EventArgs e)
{
string strControl;
Control ctl;
strControl = "radio";
ctl = LoadControl(strControl + ".ascx");
//((radio)ctl).choiceValue = "";
pl_radio.Controls.Add(ctl);
litResult.Text = ((radio)ctl).choiceValue;
}
</script>
<head runat="server">
<title>Radio Default File</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:placeHolder ID="pl_radio" runat="server" />
</div>
From ASCX choicevalue property set during oo button click or radio
select change :
<asp:Literal ID="litResult" runat="server" />
</form>
</body>
</html>
++++++++++++++++++++++++++++++++++++++++
ASCX file -
<%@ Control Language="C#" AutoEventWireup="true" Inherits="radio" %>
<script runat="server">
void Page_Init(object s, EventArgs e)
{
//choiceValue = "red";
}

</script>
<asp:RadioButtonList ID="rad_Choice"
OnSelectedIndexChanged="button_click" AutoPostBack="true"
runat="server">
<asp:ListItem Text="Choice 1" Value="1" Selected="true" />
<asp:ListItem Text="Choice 2" Value="2" />
</asp:RadioButtonList>
<br />
<asp:Literal ID="litChoice" runat="server" />
<br />
<asp:Button ID="btn_radio" OnClick="button_click" Text="Submit"
runat="server" />
+++++++++++++++++++++++++++++++++++++++
CS compiled - radio

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.HtmlControls;

/// <summary>
/// Summary description for radio
/// </summary>
public class radio:UserControl
{
public radio()
{
//
// TODO: Add constructor logic here
//
}

protected RadioButtonList rad_Choice;
protected Literal litChoice;
private string _choiceValue;

public string choiceValue
{
get { return _choiceValue; }
set
{
_choiceValue = value;
}
}

public void button_click(object s, EventArgs e)
{
choiceValue = rad_Choice.SelectedValue;
litChoice.Text = choiceValue;
}
}
++++++++++++++++++++++++++++++
as a test, i set choiceValue (cs property) in the ascx page_init file.
the aspx litResult was able to see that and get set during the aspx
page_load. i remarked that out and tried it with a call from the button
and radio buttons to the cs button_click method. the ascx litChoice
changed, but not the aspx litResult. i think im not setting the
litResult correctly in the aspx page_load.

please take a look and send me your suggestions.

again, i really appreciate the help.

ric
 
M

Mr Not So Know It All

i changed the choiceValue property to be more align with your
suggestion mike. it kind of works, but not when the radio button is
clicked and autopostback (submitted).

public string choiceValue
{
get
{
return rad_Choice.SelectedValue;
}
set
{
_choiceValue = value;
}
}

when i click on the radio button, the ascx litChoice changes, but not
the aspx litResult. after i click the button, then the aspx file
changes to the current radio button value.

thanks again for pointing me in the right direction. any additional
help would be greatly appreciated.
 
M

Mr Not So Know It All

i found the answer in google. by adding the loadcontrol in the aspx
page_init and then retreiving it in the page_load, i was able to get
the ascx property value after each radiobutton click.

<%@ Page Language="C#" AutoEventWireup="true" %>

<!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" >
<script runat="server">
protected Control ctl;
string strControl;
void Page_Init(object s, EventArgs e)
{
strControl = "radio";
ctl = LoadControl(strControl + ".ascx");
//((radio)ctl).choiceValue = "";
pl_radio.Controls.Add(ctl);
}
void Page_Load(object s, EventArgs e)
{
litResult.Text = ((radio)ctl).choiceValue;
}
</script>
<head runat="server">
<title>Radio Default File</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:placeHolder ID="pl_radio" runat="server" />
</div>
From ASCX choicevalue property set during button click or radio
select change :
<asp:Literal ID="litResult" runat="server" />
</form>
</body>
</html>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top