views:

26

answers:

1

I've created a custom server control in ASP.NET to render a standard checkbox and a hidden field like this:

public class CheckAllBox : WebControl
{

    private string checkboxClientID;

    protected override void OnInit(EventArgs e)
    {
        checkboxClientID = String.Format("{0}{1}chbCheckAll", base.ClientID, base.ClientIDSeparator); 
        base.OnInit(e);
    }

   protected override void Render(HtmlTextWriter writer)
    {
        //Render checkbox
        writer.AddAttribute(HtmlTextWriterAttribute.Id, checkboxClientID);
        writer.AddAttribute(HtmlTextWriterAttribute.Name, checkboxClientID);
        writer.AddAttribute(HtmlTextWriterAttribute.Type, "checkbox");
        writer.AddAttribute(HtmlTextWriterAttribute.Value, Values);
        writer.RenderBeginTag(HtmlTextWriterTag.Input);
        writer.RenderEndTag();

        //Render hidden field
        writer.AddAttribute(HtmlTextWriterAttribute.Id, String.Format("{0}{1}hdnExcludeValues", base.ClientID, base.ClientIDSeparator));
        writer.AddAttribute(HtmlTextWriterAttribute.Name, String.Format("{0}{1}hdnExcludeValues", base.ClientID, base.ClientIDSeparator));
        writer.AddAttribute(HtmlTextWriterAttribute.Type, "hidden");
        writer.AddAttribute(HtmlTextWriterAttribute.Value, ExcludeValues.ToString());
        writer.RenderBeginTag(HtmlTextWriterTag.Input);
        writer.RenderEndTag();
    }
}

Now I want to retrieve the value of the hidden field and the checkbox when it is posted back - is it possible with the standard html I have rendered?

I've already written jQuery that works with this markup but it does require a standard html checkbox with a value - not the kind of checkbox rendered by ASP.NET.

If it is not possible to retrieve the value from standard html inputs, do I need to rewrite this as a composite control, or is there another trick?

+2  A: 

The Form property of the Request object contains the values of all form elements for the current request.

string key = ...; // "name" attribute of the form element
string val = Request.Form[key];
Etienne de Martel
You are absolutely correct, but what was throwing me was that my checkbox value was null. But of course the reason it was null is that the value is not posted if the checkbox isn't selected. Doh....now I just need another hidden field....
Colin