Access JavaScript disabled checkbox in server side

  • (2 Pages)
  • +
  • 1
  • 2

25 Replies - 1639 Views - Last Post: 15 February 2018 - 05:52 PM

#1 madhan1   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 28-December 17

Access JavaScript disabled checkbox in server side

Posted 08 February 2018 - 10:29 AM

Hi - I have two asp textbox controls. Either one is disabled via javascript. How do I check whether the textbox is enabled or disabled in C# code behind?

I searched and found the below post:
My link

I am planning to follow below suggestion that is mentioned in that post.

"So the only thing you can do is to send a new request containing the textbox´ enabled-state via a hidden field on your form. So whenever you send a request to your server you also send the fields value also."

This is the way I am trying to create a HiddenField in code behind class. (Please note: I wanted to create this Hiddenfield in code behind only)

HtmlInputHidden HiddenCity = new HtmlInputHidden();
 HiddenCity = this.ViewState["m_Text"];
            if (HiddenCity.Enabled != true)
            {
            }



Please help. How do I check whether the textbox is enabled or disabled in C# code behind?

Is This A Good Question/Topic? 0
  • +

Replies To: Access JavaScript disabled checkbox in server side

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15262
  • View blog
  • Posts: 61,181
  • Joined: 12-June 08

Re: Access JavaScript disabled checkbox in server side

Posted 08 February 2018 - 10:30 AM

Is this ASP.NET?
Weforms?
MVC?
Razor page?
Was This Post Helpful? 0
  • +
  • -

#3 madhan1   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 28-December 17

Re: Access JavaScript disabled checkbox in server side

Posted 08 February 2018 - 10:54 AM

Thanks, This is ASP.NET web forms.
Was This Post Helpful? 0
  • +
  • -

#4 madhan1   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 28-December 17

Re: Access JavaScript disabled checkbox in server side

Posted 09 February 2018 - 03:34 PM

Can anyone please help on this?
Was This Post Helpful? 0
  • +
  • -

#5 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 7056
  • View blog
  • Posts: 23,987
  • Joined: 05-May 12

Re: Access JavaScript disabled checkbox in server side

Posted 09 February 2018 - 05:04 PM

Did you even try reading the documentation? It has sample code.

The main idea is to declare the hidden control as part of the .ASPX page, and then access it in the code behind to see what values may have been stuffed into it by the client side Javascript code.
Was This Post Helpful? 0
  • +
  • -

#6 madhan1   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 28-December 17

Re: Access JavaScript disabled checkbox in server side

Posted 10 February 2018 - 08:16 AM

Thanks for the response. Yes, I am trying to use hidden control but I am not trying to get the value from client side javascript code. Instead I am trying to see if the control is enabled or disabled in client side and trying to get the enabled or disabled state control in server side.

Can anyone please show me an example on this. you can modify the code that is provided in my first post. thanks
Was This Post Helpful? 0
  • +
  • -

#7 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15262
  • View blog
  • Posts: 61,181
  • Joined: 12-June 08

Re: Access JavaScript disabled checkbox in server side

Posted 10 February 2018 - 08:18 AM

Quote

you can modify the code that is provided in my first post. thanks

Please be mindful for the rules regarding asking folk to do your work for you. :^:
Was This Post Helpful? 0
  • +
  • -

#8 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 7056
  • View blog
  • Posts: 23,987
  • Joined: 05-May 12

Re: Access JavaScript disabled checkbox in server side

Posted 10 February 2018 - 09:19 AM

Here's the steps you need to take:
Declare the HTML hidden input in the aspx.
In the Javascript, whenever you change the enabled state of the text box, also save a value in the hidden input representing the state.
In your code behind, check the value stored in the hidden input.
Was This Post Helpful? 0
  • +
  • -

#9 madhan1   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 28-December 17

Re: Access JavaScript disabled checkbox in server side

Posted 10 February 2018 - 02:16 PM

Hi - I am just looking for an example for me to solve the problem. I already tried on my own writing code

HtmlInputHidden HiddenCity = new HtmlInputHidden();

 HiddenCity = this.ViewState["m_Text"]; //here how would I write code to access the m_Text disabled textbox

            if (HiddenCity.Enabled != true)

            {

            }



