Risho
Risho

Reputation: 2647

Checkbox not working in asp.net and HTML mixed page

I'm grappling with an issue where the site is asp.net/C# but controls on the .aspx pages are HTML and I'm not sure how well it would go over if I would change everything into asp.net controls. Also the change is minor. I was tasked to add a check box, as in <input type="checkbox" name="disableFeatureManager" runat="server" id="disableFeatureManager" />Disable Feature Manager and in the .cs page I want to check if the box is checked and make decisions based on that, but the control's checked property is always false. The submit button is also a HTML control: <input type="submit" value="Start" name="submitButton" />

In the Page_Load ckecking if check like this returns false.

if (disableFeatureManager != null && disableFeatureManager.Checked)
    nextURL.Append(FeatureManagerChoices.CreateQueryStringFromFormData(Request.Form));

Upvotes: 2

Views: 1253

Answers (2)

Risho
Risho

Reputation: 2647

As I mentioned before, I was trown into the deep end of MVC. Co-worker offered a simple answer(as least in this case):

bool isChecked = false;
if(Request["disableFeatureManager"] != null)
    isChecked = (Request["disableFeatureManager"].ToLower() == "on");

if (!isChecked)
    nextURL.Append(FeatureManagerChoices.CreateQueryStringFromFormData(Request.Form));

I think this can be further simplified in to one IF statement.

Upvotes: 0

hutchonoid
hutchonoid

Reputation: 33306

You could keep your checkbox as an Html server control by doing the following:

<input type="checkbox" name="disableFeatureManager" runat="server" id="disableFeatureManager" />

Then you could change your button to a web control as follows:

<asp:Button ID="submitStart" runat="server" OnClick="btn1_Click" Text="Start" ClientIDMode="Static" />

The only difference with the above rendered HTML will be the name that is output but you will have an Id that is submitStart due to the ClientIdMode being static, if you need a friendly consistent Id for javascript manipulation.

To wire in the event add this code which will read the value from the checkbox:

    protected void btn1_Click(object sender, EventArgs e)
    {
        var isChecked = disableFeatureManager.Checked;
    }

Checked image

Upvotes: 1

Related Questions