I am building a custom "Yes/No" control for a survey application. The control has 2 labels (Yes/No) and two image buttons which display a graphical version of a checkbox. What I need is simple, when the user clicks on either the yes or no, the images change to reflect the new value of the control-- as in when 'Yes' is selected, the 'yes' image button url changes to the selected graphic and the 'no' image button changes to the unselected variation. Fortunately, I have that part working ok (although I'm sure there's a better way to do it). What I need help with is exposing a value property for the control so when a user selects yes or no, the value is modified accordingly. I need to programatically be able to set or get the value of the control and it will render as such. I tried doing it the way I thought would work however, the value does not persist on postback so it's always false. When I debug, clicking "Yes" set's _value to true but when the page loads again, it's back to false. I will paste the control code and the codebehind I have now. Any help is very appreciated.
- Mike
=============== YESNOCONTROL.ASCX ========================
<asp:Table runat="server" ID="tblYesNo" CssClass="yesNoTable">
<asp:TableRow CssClass="yesNoRow">
<asp:TableCell CssClass="yesNoTableData">
<asp:LinkButton ID="lnYes" OnClick="Yes_Click" CssClass="yesNoLink" ForeColor="Black" Font-Underline="false" runat="server">Yes</asp:Li
nkButton>
<asp:ImageButton runat="server" OnClick="Yes_Click" ID="imgYes" CssClass="yesNoImage" ImageUrl="~/Images/YesNo_d
efault.jpg
" Width="25" Height="20" />
</asp:TableCell>
<asp:TableCell CssClass="yesNoTableData">
<asp:LinkButton ID="lnNo" OnClick="No_Click" CssClass="yesNoLink" ForeColor="Black" Font-Underline="false" runat="server">No</asp:Lin
kButton>
<asp:ImageButton runat="server" OnClick="No_Click" ID="imgNo" CssClass="yesNoImage" ImageUrl="~/Images/YesNo_s
elected.jp
g" Width="25" Height="20" />
</asp:TableCell>
</asp:TableRow>
</asp:Table>
=============== YESNOCONTROL.ASCX.CS ========================
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.
WebParts;
using System.Web.UI.HtmlControls
;
using System.ComponentModel;
public partial class Modules_YesNoControl : System.Web.UI.UserControl
{
private bool _value = false;
protected void Page_Load(object sender, EventArgs e)
{
}
[Bindable(true),Category("
Behavior")
,Descripti
on("Set the default value of this control"),DefaultValue(fal
se),Locali
zable(true
)]
public bool Value
{
get
{
return _value;
}
set
{
_value = value;
}
}
protected void Yes_Click(object sender, EventArgs e)
{
_value = true;
imgYes.ImageUrl = "~/Images/YesNo_selected.j
pg";
imgNo.ImageUrl = "~/Images/YesNo_default.jp
g";
}
protected void No_Click(object sender, EventArgs e)
{
_value = false;
imgYes.ImageUrl = "~/Images/YesNo_default.jp
g";
imgNo.ImageUrl = "~/Images/YesNo_selected.j
pg";
}
}