in line 2. I am not sure how I can access. please help on that code example. any kind of example that you could direct me that would be helpful
Was This Post Helpful? 0
  • +
  • -

#10 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 7056
  • View blog
  • Posts: 23,987
  • Joined: 05-May 12

Re: Access JavaScript disabled checkbox in server side

Posted 10 February 2018 - 05:34 PM

Throw away your example code. And no, I am not going to edit your existing "example" code because it is simply wrong trying to check the ViewState of the form, AND because that would be doing your work for you which is against the forum rules.

You have already written all the code that you need to solve this problem. As I said in post #9, all you need are these things:
  • Declare the HTML hidden input in the aspx.
  • In the Javascript, whenever you change the enabled state of the text box, also save a value in the hidden input representing the state.
  • In your code behind, check the value stored in the hidden input.


For #1, you already know how to declare a plain old textbox is the ASPx. Yes, that same textbox your Javascript is enabling and disabling. Declaring the hidden input is not any different. See the documentation that I linked to in post #5 for an example.

For #2, you already solved the problem of figuring out how Javascript can access an ASP.NET textbox otherwise you wouldn't already be enabling/disabling the textbox. You just now have to also access the the hidden input and also set its value. This in no different from getting/setting values visible input controls. If your old Javascript for enabling/disabling the textbox looked like this pseudo code:
if (some condition) {
    enable textbox
} else {
    disable textbox
}


It would now look like this pseudo code:
if (some condition) {
    enable textbox
    set hidden input value to "I can see clearly now."
} else {
    disable textbox
    set hidden input value to "I wear my sunglasses at night"
}



For #3, in your code behind you already have code that reads the value from your textbox (that gets enabled and disabled), because you must be doing something with that textbox when it is enabled. Getting the value from the hidden input control is not any different. If the value you get is "I can see clearly now", the textbox must be enabled. If the value is "I wear my sunglasses at night", then the textbox must be disabled. Again, see the documentation that I linked to in post #5 for an example.
Was This Post Helpful? 1
  • +
  • -

#11 madhan1   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 28-December 17

Re: Access JavaScript disabled checkbox in server side

Posted 12 February 2018 - 06:17 PM

Hi - Thanks for the direction. I have javascript code in class file not in aspx page. so, I have currently code like this in class.

HtmlInputHidden HiddenCity = new HtmlInputHidden();
        HtmlInputHidden HiddenTownship = new HtmlInputHidden();


string script = this.ClientID + "TownshipOrCity";
            Type csType = this.GetType();

            StringBuilder sb = new StringBuilder();
            sb.AppendFormat("function {0}townshipOrCity() ", this.ClientID);
            sb.Append("{");
            sb.Append(Environment.NewLine);
            sb.AppendFormat("var cityText = document.getElementById('{0}');{1}", this.m_cityText.ClientID, Environment.NewLine);
            sb.AppendFormat("var townshipText = document.getElementById('{0}');{1}", this.m_townshipText.ClientID, Environment.NewLine);
            sb.AppendFormat("if (cityText.value.length >= 1){0}", Environment.NewLine);
            sb.Append(" { ");
            sb.AppendFormat("townshipText.disabled = true;{0}", Environment.NewLine);
            sb.AppendFormat("document.getElementById('{0}').value = 'disabled';", this.HiddenTownship.ClientID);
            sb.Append("}");



 if (HiddenTownship.Value == "disabled")
            {
                Township.Enabled = false;
} 


I am not sure what is going wrong. in the if condition HiddenTownship.value is empty always. Do you see the problem here, please let me know.
Was This Post Helpful? 0
  • +
  • -

#12 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 7056
  • View blog
  • Posts: 23,987
  • Joined: 05-May 12

Re: Access JavaScript disabled checkbox in server side

Posted 12 February 2018 - 06:37 PM

As I told you before in #1 above, declare the hidden control in the ASPX. With your current code, it does not look like you have added the hidden control to the page.

Also, your building up that script using a string builder is a very hard to read and error prone approach. Why not put that into the ASPX as well?
Was This Post Helpful? 0
  • +
  • -

