Click here to Skip to main content
15,893,668 members
Please Sign up or sign in to vote.
1.33/5 (3 votes)
See more:
In javascript function I checking checkbox and setting value to label after satisfication of perticular condition.But in serverside event I am getting checkbox1.checked = false and text of label is default text of that label, not modified one.Why this is happening?How to handle that?

What I have tried:

I made checkbox1.checked== true in javascript
Posted
Updated 9-Jun-16 23:32pm
Comments
ZurdoDev 9-Jun-16 9:31am    
Click improve question and show all relevant code.
Sergey Alexandrovich Kryukov 9-Jun-16 11:14am    
It tells me that you need to learn what Web does and the life cycle of the Web page. Probably, your page does postback, and nothing tells it the changed states of the controls. After postback, everything is rendered from scratch. You are the one who is supposed to take care of that. There are different ways to do that, including one purely client-side way agnostic to the server side. I'm not sure you can use help immediately; I would advise you to learn general principles of Web operation first.
—SA
Zafar Sultan 10-Jun-16 6:52am    
He needs "answer" not your advice :D:D:D
Member 11589429 10-Jun-16 0:49am    
Thanks Sergey Alexandrovich Kryukov for ur advice.I know postback happen.Actually I was expecting answer not theory part.By the way I handled it using hidden field.It is always better to answer question if u know instead of diverting mind.
Zafar Sultan 10-Jun-16 6:50am    
He is right. It is up to you to take care of maintaining state of your controls on your page. We can not read your complete code remotely. It looks like there is a postback which refreshes the page and due t which your controls are rendering again. This is the normal way a web page behaves. What is it that diverts your mind? Why this rudeness? Just because you have been pointed in the right direction? This is surely not a sign of a good programmer. Always looking for answers provided by others is not an option. You will only get direction. Rest is up to you. And by the way checkbox1.checked== true is not assignment and by any chance(which is impossible) it becomes in a century or two, it has nothing to do with server side code.

1 solution

Here is the implementation i tried with the scenario:-

ASP.NET
<p>
        <asp:checkbox id="chkTest" runat="server" clientidmode="Static" xmlns:asp="#unknown" />
        <asp:label id="lblTest" runat="server" text="Label" clientidmode="Static" xmlns:asp="#unknown"></asp:label>
    </p>
    <p>
        <asp:button id="btnTest" runat="server" text="Button" onclick="btnTest_Click" xmlns:asp="#unknown" />
    </p>



Javascript Code:-

JavaScript
<script type="text/javascript">
        $(function () {
            document.getElementById('chkTest').checked = true;
            $('#lblTest').text('Smruti');
        });
    </script>


On ServerSide button click:-

C#
protected void btnTest_Click(object sender, EventArgs e)
        {
            bool val = chkTest.Checked;
        }


I think the property value
HTML
ClientIDMode="Static"
is the thing which you have to take care.

Please let me know if this is helpful or please share your code and we will analyse to get the things done.
 
Share this answer
 
Comments
Member 11589429 14-Jun-16 3:33am    
Hi SRS(The Coder),thanks for ur help.But I want to ask one thing.When we say variable is static,scope of it at application level.R u sure will this thing will not apply to control
SRS(The Coder) 14-Jun-16 5:32am    
r u asking about ClientIDMode property of the control ?

If yes, the concept behind it is
when the respective html page is being rendered for any aspx page the asp.net controls also get rendered to html elements whose id gets generated randomly to a unpredictable value. Using this property setting to static, it maintains the same id what we have given to the asp.net control only.

Hope this makes sense.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900