0

Is it possible to set a property of the code behind using javascript?

I have the following:

private string dateFormat;
public string DateFormat
{
    get
    {
        return dateFormat;
    }
    set
    {
        dateFormat = value;
    }
}

and want to set it like this: '<%=DateFormat%>' = "dd-mm-YYYY"

But when I run this and add a debbugger it comes out like this: '' = "dd-mm-YYYY".

0

2 Answers 2

2

Try without quotes:

var <%=DateFormat%> = "dd-mm-YYYY";

but in your case the property does not have a value so you should check if it is set.

Sign up to request clarification or add additional context in comments.

Comments

1

You can't do this directly; you need to use an ASP.NET hidden field which will then return the value to your code on a postback, and persist it across multiple postbacks.

The C# would be like

public string DateFormat
{
    get
    {
        return DateFormatField.Value;
    }
    set
    {
        DateFormatField.Value = value;
    }
}

The JavaScript to set it would be something like

document.getElementById('<%=DateFormatField.ClientID%>').value = 'dd-mm-YYYY';

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.