#13 baavgai   User is online

  • Dreaming Coder
  • member icon


Reputation: 7483
  • View blog
  • Posts: 15,510
  • Joined: 16-October 07

Re: Access JavaScript disabled checkbox in server side

Posted 13 February 2018 - 05:09 AM

View Postmadhan1, on 12 February 2018 - 08:17 PM, said:

 if (HiddenTownship.Value == "disabled")
            {
                Township.Enabled = false;
} 


You seem to believe that something on the Javascript side will can impact how ASP.NET behaves on the server side. Think about that for a moment...

Your server, ultimately, generates code that is then handed off to the client's browser. Once the browser has it, your server looses control. Your server can get involved again when it is called about something.

You could, reasonably, have a plain page with Javascript that redirects to another page. Then, if the page doesn't handle Javascript, the browser just stays on that page with the default text telling them they need Javascript, otherwise, the user changes to a pages that then know they've passed this initial test.

Also, this sb.AppendFormat("function {0}townshipOrCity() ", this.ClientID); is fundamentally horrid. You server should offer data only, preferably in the form of JSON. Your Javascript, in a .js file, code should use the provided data and go from there.
Was This Post Helpful? 0
  • +
  • -

#14 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 7056
  • View blog
  • Posts: 23,987
  • Joined: 05-May 12

Re: Access JavaScript disabled checkbox in server side

Posted 13 February 2018 - 08:08 AM

Looking at madhan1's older threads, it looks like he is build a custom control rather than web page. Regardless, the same principles apply. Instead of using a .ASPX file and corresponding code behind, use an .ASCX and corresponding code behind.

The reason why he is using the string builder is to insert the control ID values at runtime is because of the way ASP.NET web form controls work: ID's are dynamically assigned. The way to deal with this is to use something like this in the .ASCX or .ASPX:
<script>
function <%= ClientID %>townshipOrCity() {
    var cityText = document.getElementById('<%= m_cityText.ClientID %>');
    var townshipText = document.getElementById('<%= m_townshipText.ClientID %>');
    if (cityText.value.length >= 1) {
        townshipText.disabled = true;
        document.getElementById('<%= m_hiddenTownship.ClientID %>').value = 'disabled';
    }
}
</script>


Was This Post Helpful? 0
  • +
  • -

#15 baavgai   User is online

  • Dreaming Coder
  • member icon


Reputation: 7483
  • View blog
  • Posts: 15,510
  • Joined: 16-October 07

Re: Access JavaScript disabled checkbox in server side

Posted 13 February 2018 - 09:23 AM

Understood, but the mix of data and code can still easily be avoided. e.g.
<script>
const tocDat = {
    cityId: '<%= m_cityText.ClientID %>',
    townshipId: '<%= m_townshipText.ClientID %>',
    hiddenTownshipId: '<%= m_hiddenTownship.ClientID %>'
};
function townshipOrCity() {
    var cityText = document.getElementById(tocDat.cityId);
    var townshipText = document.getElementById(tocDat.townshipId);
    if (cityText.value.length >= 1) {
        townshipText.disabled = true;
        document.getElementById(tocDat.hiddenTownshipId).value = 'disabled';
    }
}
</script>



Here, data is generated on the server. There are cleaner ways to do this, but the point is that code separated from data.

Granted, that rewriting of the function name is extra messy and honestly looks like a design flaw to me. However, if you insisted on it, you could go with something like:
<script>
const allData = {
    '<%= ClientID %>': {
        cityId: '<%= m_cityText.ClientID %>',
        townshipId: '<%= m_townshipText.ClientID %>',
        hiddenTownshipId: '<%= m_hiddenTownship.ClientID %>'
    }
};
function <%= ClientID %>townshipOrCity() { townshipOrCity(allData['<%= ClientID %>']; }

function townshipOrCity(tocDat) {
    var cityText = document.getElementById(tocDat.cityId);
    var townshipText = document.getElementById(tocDat.townshipId);
    if (cityText.value.length >= 1) {
        townshipText.disabled = true;
        document.getElementById(tocDat.hiddenTownshipId).value = 'disabled';
    }
}
</script>



Still allowing the bulk of the code to be written as code and tested reasonably.
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2